diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 76899dad41a74..3de9998653d0a 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -1066,32 +1066,23 @@ impl<'a, 'tcx> Checker<'a, 'tcx> { } else { self.valid_promotion_candidates() }; + debug!("qualify_const: promotion_candidates={:?}", promotion_candidates); for candidate in promotion_candidates { match candidate { - Candidate::Repeat(Location { block: bb, statement_index: stmt_idx }) => { - if let StatementKind::Assign(box(_, Rvalue::Repeat( - Operand::Move(place), - _ - ))) = &self.body[bb].statements[stmt_idx].kind { - if let Some(index) = place.as_local() { - promoted_temps.insert(index); - } - } - } Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => { - if let StatementKind::Assign( - box( - _, - Rvalue::Ref(_, _, place) - ) - ) = &self.body[bb].statements[stmt_idx].kind { - if let Some(index) = place.as_local() { - promoted_temps.insert(index); + if let StatementKind::Assign(box( _, Rvalue::Ref(_, _, place))) + = &self.body[bb].statements[stmt_idx].kind + { + if let PlaceBase::Local(local) = place.base { + promoted_temps.insert(local); } } } - Candidate::Argument { .. } => {} + + // Only rvalue-static promotion requires extending the lifetime of the promoted + // local. + Candidate::Argument { .. } | Candidate::Repeat(_) => {} } } diff --git a/src/test/ui/consts/promote_borrowed_field.rs b/src/test/ui/consts/promote_borrowed_field.rs new file mode 100644 index 0000000000000..c4841b46f60d9 --- /dev/null +++ b/src/test/ui/consts/promote_borrowed_field.rs @@ -0,0 +1,12 @@ +// run-pass + +// From https://github.com/rust-lang/rust/issues/65727 + +const _: &i32 = { + let x = &(5, false).0; + x +}; + +fn main() { + let _: &'static i32 = &(5, false).0; +}