Transmute from NonNull to pointer when elaborating a box deref (MCP807)

This commit is contained in:
Scott McMurray 2024-11-15 00:35:46 -08:00
parent fd98df8f14
commit ad5f912d96
12 changed files with 29 additions and 28 deletions

View file

@ -29,13 +29,8 @@ fn build_ptr_tys<'tcx>(
pub(super) fn build_projection<'tcx>( pub(super) fn build_projection<'tcx>(
unique_ty: Ty<'tcx>, unique_ty: Ty<'tcx>,
nonnull_ty: Ty<'tcx>, nonnull_ty: Ty<'tcx>,
ptr_ty: Ty<'tcx>, ) -> [PlaceElem<'tcx>; 2] {
) -> [PlaceElem<'tcx>; 3] { [PlaceElem::Field(FieldIdx::ZERO, unique_ty), PlaceElem::Field(FieldIdx::ZERO, nonnull_ty)]
[
PlaceElem::Field(FieldIdx::ZERO, unique_ty),
PlaceElem::Field(FieldIdx::ZERO, nonnull_ty),
PlaceElem::Field(FieldIdx::ZERO, ptr_ty),
]
} }
struct ElaborateBoxDerefVisitor<'a, 'tcx> { struct ElaborateBoxDerefVisitor<'a, 'tcx> {
@ -75,10 +70,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
self.patch.add_assign( self.patch.add_assign(
location, location,
Place::from(ptr_local), Place::from(ptr_local),
Rvalue::Use(Operand::Copy( Rvalue::Cast(
Place::from(place.local) CastKind::Transmute,
.project_deeper(&build_projection(unique_ty, nonnull_ty, ptr_ty), tcx), Operand::Copy(
)), Place::from(place.local)
.project_deeper(&build_projection(unique_ty, nonnull_ty), tcx),
),
ptr_ty,
),
); );
place.local = ptr_local; place.local = ptr_local;
@ -133,8 +132,10 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
let (unique_ty, nonnull_ty, ptr_ty) = let (unique_ty, nonnull_ty, ptr_ty) =
build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did); build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did);
new_projections new_projections.extend_from_slice(&build_projection(unique_ty, nonnull_ty));
.extend_from_slice(&build_projection(unique_ty, nonnull_ty, ptr_ty)); // While we can't project into `NonNull<_>` in a basic block
// due to MCP#807, this is debug info where it's fine.
new_projections.push(PlaceElem::Field(FieldIdx::ZERO, ptr_ty));
new_projections.push(PlaceElem::Deref); new_projections.push(PlaceElem::Deref);
} else if let Some(new_projections) = new_projections.as_mut() { } else if let Some(new_projections) = new_projections.as_mut() {
// Keep building up our projections list once we've started it. // Keep building up our projections list once we've started it.

View file

@ -7,7 +7,7 @@
fn main() { fn main() {
// CHECK-LABEL: fn main( // CHECK-LABEL: fn main(
// CHECK: [[box:_.*]] = ShallowInitBox( // CHECK: [[box:_.*]] = ShallowInitBox(
// CHECK: [[ptr:_.*]] = copy ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S); // CHECK: [[ptr:_.*]] = copy (([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>) as *const S (Transmute);
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]]; // CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
// CHECK: [[ret]]: { // CHECK: [[ret]]: {
// CHECK: [[box2:_.*]] = move [[box]]; // CHECK: [[box2:_.*]] = move [[box]];

View file

@ -32,11 +32,11 @@
bb1: { bb1: {
StorageLive(_7); StorageLive(_7);
_7 = ShallowInitBox(move _6, i32); _7 = ShallowInitBox(move _6, i32);
_8 = copy (((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
(*_8) = const 42_i32; (*_8) = const 42_i32;
_3 = move _7; _3 = move _7;
StorageDead(_7); StorageDead(_7);
_9 = copy (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_2 = copy (*_9); _2 = copy (*_9);
- _1 = Add(move _2, const 0_i32); - _1 = Add(move _2, const 0_i32);
- StorageDead(_2); - StorageDead(_2);

View file

@ -32,11 +32,11 @@
bb1: { bb1: {
StorageLive(_7); StorageLive(_7);
_7 = ShallowInitBox(move _6, i32); _7 = ShallowInitBox(move _6, i32);
_8 = copy (((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
(*_8) = const 42_i32; (*_8) = const 42_i32;
_3 = move _7; _3 = move _7;
StorageDead(_7); StorageDead(_7);
_9 = copy (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_2 = copy (*_9); _2 = copy (*_9);
- _1 = Add(move _2, const 0_i32); - _1 = Add(move _2, const 0_i32);
- StorageDead(_2); - StorageDead(_2);

View file

@ -12,9 +12,9 @@
bb0: { bb0: {
StorageLive(_1); StorageLive(_1);
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); - _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
- _2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); - _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); + _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
+ _2 = const {0x1 as *const Never}; + _2 = const std::ptr::NonNull::<Never> {{ pointer: {0x1 as *const Never} }} as *const Never (Transmute);
unreachable; unreachable;
} }
} }

View file

@ -12,9 +12,9 @@
bb0: { bb0: {
StorageLive(_1); StorageLive(_1);
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); - _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
- _2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); - _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); + _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
+ _2 = const {0x1 as *const Never}; + _2 = const std::ptr::NonNull::<Never> {{ pointer: {0x1 as *const Never} }} as *const Never (Transmute);
unreachable; unreachable;
} }
} }

View file

@ -13,7 +13,7 @@
StorageLive(_1); StorageLive(_1);
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); - _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); + _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
unreachable; unreachable;
} }
} }

View file

@ -13,7 +13,7 @@
StorageLive(_1); StorageLive(_1);
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); - _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); + _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
unreachable; unreachable;
} }
} }

View file

@ -17,7 +17,7 @@
} }
bb1: { bb1: {
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
PlaceMention((*_2)); PlaceMention((*_2));
unreachable; unreachable;
} }

View file

@ -17,7 +17,7 @@
} }
bb1: { bb1: {
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never); _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
PlaceMention((*_2)); PlaceMention((*_2));
unreachable; unreachable;
} }

View file

@ -9,7 +9,7 @@ fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
bb0: { bb0: {
Retag([fn entry] _1); Retag([fn entry] _1);
_2 = deref_copy (*_1); _2 = deref_copy (*_1);
_3 = copy (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _3 = copy ((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_0 = &raw mut (*_3); _0 = &raw mut (*_3);
Retag([raw] _0); Retag([raw] _0);
return; return;

View file

@ -9,7 +9,7 @@ fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
bb0: { bb0: {
Retag([fn entry] _1); Retag([fn entry] _1);
_2 = deref_copy (*_1); _2 = deref_copy (*_1);
_3 = copy (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); _3 = copy ((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_0 = &raw mut (*_3); _0 = &raw mut (*_3);
Retag([raw] _0); Retag([raw] _0);
return; return;