Skip to content

Commit ad5f912

Browse files
committed
Transmute from NonNull to pointer when elaborating a box deref (MCP807)
1 parent fd98df8 commit ad5f912

12 files changed

+29
-28
lines changed

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ fn build_ptr_tys<'tcx>(
2929
pub(super) fn build_projection<'tcx>(
3030
unique_ty: Ty<'tcx>,
3131
nonnull_ty: Ty<'tcx>,
32-
ptr_ty: Ty<'tcx>,
33-
) -> [PlaceElem<'tcx>; 3] {
34-
[
35-
PlaceElem::Field(FieldIdx::ZERO, unique_ty),
36-
PlaceElem::Field(FieldIdx::ZERO, nonnull_ty),
37-
PlaceElem::Field(FieldIdx::ZERO, ptr_ty),
38-
]
32+
) -> [PlaceElem<'tcx>; 2] {
33+
[PlaceElem::Field(FieldIdx::ZERO, unique_ty), PlaceElem::Field(FieldIdx::ZERO, nonnull_ty)]
3934
}
4035

4136
struct ElaborateBoxDerefVisitor<'a, 'tcx> {
@@ -75,10 +70,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
7570
self.patch.add_assign(
7671
location,
7772
Place::from(ptr_local),
78-
Rvalue::Use(Operand::Copy(
79-
Place::from(place.local)
80-
.project_deeper(&build_projection(unique_ty, nonnull_ty, ptr_ty), tcx),
81-
)),
73+
Rvalue::Cast(
74+
CastKind::Transmute,
75+
Operand::Copy(
76+
Place::from(place.local)
77+
.project_deeper(&build_projection(unique_ty, nonnull_ty), tcx),
78+
),
79+
ptr_ty,
80+
),
8281
);
8382

8483
place.local = ptr_local;
@@ -133,8 +132,10 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
133132
let (unique_ty, nonnull_ty, ptr_ty) =
134133
build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did);
135134

136-
new_projections
137-
.extend_from_slice(&build_projection(unique_ty, nonnull_ty, ptr_ty));
135+
new_projections.extend_from_slice(&build_projection(unique_ty, nonnull_ty));
136+
// While we can't project into `NonNull<_>` in a basic block
137+
// due to MCP#807, this is debug info where it's fine.
138+
new_projections.push(PlaceElem::Field(FieldIdx::ZERO, ptr_ty));
138139
new_projections.push(PlaceElem::Deref);
139140
} else if let Some(new_projections) = new_projections.as_mut() {
140141
// Keep building up our projections list once we've started it.

tests/mir-opt/box_expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
fn main() {
88
// CHECK-LABEL: fn main(
99
// CHECK: [[box:_.*]] = ShallowInitBox(
10-
// CHECK: [[ptr:_.*]] = copy ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S);
10+
// CHECK: [[ptr:_.*]] = copy (([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>) as *const S (Transmute);
1111
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
1212
// CHECK: [[ret]]: {
1313
// CHECK: [[box2:_.*]] = move [[box]];

tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
bb1: {
3333
StorageLive(_7);
3434
_7 = ShallowInitBox(move _6, i32);
35-
_8 = copy (((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
35+
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3636
(*_8) = const 42_i32;
3737
_3 = move _7;
3838
StorageDead(_7);
39-
_9 = copy (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
39+
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
4040
_2 = copy (*_9);
4141
- _1 = Add(move _2, const 0_i32);
4242
- StorageDead(_2);

tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
bb1: {
3333
StorageLive(_7);
3434
_7 = ShallowInitBox(move _6, i32);
35-
_8 = copy (((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
35+
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3636
(*_8) = const 42_i32;
3737
_3 = move _7;
3838
StorageDead(_7);
39-
_9 = copy (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
39+
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
4040
_2 = copy (*_9);
4141
- _1 = Add(move _2, const 0_i32);
4242
- StorageDead(_2);

tests/mir-opt/const_prop/transmute.unreachable_box.GVN.32bit.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
bb0: {
1313
StorageLive(_1);
1414
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
15-
- _2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
15+
- _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
1616
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
17-
+ _2 = const {0x1 as *const Never};
17+
+ _2 = const std::ptr::NonNull::<Never> {{ pointer: {0x1 as *const Never} }} as *const Never (Transmute);
1818
unreachable;
1919
}
2020
}

tests/mir-opt/const_prop/transmute.unreachable_box.GVN.64bit.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
bb0: {
1313
StorageLive(_1);
1414
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
15-
- _2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
15+
- _2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
1616
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
17-
+ _2 = const {0x1 as *const Never};
17+
+ _2 = const std::ptr::NonNull::<Never> {{ pointer: {0x1 as *const Never} }} as *const Never (Transmute);
1818
unreachable;
1919
}
2020
}

tests/mir-opt/dataflow-const-prop/transmute.unreachable_box.DataflowConstProp.32bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
StorageLive(_1);
1414
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
1515
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
16-
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
16+
_2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
1717
unreachable;
1818
}
1919
}

tests/mir-opt/dataflow-const-prop/transmute.unreachable_box.DataflowConstProp.64bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
StorageLive(_1);
1414
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
1515
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global);
16-
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
16+
_2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
1717
unreachable;
1818
}
1919
}

tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818

1919
bb1: {
20-
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
20+
_2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
2121
PlaceMention((*_2));
2222
unreachable;
2323
}

tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818

1919
bb1: {
20-
_2 = copy (((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>).0: *const Never);
20+
_2 = copy ((_1.0: std::ptr::Unique<Never>).0: std::ptr::NonNull<Never>) as *const Never (Transmute);
2121
PlaceMention((*_2));
2222
unreachable;
2323
}

tests/mir-opt/retag.box_to_raw_mut.SimplifyCfg-pre-optimizations.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
99
bb0: {
1010
Retag([fn entry] _1);
1111
_2 = deref_copy (*_1);
12-
_3 = copy (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
12+
_3 = copy ((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
1313
_0 = &raw mut (*_3);
1414
Retag([raw] _0);
1515
return;

tests/mir-opt/retag.box_to_raw_mut.SimplifyCfg-pre-optimizations.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
99
bb0: {
1010
Retag([fn entry] _1);
1111
_2 = deref_copy (*_1);
12-
_3 = copy (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
12+
_3 = copy ((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
1313
_0 = &raw mut (*_3);
1414
Retag([raw] _0);
1515
return;

0 commit comments

Comments
 (0)