Add docs, remove code, change subtyper code

This commit is contained in:
ouz-a 2023-08-28 11:19:19 +03:00
parent 3148e6a993
commit cd7f471931
24 changed files with 106 additions and 363 deletions

View file

@ -7,13 +7,13 @@ use rustc_middle::ty::TyCtxt;
pub struct Subtyper;
pub struct SubTypeCheker<'a, 'tcx> {
pub struct SubTypeChecker<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
patcher: MirPatch<'tcx>,
local_decls: &'a IndexVec<Local, LocalDecl<'tcx>>,
}
impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeCheker<'a, 'tcx> {
impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
@ -25,28 +25,30 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeCheker<'a, 'tcx> {
location: Location,
) {
let place_ty = place.ty(self.local_decls, self.tcx);
let rval_ty = rvalue.ty(self.local_decls, self.tcx);
let mut rval_ty = rvalue.ty(self.local_decls, self.tcx);
if place_ty.ty != rval_ty {
// Not erasing this causes `Free Regions` errors in validator,
// when rval is `ReStatic`.
rval_ty = self.tcx.erase_regions_ty(rval_ty);
let temp = self
.patcher
.new_temp(rval_ty, self.local_decls[place.as_ref().local].source_info.span);
let new_place =
Place::from(temp).project_deeper(&[ProjectionElem::Subtype(place_ty.ty)], self.tcx);
let new_place = Place::from(temp);
self.patcher.add_assign(location, new_place, rvalue.clone());
let new_rval = Rvalue::Use(Operand::Move(new_place));
*rvalue = new_rval;
let subtyped =
new_place.project_deeper(&[ProjectionElem::Subtype(place_ty.ty)], self.tcx);
*rvalue = Rvalue::Use(Operand::Move(subtyped));
}
}
}
pub fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let patch = MirPatch::new(body);
let mut checker = SubTypeCheker { tcx, patcher: patch, local_decls: &body.local_decls };
let mut checker = SubTypeChecker { tcx, patcher: patch, local_decls: &body.local_decls };
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
checker.visit_basic_block_data(bb, data);
}
checker.patcher.apply(body);
}

View file

@ -306,6 +306,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty),
};
value = self.insert(Value::Projection(value, proj));
}