Skip to content

Commit 4462b4a

Browse files
committed
Elaborate all box dereferences in ElaborateBoxDerefs
so that it is the only pass responsible for elaboration, instead of splitting this responsibility between the `StateTransform` and `ElaborateBoxDerefs`.
1 parent 31d892a commit 4462b4a

File tree

2 files changed

+9
-102
lines changed

2 files changed

+9
-102
lines changed

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,8 @@ impl<'tcx> MirPass<'tcx> for ElaborateBoxDerefs {
107107
let mut visitor =
108108
ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch };
109109

110-
for (block, BasicBlockData { statements, terminator, .. }) in
111-
body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut()
112-
{
113-
let mut index = 0;
114-
for statement in statements {
115-
let location = Location { block, statement_index: index };
116-
visitor.visit_statement(statement, location);
117-
index += 1;
118-
}
119-
120-
let location = Location { block, statement_index: index };
121-
match terminator {
122-
// yielding into a box is handled when lowering generators
123-
Some(Terminator { kind: TerminatorKind::Yield { value, .. }, .. }) => {
124-
visitor.visit_operand(value, location);
125-
}
126-
Some(terminator) => {
127-
visitor.visit_terminator(terminator, location);
128-
}
129-
None => {}
130-
}
110+
for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
111+
visitor.visit_basic_block_data(block, data);
131112
}
132113

133114
visitor.patch.apply(body);

compiler/rustc_mir_transform/src/generator.rs

+7-81
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,6 @@ fn create_cases<'tcx>(
11821182
transform: &TransformVisitor<'tcx>,
11831183
operation: Operation,
11841184
) -> Vec<(usize, BasicBlock)> {
1185-
let tcx = transform.tcx;
1186-
11871185
let source_info = SourceInfo::outermost(body.span);
11881186

11891187
transform
@@ -1216,85 +1214,13 @@ fn create_cases<'tcx>(
12161214
if operation == Operation::Resume {
12171215
// Move the resume argument to the destination place of the `Yield` terminator
12181216
let resume_arg = Local::new(2); // 0 = return, 1 = self
1219-
1220-
// handle `box yield` properly
1221-
let box_place = if let [projection @ .., ProjectionElem::Deref] =
1222-
&**point.resume_arg.projection
1223-
{
1224-
let box_place =
1225-
Place::from(point.resume_arg.local).project_deeper(projection, tcx);
1226-
1227-
let box_ty = box_place.ty(&body.local_decls, tcx).ty;
1228-
1229-
if box_ty.is_box() { Some((box_place, box_ty)) } else { None }
1230-
} else {
1231-
None
1232-
};
1233-
1234-
if let Some((box_place, box_ty)) = box_place {
1235-
let unique_did = box_ty
1236-
.ty_adt_def()
1237-
.expect("expected Box to be an Adt")
1238-
.non_enum_variant()
1239-
.fields[0]
1240-
.did;
1241-
1242-
let Some(nonnull_def) = tcx.type_of(unique_did).ty_adt_def() else {
1243-
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
1244-
};
1245-
1246-
let nonnull_did = nonnull_def.non_enum_variant().fields[0].did;
1247-
1248-
let (unique_ty, nonnull_ty, ptr_ty) =
1249-
crate::elaborate_box_derefs::build_ptr_tys(
1250-
tcx,
1251-
box_ty.boxed_ty(),
1252-
unique_did,
1253-
nonnull_did,
1254-
);
1255-
1256-
let ptr_local = body.local_decls.push(LocalDecl::new(ptr_ty, body.span));
1257-
1258-
statements.push(Statement {
1259-
source_info,
1260-
kind: StatementKind::StorageLive(ptr_local),
1261-
});
1262-
1263-
statements.push(Statement {
1264-
source_info,
1265-
kind: StatementKind::Assign(Box::new((
1266-
Place::from(ptr_local),
1267-
Rvalue::Use(Operand::Copy(box_place.project_deeper(
1268-
&crate::elaborate_box_derefs::build_projection(
1269-
unique_ty, nonnull_ty, ptr_ty,
1270-
),
1271-
tcx,
1272-
))),
1273-
))),
1274-
});
1275-
1276-
statements.push(Statement {
1277-
source_info,
1278-
kind: StatementKind::Assign(Box::new((
1279-
Place::from(ptr_local)
1280-
.project_deeper(&[ProjectionElem::Deref], tcx),
1281-
Rvalue::Use(Operand::Move(resume_arg.into())),
1282-
))),
1283-
});
1284-
1285-
statements.push(Statement {
1286-
source_info,
1287-
kind: StatementKind::StorageDead(ptr_local),
1288-
});
1289-
} else {
1290-
statements.push(Statement {
1291-
source_info,
1292-
kind: StatementKind::Assign(Box::new((
1293-
point.resume_arg,
1294-
Rvalue::Use(Operand::Move(resume_arg.into())),
1295-
))),
1296-
});
1297-
}
1217+
statements.push(Statement {
1218+
source_info,
1219+
kind: StatementKind::Assign(Box::new((
1220+
point.resume_arg,
1221+
Rvalue::Use(Operand::Move(resume_arg.into())),
1222+
))),
1223+
});
12981224
}
12991225

13001226
// Then jump to the real target

0 commit comments

Comments
 (0)