Auto merge of #95576 - DrMeepster:box_erasure, r=oli-obk

Remove dereferencing of Box from codegen

Through #94043, #94414, #94873, and #95328, I've been fixing issues caused by Box being treated like a pointer when it is not a pointer. However, these PRs just introduced special cases for Box. This PR removes those special cases and instead transforms a deref of Box into a deref of the pointer it contains.

Hopefully, this is the end of the Box<T, A> ICEs.
This commit is contained in:
bors 2022-06-21 11:00:39 +00:00
commit a25b1315ee
18 changed files with 477 additions and 162 deletions

View file

@ -56,7 +56,7 @@ use crate::MirPass;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::{BitMatrix, BitSet};
use rustc_index::bit_set::{BitMatrix, BitSet, GrowableBitSet};
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::mir::dump_mir;
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
@ -211,7 +211,7 @@ struct SuspensionPoint<'tcx> {
/// Which block to jump to if the generator is dropped in this state.
drop: Option<BasicBlock>,
/// Set of locals that have live storage while at this suspension point.
storage_liveness: BitSet<Local>,
storage_liveness: GrowableBitSet<Local>,
}
struct TransformVisitor<'tcx> {
@ -367,7 +367,7 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
resume,
resume_arg,
drop,
storage_liveness: self.storage_liveness[block].clone().unwrap(),
storage_liveness: self.storage_liveness[block].clone().unwrap().into(),
});
VariantIdx::new(state)
@ -1182,6 +1182,8 @@ fn create_cases<'tcx>(
transform: &TransformVisitor<'tcx>,
operation: Operation,
) -> Vec<(usize, BasicBlock)> {
let tcx = transform.tcx;
let source_info = SourceInfo::outermost(body.span);
transform
@ -1214,13 +1216,85 @@ fn create_cases<'tcx>(
if operation == Operation::Resume {
// Move the resume argument to the destination place of the `Yield` terminator
let resume_arg = Local::new(2); // 0 = return, 1 = self
statements.push(Statement {
source_info,
kind: StatementKind::Assign(Box::new((
point.resume_arg,
Rvalue::Use(Operand::Move(resume_arg.into())),
))),
});
// handle `box yield` properly
let box_place = if let [projection @ .., ProjectionElem::Deref] =
&**point.resume_arg.projection
{
let box_place =
Place::from(point.resume_arg.local).project_deeper(projection, tcx);
let box_ty = box_place.ty(&body.local_decls, tcx).ty;
if box_ty.is_box() { Some((box_place, box_ty)) } else { None }
} else {
None
};
if let Some((box_place, box_ty)) = box_place {
let unique_did = box_ty
.ty_adt_def()
.expect("expected Box to be an Adt")
.non_enum_variant()
.fields[0]
.did;
let Some(nonnull_def) = tcx.type_of(unique_did).ty_adt_def() else {
span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique")
};
let nonnull_did = nonnull_def.non_enum_variant().fields[0].did;
let (unique_ty, nonnull_ty, ptr_ty) =
crate::elaborate_box_derefs::build_ptr_tys(
tcx,
box_ty.boxed_ty(),
unique_did,
nonnull_did,
);
let ptr_local = body.local_decls.push(LocalDecl::new(ptr_ty, body.span));
statements.push(Statement {
source_info,
kind: StatementKind::StorageLive(ptr_local),
});
statements.push(Statement {
source_info,
kind: StatementKind::Assign(Box::new((
Place::from(ptr_local),
Rvalue::Use(Operand::Copy(box_place.project_deeper(
&crate::elaborate_box_derefs::build_projection(
unique_ty, nonnull_ty, ptr_ty,
),
tcx,
))),
))),
});
statements.push(Statement {
source_info,
kind: StatementKind::Assign(Box::new((
Place::from(ptr_local)
.project_deeper(&[ProjectionElem::Deref], tcx),
Rvalue::Use(Operand::Move(resume_arg.into())),
))),
});
statements.push(Statement {
source_info,
kind: StatementKind::StorageDead(ptr_local),
});
} else {
statements.push(Statement {
source_info,
kind: StatementKind::Assign(Box::new((
point.resume_arg,
Rvalue::Use(Operand::Move(resume_arg.into())),
))),
});
}
}
// Then jump to the real target