Skip to content

Commit 9bd740a

Browse files
committed
Auto merge of #77947 - tmiasko:promoted-scope, r=oli-obk
Create a single source scope for promoteds A promoted inherits all scopes from the parent body. At the same time, almost all statements and terminators inside the promoted body so far refer only to one of those scopes: the outermost one. Instead of inheriting all scopes, inherit only a single scope corresponding to the location of the promoted, making sure that there are no references to other scopes.
2 parents b6e2dc6 + db54752 commit 9bd740a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/rustc_mir/src/transform/promote_consts.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ impl Candidate {
124124
Candidate::Argument { .. } | Candidate::InlineAsm { .. } => true,
125125
}
126126
}
127+
128+
fn source_info(&self, body: &Body<'_>) -> SourceInfo {
129+
match self {
130+
Candidate::Ref(location) | Candidate::Repeat(location) => *body.source_info(*location),
131+
Candidate::Argument { bb, .. } | Candidate::InlineAsm { bb, .. } => {
132+
*body.source_info(body.terminator_loc(*bb))
133+
}
134+
}
135+
}
127136
}
128137

129138
fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<usize>> {
@@ -953,6 +962,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
953962
from_hir_call,
954963
fn_span,
955964
},
965+
source_info: SourceInfo::outermost(terminator.source_info.span),
956966
..terminator
957967
};
958968
}
@@ -1163,12 +1173,13 @@ pub fn promote_candidates<'tcx>(
11631173
// Declare return place local so that `mir::Body::new` doesn't complain.
11641174
let initial_locals = iter::once(LocalDecl::new(tcx.types.never, body.span)).collect();
11651175

1176+
let mut scope = body.source_scopes[candidate.source_info(body).scope].clone();
1177+
scope.parent_scope = None;
1178+
11661179
let mut promoted = Body::new(
11671180
body.source, // `promoted` gets filled in below
11681181
IndexVec::new(),
1169-
// FIXME: maybe try to filter this to avoid blowing up
1170-
// memory usage?
1171-
body.source_scopes.clone(),
1182+
IndexVec::from_elem_n(scope, 1),
11721183
initial_locals,
11731184
IndexVec::new(),
11741185
0,

src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].ConstProp.after.mir

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ promoted[0] in FOO: &[&i32; 1] = {
55
let mut _1: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
66
let mut _2: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
77
let mut _3: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
8-
scope 1 {
9-
}
108

119
bb0: {
1210
_3 = const {alloc2: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43

0 commit comments

Comments
 (0)