Skip to content

Commit 8345391

Browse files
committed
Use let/else to de-indent ElaborateBoxDerefs::run_pass.
1 parent 0ac6d8a commit 8345391

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,62 +90,59 @@ pub(super) struct ElaborateBoxDerefs;
9090

9191
impl<'tcx> MirPass<'tcx> for ElaborateBoxDerefs {
9292
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
93-
if let Some(def_id) = tcx.lang_items().owned_box() {
94-
let unique_did = tcx.adt_def(def_id).non_enum_variant().fields[FieldIdx::ZERO].did;
93+
// If box is not present, this pass doesn't need to do anything.
94+
let Some(def_id) = tcx.lang_items().owned_box() else { return };
9595

96-
let Some(nonnull_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def()
97-
else {
98-
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
99-
};
96+
let unique_did = tcx.adt_def(def_id).non_enum_variant().fields[FieldIdx::ZERO].did;
10097

101-
let nonnull_did = nonnull_def.non_enum_variant().fields[FieldIdx::ZERO].did;
98+
let Some(nonnull_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def() else {
99+
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
100+
};
102101

103-
let patch = MirPatch::new(body);
102+
let nonnull_did = nonnull_def.non_enum_variant().fields[FieldIdx::ZERO].did;
104103

105-
let local_decls = &mut body.local_decls;
104+
let patch = MirPatch::new(body);
106105

107-
let mut visitor =
108-
ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch };
106+
let local_decls = &mut body.local_decls;
109107

110-
for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
111-
visitor.visit_basic_block_data(block, data);
112-
}
108+
let mut visitor =
109+
ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch };
113110

114-
visitor.patch.apply(body);
111+
for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
112+
visitor.visit_basic_block_data(block, data);
113+
}
115114

116-
for debug_info in body.var_debug_info.iter_mut() {
117-
if let VarDebugInfoContents::Place(place) = &mut debug_info.value {
118-
let mut new_projections: Option<Vec<_>> = None;
115+
visitor.patch.apply(body);
119116

120-
for (base, elem) in place.iter_projections() {
121-
let base_ty = base.ty(&body.local_decls, tcx).ty;
117+
for debug_info in body.var_debug_info.iter_mut() {
118+
if let VarDebugInfoContents::Place(place) = &mut debug_info.value {
119+
let mut new_projections: Option<Vec<_>> = None;
122120

123-
if elem == PlaceElem::Deref && base_ty.is_box() {
124-
// Clone the projections before us, since now we need to mutate them.
125-
let new_projections =
126-
new_projections.get_or_insert_with(|| base.projection.to_vec());
121+
for (base, elem) in place.iter_projections() {
122+
let base_ty = base.ty(&body.local_decls, tcx).ty;
127123

128-
let (unique_ty, nonnull_ty, ptr_ty) =
129-
build_ptr_tys(tcx, base_ty.boxed_ty(), unique_did, nonnull_did);
124+
if elem == PlaceElem::Deref && base_ty.is_box() {
125+
// Clone the projections before us, since now we need to mutate them.
126+
let new_projections =
127+
new_projections.get_or_insert_with(|| base.projection.to_vec());
130128

131-
new_projections.extend_from_slice(&build_projection(
132-
unique_ty, nonnull_ty, ptr_ty,
133-
));
134-
new_projections.push(PlaceElem::Deref);
135-
} else if let Some(new_projections) = new_projections.as_mut() {
136-
// Keep building up our projections list once we've started it.
137-
new_projections.push(elem);
138-
}
139-
}
129+
let (unique_ty, nonnull_ty, ptr_ty) =
130+
build_ptr_tys(tcx, base_ty.boxed_ty(), unique_did, nonnull_did);
140131

141-
// Store the mutated projections if we actually changed something.
142-
if let Some(new_projections) = new_projections {
143-
place.projection = tcx.mk_place_elems(&new_projections);
132+
new_projections
133+
.extend_from_slice(&build_projection(unique_ty, nonnull_ty, ptr_ty));
134+
new_projections.push(PlaceElem::Deref);
135+
} else if let Some(new_projections) = new_projections.as_mut() {
136+
// Keep building up our projections list once we've started it.
137+
new_projections.push(elem);
144138
}
145139
}
140+
141+
// Store the mutated projections if we actually changed something.
142+
if let Some(new_projections) = new_projections {
143+
place.projection = tcx.mk_place_elems(&new_projections);
144+
}
146145
}
147-
} else {
148-
// box is not present, this pass doesn't need to do anything
149146
}
150147
}
151148
}

0 commit comments

Comments
 (0)