From 2b6815a69e618a96a221d8db24ace87d10ae69f0 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Sat, 23 Nov 2019 12:25:03 -0500 Subject: [PATCH] [const prop] Fix "alloc id without corresponding allocation" ICE Fixes #66345 --- src/librustc_mir/transform/const_prop.rs | 4 +--- .../const_prop/const_prop_fails_gracefully.rs | 6 ++--- src/test/mir-opt/const_prop/ref_deref.rs | 2 +- src/test/mir-opt/const_prop/reify_fn_ptr.rs | 2 +- src/test/mir-opt/const_prop/slice_len.rs | 4 ++-- src/test/ui/consts/issue-66345.rs | 24 +++++++++++++++++++ 6 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/consts/issue-66345.rs diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 4333e5c51427..f7572f366842 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -649,9 +649,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool { - if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 { - return true; - } else if self.tcx.sess.opts.debugging_opts.mir_opt_level == 0 { + if self.tcx.sess.opts.debugging_opts.mir_opt_level == 0 { return false; } diff --git a/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs b/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs index 34d4a534ad9f..3f82b81a47de 100644 --- a/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs +++ b/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs @@ -23,9 +23,9 @@ fn main() { // START rustc.main.ConstProp.after.mir // bb0: { // ... -// _4 = const Scalar(AllocId(1).0x0) : &i32; -// _3 = const Scalar(AllocId(1).0x0) : &i32; -// _2 = const Value(Scalar(AllocId(1).0x0)) : *const i32; +// _4 = const main::FOO; +// _3 = _4; +// _2 = move _3 as *const i32 (Misc); // ... // _1 = move _2 as usize (Misc); // ... diff --git a/src/test/mir-opt/const_prop/ref_deref.rs b/src/test/mir-opt/const_prop/ref_deref.rs index 2d04822c0e78..d45ffdc87753 100644 --- a/src/test/mir-opt/const_prop/ref_deref.rs +++ b/src/test/mir-opt/const_prop/ref_deref.rs @@ -14,7 +14,7 @@ fn main() { // START rustc.main.ConstProp.after.mir // bb0: { // ... -// _2 = const Scalar(AllocId(0).0x0) : &i32; +// _2 = &(promoted[0]: i32); // _1 = const 4i32; // ... // } diff --git a/src/test/mir-opt/const_prop/reify_fn_ptr.rs b/src/test/mir-opt/const_prop/reify_fn_ptr.rs index ad7f195676a6..4d6fe905b0c3 100644 --- a/src/test/mir-opt/const_prop/reify_fn_ptr.rs +++ b/src/test/mir-opt/const_prop/reify_fn_ptr.rs @@ -16,7 +16,7 @@ fn main() { // START rustc.main.ConstProp.after.mir // bb0: { // ... -// _3 = const main; +// _3 = const main as fn() (Pointer(ReifyFnPointer)); // _2 = move _3 as usize (Misc); // ... // _1 = move _2 as *const fn() (Misc); diff --git a/src/test/mir-opt/const_prop/slice_len.rs b/src/test/mir-opt/const_prop/slice_len.rs index 05595ce147c9..d6ff76b34b9b 100644 --- a/src/test/mir-opt/const_prop/slice_len.rs +++ b/src/test/mir-opt/const_prop/slice_len.rs @@ -24,8 +24,8 @@ fn main() { // START rustc.main.ConstProp.after.mir // bb0: { // ... -// _4 = const Scalar(AllocId(0).0x0) : &[u32; 3]; -// _3 = const Scalar(AllocId(0).0x0) : &[u32; 3]; +// _4 = &(promoted[0]: [u32; 3]); +// _3 = _4; // _2 = move _3 as &[u32] (Pointer(Unsize)); // ... // _6 = const 1usize; diff --git a/src/test/ui/consts/issue-66345.rs b/src/test/ui/consts/issue-66345.rs new file mode 100644 index 000000000000..7d0de73007c8 --- /dev/null +++ b/src/test/ui/consts/issue-66345.rs @@ -0,0 +1,24 @@ +// run-pass +// compile-flags: -Z mir-opt-level=3 + +// Checks that the compiler does not ICE when passing references to field of by-value struct +// with -Z mir-opt-level=3 + +fn do_nothing(_: &()) {} + +pub struct Foo { + bar: (), +} + +pub fn by_value_1(foo: Foo) { + do_nothing(&foo.bar); +} + +pub fn by_value_2(foo: Foo) { + do_nothing(&foo.bar); +} + +fn main() { + by_value_1(Foo { bar: () }); + by_value_2::<()>(Foo { bar: () }); +}