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:
commit
48947eafda
1 changed files with 76 additions and 95 deletions
|
@ -93,17 +93,8 @@ impl TempState {
|
|||
/// returned value in a promoted MIR, unless it's a subset
|
||||
/// of a larger candidate.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum Candidate {
|
||||
/// Borrow of a constant temporary, candidate for lifetime extension.
|
||||
Ref(Location),
|
||||
}
|
||||
|
||||
impl Candidate {
|
||||
fn source_info(&self, body: &Body<'_>) -> SourceInfo {
|
||||
match self {
|
||||
Candidate::Ref(location) => *body.source_info(*location),
|
||||
}
|
||||
}
|
||||
pub struct Candidate {
|
||||
location: Location,
|
||||
}
|
||||
|
||||
struct Collector<'a, 'tcx> {
|
||||
|
@ -167,7 +158,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
|
|||
|
||||
match *rvalue {
|
||||
Rvalue::Ref(..) => {
|
||||
self.candidates.push(Candidate::Ref(location));
|
||||
self.candidates.push(Candidate { location });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -209,8 +200,7 @@ struct Unpromotable;
|
|||
|
||||
impl<'tcx> Validator<'_, 'tcx> {
|
||||
fn validate_candidate(&self, candidate: Candidate) -> Result<(), Unpromotable> {
|
||||
match candidate {
|
||||
Candidate::Ref(loc) => {
|
||||
let loc = candidate.location;
|
||||
let statement = &self.body[loc.block].statements[loc.statement_index];
|
||||
match &statement.kind {
|
||||
StatementKind::Assign(box (_, Rvalue::Ref(_, kind, place))) => {
|
||||
|
@ -239,8 +229,6 @@ impl<'tcx> Validator<'_, 'tcx> {
|
|||
_ => bug!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME(eddyb) maybe cache this?
|
||||
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();
|
||||
match candidate {
|
||||
Candidate::Ref(loc) => {
|
||||
let loc = candidate.location;
|
||||
let statement = &mut blocks[loc.block].statements[loc.statement_index];
|
||||
match statement.kind {
|
||||
StatementKind::Assign(box (
|
||||
|
@ -922,8 +909,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
}
|
||||
_ => bug!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(self.new_block(), START_BLOCK);
|
||||
|
@ -964,10 +949,8 @@ pub fn promote_candidates<'tcx>(
|
|||
|
||||
let mut extra_statements = vec![];
|
||||
for candidate in candidates.into_iter().rev() {
|
||||
match candidate {
|
||||
Candidate::Ref(Location { block, statement_index }) => {
|
||||
if let StatementKind::Assign(box (place, _)) =
|
||||
&body[block].statements[statement_index].kind
|
||||
let Location { block, statement_index } = candidate.location;
|
||||
if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind
|
||||
{
|
||||
if let Some(local) = place.as_local() {
|
||||
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.
|
||||
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;
|
||||
|
||||
let promoted = Body::new(
|
||||
|
|
Loading…
Reference in a new issue