Skip to content

Commit ffc90c3

Browse files
committed
Mark simplify_aggregate_to_copy mir-opt as unsound
1 parent 097cb99 commit ffc90c3

10 files changed

+59
-33
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10791079
}
10801080
}
10811081

1082-
if let AggregateTy::Def(_, _) = ty
1082+
// unsound: https://github.com/rust-lang/rust/issues/132353
1083+
if tcx.sess.opts.unstable_opts.unsound_mir_opts
1084+
&& let AggregateTy::Def(_, _) = ty
10831085
&& let Some(value) =
10841086
self.simplify_aggregate_to_copy(rvalue, location, &fields, variant_index)
10851087
{

tests/codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ revisions: DEBUGINFO NODEBUGINFO
2+
//@ compile-flags: -Zunsound-mir-opts
3+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
24
//@ compile-flags: -O -Cno-prepopulate-passes
35
//@ [DEBUGINFO] compile-flags: -Cdebuginfo=full
46

tests/mir-opt/gvn_clone.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline
35

tests/mir-opt/gvn_clone.{impl#0}-clone.GVN.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` before GVN
2-
+ // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` after GVN
1+
- // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` before GVN
2+
+ // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` after GVN
33

4-
fn <impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone(_1: &AllCopy) -> AllCopy {
4+
fn <impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone(_1: &AllCopy) -> AllCopy {
55
debug self => _1;
66
let mut _0: AllCopy;
77
let mut _2: i32;

tests/mir-opt/gvn_copy_aggregate.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353.
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Cpanic=abort
35

tests/mir-opt/pre-codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ compile-flags: -Cdebuginfo=full
24

35
// Check if we have transformed the nested clone to the copy in the complete pipeline.

tests/mir-opt/pre-codegen/no_inlined_clone.{impl#0}-clone.PreCodegen.after.mir

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
fn <impl at $DIR/no_inlined_clone.rs:9:10: 9:15>::clone(_1: &Foo) -> Foo {
44
debug self => _1;
55
let mut _0: Foo;
6+
let mut _2: i32;
67

78
bb0: {
8-
_0 = copy (*_1);
9+
StorageLive(_2);
10+
_2 = copy ((*_1).0: i32);
11+
_0 = Foo { a: move _2 };
12+
StorageDead(_2);
913
return;
1014
}
1115
}

tests/mir-opt/pre-codegen/try_identity.old.PreCodegen.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
1919
}
2020

2121
bb1: {
22-
_3 = copy ((_1 as Ok).0: T);
23-
_0 = copy _1;
22+
_3 = move ((_1 as Ok).0: T);
23+
_0 = Result::<T, E>::Ok(copy _3);
2424
goto -> bb3;
2525
}
2626

2727
bb2: {
28-
_4 = copy ((_1 as Err).0: E);
29-
_0 = copy _1;
28+
_4 = move ((_1 as Err).0: E);
29+
_0 = Result::<T, E>::Err(copy _4);
3030
goto -> bb3;
3131
}
3232

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
77
debug self => _1;
88
scope 2 (inlined Vec::<u8>::as_slice) {
99
debug self => _1;
10-
let mut _6: usize;
10+
let mut _7: usize;
1111
scope 3 (inlined Vec::<u8>::as_ptr) {
1212
debug self => _1;
1313
let mut _2: &alloc::raw_vec::RawVec<u8>;
@@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
1616
let mut _3: &alloc::raw_vec::RawVecInner;
1717
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
1818
debug self => _3;
19+
let mut _6: std::ptr::NonNull<u8>;
1920
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
2021
debug self => _3;
2122
let mut _4: std::ptr::NonNull<u8>;
@@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3132
}
3233
}
3334
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
34-
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
35+
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
3536
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
3637
}
3738
}
3839
scope 11 (inlined NonNull::<u8>::as_ptr) {
39-
debug self => _4;
40+
debug self => _6;
4041
}
4142
}
4243
}
4344
}
4445
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
4546
debug data => _5;
46-
debug len => _6;
47-
let _7: *const [u8];
47+
debug len => _7;
48+
let _8: *const [u8];
4849
scope 13 (inlined core::ub_checks::check_language_ub) {
4950
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
5051
}
@@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
5556
}
5657
scope 17 (inlined slice_from_raw_parts::<u8>) {
5758
debug data => _5;
58-
debug len => _6;
59+
debug len => _7;
5960
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
6061
debug data_pointer => _5;
61-
debug metadata => _6;
62+
debug metadata => _7;
6263
}
6364
}
6465
}
@@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
7071
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
7172
StorageLive(_3);
7273
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
74+
StorageLive(_6);
75+
StorageLive(_4);
7376
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
7477
_5 = copy (_4.0: *const u8);
78+
_6 = NonNull::<u8> { pointer: copy _5 };
79+
StorageDead(_4);
80+
StorageDead(_6);
7581
StorageDead(_3);
7682
StorageDead(_2);
77-
StorageLive(_6);
78-
_6 = copy ((*_1).1: usize);
7983
StorageLive(_7);
80-
_7 = *const [u8] from (copy _5, copy _6);
81-
_0 = &(*_7);
84+
_7 = copy ((*_1).1: usize);
85+
StorageLive(_8);
86+
_8 = *const [u8] from (copy _5, copy _7);
87+
_0 = &(*_8);
88+
StorageDead(_8);
8289
StorageDead(_7);
83-
StorageDead(_6);
8490
return;
8591
}
8692
}

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
77
debug self => _1;
88
scope 2 (inlined Vec::<u8>::as_slice) {
99
debug self => _1;
10-
let mut _6: usize;
10+
let mut _7: usize;
1111
scope 3 (inlined Vec::<u8>::as_ptr) {
1212
debug self => _1;
1313
let mut _2: &alloc::raw_vec::RawVec<u8>;
@@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
1616
let mut _3: &alloc::raw_vec::RawVecInner;
1717
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
1818
debug self => _3;
19+
let mut _6: std::ptr::NonNull<u8>;
1920
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
2021
debug self => _3;
2122
let mut _4: std::ptr::NonNull<u8>;
@@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3132
}
3233
}
3334
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
34-
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
35+
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
3536
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
3637
}
3738
}
3839
scope 11 (inlined NonNull::<u8>::as_ptr) {
39-
debug self => _4;
40+
debug self => _6;
4041
}
4142
}
4243
}
4344
}
4445
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
4546
debug data => _5;
46-
debug len => _6;
47-
let _7: *const [u8];
47+
debug len => _7;
48+
let _8: *const [u8];
4849
scope 13 (inlined core::ub_checks::check_language_ub) {
4950
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
5051
}
@@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
5556
}
5657
scope 17 (inlined slice_from_raw_parts::<u8>) {
5758
debug data => _5;
58-
debug len => _6;
59+
debug len => _7;
5960
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
6061
debug data_pointer => _5;
61-
debug metadata => _6;
62+
debug metadata => _7;
6263
}
6364
}
6465
}
@@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
7071
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
7172
StorageLive(_3);
7273
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
74+
StorageLive(_6);
75+
StorageLive(_4);
7376
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
7477
_5 = copy (_4.0: *const u8);
78+
_6 = NonNull::<u8> { pointer: copy _5 };
79+
StorageDead(_4);
80+
StorageDead(_6);
7581
StorageDead(_3);
7682
StorageDead(_2);
77-
StorageLive(_6);
78-
_6 = copy ((*_1).1: usize);
7983
StorageLive(_7);
80-
_7 = *const [u8] from (copy _5, copy _6);
81-
_0 = &(*_7);
84+
_7 = copy ((*_1).1: usize);
85+
StorageLive(_8);
86+
_8 = *const [u8] from (copy _5, copy _7);
87+
_0 = &(*_8);
88+
StorageDead(_8);
8289
StorageDead(_7);
83-
StorageDead(_6);
8490
return;
8591
}
8692
}

0 commit comments

Comments
 (0)