Skip to content

Commit 3804876

Browse files
committed
Auto merge of #65728 - ecstatic-morse:promotion-const-proj, r=eddyb
Fix promotion in a `const` when projections are present Resolves #65727. This marks the entire local as "needs promotion" when only a projection of that local appears in a promotable context. This should only affect promotion in a `const` or `static`, not in a `fn` or `const fn`, which is handled in `promote_consts.rs`. r? @eddyb
2 parents 3f0e164 + 420457e commit 3804876

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/librustc_mir/transform/qualify_consts.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -1066,32 +1066,23 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
10661066
} else {
10671067
self.valid_promotion_candidates()
10681068
};
1069+
10691070
debug!("qualify_const: promotion_candidates={:?}", promotion_candidates);
10701071
for candidate in promotion_candidates {
10711072
match candidate {
1072-
Candidate::Repeat(Location { block: bb, statement_index: stmt_idx }) => {
1073-
if let StatementKind::Assign(box(_, Rvalue::Repeat(
1074-
Operand::Move(place),
1075-
_
1076-
))) = &self.body[bb].statements[stmt_idx].kind {
1077-
if let Some(index) = place.as_local() {
1078-
promoted_temps.insert(index);
1079-
}
1080-
}
1081-
}
10821073
Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
1083-
if let StatementKind::Assign(
1084-
box(
1085-
_,
1086-
Rvalue::Ref(_, _, place)
1087-
)
1088-
) = &self.body[bb].statements[stmt_idx].kind {
1089-
if let Some(index) = place.as_local() {
1090-
promoted_temps.insert(index);
1074+
if let StatementKind::Assign(box( _, Rvalue::Ref(_, _, place)))
1075+
= &self.body[bb].statements[stmt_idx].kind
1076+
{
1077+
if let PlaceBase::Local(local) = place.base {
1078+
promoted_temps.insert(local);
10911079
}
10921080
}
10931081
}
1094-
Candidate::Argument { .. } => {}
1082+
1083+
// Only rvalue-static promotion requires extending the lifetime of the promoted
1084+
// local.
1085+
Candidate::Argument { .. } | Candidate::Repeat(_) => {}
10951086
}
10961087
}
10971088

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
3+
// From https://github.com/rust-lang/rust/issues/65727
4+
5+
const _: &i32 = {
6+
let x = &(5, false).0;
7+
x
8+
};
9+
10+
fn main() {
11+
let _: &'static i32 = &(5, false).0;
12+
}

0 commit comments

Comments
 (0)