Use let/else to de-indent ElaborateBoxDerefs::run_pass.

This commit is contained in:
Nicholas Nethercote 2024-09-09 08:49:47 +10:00
parent cc09ab3c75
commit 181fbd5ce8

View file

@ -92,64 +92,61 @@ pub(super) struct ElaborateBoxDerefs;
impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs { impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if let Some(def_id) = tcx.lang_items().owned_box() { // If box is not present, this pass doesn't need to do anything.
let unique_did = tcx.adt_def(def_id).non_enum_variant().fields[FieldIdx::ZERO].did; let Some(def_id) = tcx.lang_items().owned_box() else { return };
let Some(nonnull_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def() let unique_did = tcx.adt_def(def_id).non_enum_variant().fields[FieldIdx::ZERO].did;
else {
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
};
let nonnull_did = nonnull_def.non_enum_variant().fields[FieldIdx::ZERO].did; let Some(nonnull_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def() else {
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
};
let patch = MirPatch::new(body); let nonnull_did = nonnull_def.non_enum_variant().fields[FieldIdx::ZERO].did;
let local_decls = &mut body.local_decls; let patch = MirPatch::new(body);
let mut visitor = let local_decls = &mut body.local_decls;
ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch };
for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() { let mut visitor =
visitor.visit_basic_block_data(block, data); ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch };
}
visitor.patch.apply(body); for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
visitor.visit_basic_block_data(block, data);
}
for debug_info in body.var_debug_info.iter_mut() { visitor.patch.apply(body);
if let VarDebugInfoContents::Place(place) = &mut debug_info.value {
let mut new_projections: Option<Vec<_>> = None;
for (base, elem) in place.iter_projections() { for debug_info in body.var_debug_info.iter_mut() {
let base_ty = base.ty(&body.local_decls, tcx).ty; if let VarDebugInfoContents::Place(place) = &mut debug_info.value {
let mut new_projections: Option<Vec<_>> = None;
if let PlaceElem::Deref = elem for (base, elem) in place.iter_projections() {
&& let Some(boxed_ty) = base_ty.boxed_ty() let base_ty = base.ty(&body.local_decls, tcx).ty;
{
// Clone the projections before us, since now we need to mutate them.
let new_projections =
new_projections.get_or_insert_with(|| base.projection.to_vec());
let (unique_ty, nonnull_ty, ptr_ty) = if let PlaceElem::Deref = elem
build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did); && let Some(boxed_ty) = base_ty.boxed_ty()
{
// Clone the projections before us, since now we need to mutate them.
let new_projections =
new_projections.get_or_insert_with(|| base.projection.to_vec());
new_projections.extend_from_slice(&build_projection( let (unique_ty, nonnull_ty, ptr_ty) =
unique_ty, nonnull_ty, ptr_ty, build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did);
));
new_projections.push(PlaceElem::Deref);
} else if let Some(new_projections) = new_projections.as_mut() {
// Keep building up our projections list once we've started it.
new_projections.push(elem);
}
}
// Store the mutated projections if we actually changed something. new_projections
if let Some(new_projections) = new_projections { .extend_from_slice(&build_projection(unique_ty, nonnull_ty, ptr_ty));
place.projection = tcx.mk_place_elems(&new_projections); new_projections.push(PlaceElem::Deref);
} else if let Some(new_projections) = new_projections.as_mut() {
// Keep building up our projections list once we've started it.
new_projections.push(elem);
} }
} }
// Store the mutated projections if we actually changed something.
if let Some(new_projections) = new_projections {
place.projection = tcx.mk_place_elems(&new_projections);
}
} }
} else {
// box is not present, this pass doesn't need to do anything
} }
} }
} }