Skip to content

Commit 18e2a89

Browse files
committedAug 1, 2020
Auto merge of rust-lang#74945 - dingxiangfei2009:promote-static-ref-deref, r=oli-obk
[mir] Special treatment for dereferencing a borrow to a static definition Fix rust-lang#70584. As suggested by @oli-obk in this [comment](rust-lang#70584 (comment)), one can chase the definition of the local variable being de-referenced and check if it is a true static variable. If that is the case, `validate_place` will admit the promotion. This is my first time to contribute to `rustc`, and I have two questions. 1. A generalization to some extent is applied to decide if the promotion is possible in the static context. In case that there are more projection operations preceding the de-referencing, `validate_place` recursively decent into inner projection operations. I have put thoughts into its correctness but I am not totally sure about it. 2. I have a hard time to find a good place for the test case. This patch has to do with MIR, but this test case would look out of place compared to other tests in `src/test/ui/mir` or `src/test/ui/borrowck` because it does not generate errors while others do. It is tentatively placed in `src/test/ui/statics` for now. Thank you for any comments and suggestions!
2 parents 22e6099 + c511454 commit 18e2a89

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed
 

‎src/librustc_mir/transform/promote_consts.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,47 @@ impl<'tcx> Validator<'_, 'tcx> {
502502
fn validate_place(&self, place: PlaceRef<'tcx>) -> Result<(), Unpromotable> {
503503
match place {
504504
PlaceRef { local, projection: [] } => self.validate_local(local),
505-
PlaceRef { local: _, projection: [proj_base @ .., elem] } => {
505+
PlaceRef { local, projection: [proj_base @ .., elem] } => {
506506
match *elem {
507-
ProjectionElem::Deref | ProjectionElem::Downcast(..) => {
507+
ProjectionElem::Deref => {
508+
let mut not_promotable = true;
509+
// This is a special treatment for cases like *&STATIC where STATIC is a
510+
// global static variable.
511+
// This pattern is generated only when global static variables are directly
512+
// accessed and is qualified for promotion safely.
513+
if let TempState::Defined { location, .. } = self.temps[local] {
514+
let def_stmt =
515+
self.body[location.block].statements.get(location.statement_index);
516+
if let Some(Statement {
517+
kind:
518+
StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(c)))),
519+
..
520+
}) = def_stmt
521+
{
522+
if let Some(did) = c.check_static_ptr(self.tcx) {
523+
if let Some(hir::ConstContext::Static(..)) = self.const_kind {
524+
// The `is_empty` predicate is introduced to exclude the case
525+
// where the projection operations are [ .field, * ].
526+
// The reason is because promotion will be illegal if field
527+
// accesses preceed the dereferencing.
528+
// Discussion can be found at
529+
// https://github.com/rust-lang/rust/pull/74945#discussion_r463063247
530+
// There may be opportunity for generalization, but this needs to be
531+
// accounted for.
532+
if proj_base.is_empty()
533+
&& !self.tcx.is_thread_local_static(did)
534+
{
535+
not_promotable = false;
536+
}
537+
}
538+
}
539+
}
540+
}
541+
if not_promotable {
542+
return Err(Unpromotable);
543+
}
544+
}
545+
ProjectionElem::Downcast(..) => {
508546
return Err(Unpromotable);
509547
}
510548

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// check-pass
2+
3+
// Use of global static variables in literal values should be allowed for
4+
// promotion.
5+
// This test is to demonstrate the issue raised in
6+
// https://github.com/rust-lang/rust/issues/70584
7+
8+
// Literal values were previously promoted into local static values when
9+
// other global static variables are used.
10+
11+
struct A<T: 'static>(&'static T);
12+
struct B<T: 'static + ?Sized> {
13+
x: &'static T,
14+
}
15+
static STR: &'static [u8] = b"hi";
16+
static C: A<B<B<[u8]>>> = {
17+
A(&B {
18+
x: &B { x: STR },
19+
})
20+
};
21+
22+
pub struct Slice(&'static [i32]);
23+
24+
static CONTENT: i32 = 42;
25+
pub static CONTENT_MAP: Slice = Slice(&[CONTENT]);
26+
27+
pub static FOO: (i32, i32) = (42, 43);
28+
pub static CONTENT_MAP2: Slice = Slice(&[FOO.0]);
29+
30+
fn main() {
31+
assert_eq!(b"hi", C.0.x.x);
32+
assert_eq!(&[42], CONTENT_MAP.0);
33+
assert_eq!(&[42], CONTENT_MAP2.0);
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.