Rollup merge of #90633 - tmiasko:candidate-struct, r=nagisa

Refactor single variant `Candidate` enum into a struct

`Candidate` enum has only a single `Ref` variant.  Refactor it into a
struct and reduce overall indentation of the code by two levels.

No functional changes.
This commit is contained in:
Yuki Okushi 2021-11-19 13:06:34 +09:00 committed by GitHub
commit 48947eafda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -93,17 +93,8 @@ impl TempState {
/// returned value in a promoted MIR, unless it's a subset /// returned value in a promoted MIR, unless it's a subset
/// of a larger candidate. /// of a larger candidate.
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Candidate { pub struct Candidate {
/// Borrow of a constant temporary, candidate for lifetime extension. location: Location,
Ref(Location),
}
impl Candidate {
fn source_info(&self, body: &Body<'_>) -> SourceInfo {
match self {
Candidate::Ref(location) => *body.source_info(*location),
}
}
} }
struct Collector<'a, 'tcx> { struct Collector<'a, 'tcx> {
@ -167,7 +158,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
match *rvalue { match *rvalue {
Rvalue::Ref(..) => { Rvalue::Ref(..) => {
self.candidates.push(Candidate::Ref(location)); self.candidates.push(Candidate { location });
} }
_ => {} _ => {}
} }
@ -209,8 +200,7 @@ struct Unpromotable;
impl<'tcx> Validator<'_, 'tcx> { impl<'tcx> Validator<'_, 'tcx> {
fn validate_candidate(&self, candidate: Candidate) -> Result<(), Unpromotable> { fn validate_candidate(&self, candidate: Candidate) -> Result<(), Unpromotable> {
match candidate { let loc = candidate.location;
Candidate::Ref(loc) => {
let statement = &self.body[loc.block].statements[loc.statement_index]; let statement = &self.body[loc.block].statements[loc.statement_index];
match &statement.kind { match &statement.kind {
StatementKind::Assign(box (_, Rvalue::Ref(_, kind, place))) => { StatementKind::Assign(box (_, Rvalue::Ref(_, kind, place))) => {
@ -239,8 +229,6 @@ impl<'tcx> Validator<'_, 'tcx> {
_ => bug!(), _ => bug!(),
} }
} }
}
}
// FIXME(eddyb) maybe cache this? // FIXME(eddyb) maybe cache this?
fn qualif_local<Q: qualifs::Qualif>(&self, local: Local) -> bool { fn qualif_local<Q: qualifs::Qualif>(&self, local: Local) -> bool {
@ -871,8 +859,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
})) }))
}; };
let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut(); let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
match candidate { let loc = candidate.location;
Candidate::Ref(loc) => {
let statement = &mut blocks[loc.block].statements[loc.statement_index]; let statement = &mut blocks[loc.block].statements[loc.statement_index];
match statement.kind { match statement.kind {
StatementKind::Assign(box ( StatementKind::Assign(box (
@ -922,8 +909,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
} }
_ => bug!(), _ => bug!(),
} }
}
}
}; };
assert_eq!(self.new_block(), START_BLOCK); assert_eq!(self.new_block(), START_BLOCK);
@ -964,10 +949,8 @@ pub fn promote_candidates<'tcx>(
let mut extra_statements = vec![]; let mut extra_statements = vec![];
for candidate in candidates.into_iter().rev() { for candidate in candidates.into_iter().rev() {
match candidate { let Location { block, statement_index } = candidate.location;
Candidate::Ref(Location { block, statement_index }) => { if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind
if let StatementKind::Assign(box (place, _)) =
&body[block].statements[statement_index].kind
{ {
if let Some(local) = place.as_local() { if let Some(local) = place.as_local() {
if temps[local] == TempState::PromotedOut { if temps[local] == TempState::PromotedOut {
@ -976,13 +959,11 @@ pub fn promote_candidates<'tcx>(
} }
} }
} }
}
}
// Declare return place local so that `mir::Body::new` doesn't complain. // Declare return place local so that `mir::Body::new` doesn't complain.
let initial_locals = iter::once(LocalDecl::new(tcx.types.never, body.span)).collect(); let initial_locals = iter::once(LocalDecl::new(tcx.types.never, body.span)).collect();
let mut scope = body.source_scopes[candidate.source_info(body).scope].clone(); let mut scope = body.source_scopes[body.source_info(candidate.location).scope].clone();
scope.parent_scope = None; scope.parent_scope = None;
let promoted = Body::new( let promoted = Body::new(