Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,32 +1065,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(_) => {}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/consts/promote_borrowed_field.rs
Original file line number Diff line number Diff line change
@@ -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;
}