Skip to content

Commit 442b9a9

Browse files
Validation
1 parent b63341e commit 442b9a9

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

compiler/rustc_const_eval/src/interpret/step.rs

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
279279
}
280280

281281
WrapUnsafeBinder(ref op, _ty) => {
282+
// Constructing an unsafe binder acts like a transmute
283+
// since the operand's layout does not change.
282284
let op = self.eval_operand(op, None)?;
283285
self.copy_op_allow_transmute(&op, &dest)?;
284286
}

compiler/rustc_mir_transform/src/validate.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
807807
)
808808
}
809809
}
810+
ProjectionElem::UnwrapUnsafeBinder(unwrapped_ty) => {
811+
let binder_ty = place_ref.ty(&self.body.local_decls, self.tcx);
812+
let ty::UnsafeBinder(binder_ty) = *binder_ty.ty.kind() else {
813+
self.fail(
814+
location,
815+
format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
816+
);
817+
return;
818+
};
819+
let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
820+
if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
821+
self.fail(
822+
location,
823+
format!(
824+
"Cannot unwrap unsafe binder {binder_ty:?} into type {unwrapped_ty:?}"
825+
),
826+
);
827+
}
828+
}
810829
_ => {}
811830
}
812831
self.super_projection_elem(place_ref, elem, context, location);
@@ -1361,8 +1380,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13611380
| Rvalue::ThreadLocalRef(_)
13621381
| Rvalue::RawPtr(_, _)
13631382
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbChecks, _)
1364-
| Rvalue::Discriminant(_)
1365-
| Rvalue::WrapUnsafeBinder(..) => {}
1383+
| Rvalue::Discriminant(_) => {}
1384+
1385+
Rvalue::WrapUnsafeBinder(op, ty) => {
1386+
let unwrapped_ty = op.ty(self.body, self.tcx);
1387+
let ty::UnsafeBinder(binder_ty) = *ty.kind() else {
1388+
self.fail(
1389+
location,
1390+
format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
1391+
);
1392+
return;
1393+
};
1394+
let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
1395+
if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
1396+
self.fail(
1397+
location,
1398+
format!("Cannot wrap {unwrapped_ty:?} into unsafe binder {binder_ty:?}"),
1399+
);
1400+
}
1401+
}
13661402
}
13671403
self.super_rvalue(rvalue, location);
13681404
}

0 commit comments

Comments
 (0)