Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression ICE - broken MIR under -C opt-level=s on if let Some(v) = None as Option<...> #88307

Closed
richkadel opened this issue Aug 24, 2021 · 11 comments · Fixed by #88572
Closed
Labels
C-bug Category: This is a bug. P-high High priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@richkadel
Copy link
Contributor

richkadel commented Aug 24, 2021

I tried this code (reduced from a macro expansion in previously working code):

fn main() {
    if let Some(val) = None as Option<String> {
        println!("never reached for val={}", val);
    }
    println!("end of test");
}  

I expected to see this happen:

Prior to a recent change, this compiles and running the program prints "end of test".

Instead, this happened:

rustc -Copt-level=s broken_mir.rs
error: internal compiler error: broken MIR in Item(WithOptConstParam { did: DefId(0:3 ~ broken_mir[b66b]::main), const_param_did: None }) (end of phase Optimization) at bb5[0]:
use of local _2, which has no storage here
 --> broken_mir.rs:4:5
  |
4 |     }
  |     ^
  |

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (2a6fb9a4c 2021-08-16)
binary: rustc
commit-hash: 2a6fb9a4c0e5ca7a81999065943b211c226fe9d8
commit-date: 2021-08-16
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

Note, I also tested with a version of the compiler just prior to the commit-hash above, and the program compiles and runs correctly under -C opt-level=s:

rustc 1.56.0-nightly (2bd17c1d4 2021-08-15)
binary: rustc
commit-hash: 2bd17c1d43bba43412cc2f051323a279d6751e43
commit-date: 2021-08-15
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

So this appears to confirm the error was introduced with the PR Introduce hir::ExprKind::Let - Take 2 #80357

Backtrace

  = note: delayed at compiler/rustc_mir/src/transform/validate.rs:121:36

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1134:13
stack backtrace:
   0:     0x7fe0c362f140 - std::backtrace_rs::backtrace::libunwind::trace::hea80398fdae6c868
                               at /usr/local/google/home/richkadel/rust/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x7fe0c362f140 - std::backtrace_rs::backtrace::trace_unsynchronized::h9f50f49ec5fa6e70
                               at /usr/local/google/home/richkadel/rust/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fe0c362f140 - std::sys_common::backtrace::_print_fmt::hae6bb9b578c9b29f
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7fe0c362f140 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h4976016e0890e206
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x7fe0c36a840a - core::fmt::write::hb4b241262ffb1648
                               at /usr/local/google/home/richkadel/rust/library/core/src/fmt/mod.rs:1117:17
   5:     0x7fe0c361eef5 - std::io::Write::write_fmt::h44e5eb8ff82a1e4a
                               at /usr/local/google/home/richkadel/rust/library/std/src/io/mod.rs:1667:15
   6:     0x7fe0c3632dc7 - std::sys_common::backtrace::_print::hf6914f4f16c92c55
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7fe0c3632dc7 - std::sys_common::backtrace::print::hcc87da1739c3aaca
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7fe0c3632dc7 - std::panicking::default_hook::{{closure}}::he49b902836396e57
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:210:50
   9:     0x7fe0c363285b - std::panicking::default_hook::hae5ed4329a98ede1
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:227:9
  10:     0x7fe0c4bbeac1 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h63fb9126cc2ce127
                               at /usr/local/google/home/richkadel/rust/library/alloc/src/boxed.rs:1650:9
  11:     0x7fe0c4bbeac1 - rustc_driver::DEFAULT_HOOK::{{closure}}::{{closure}}::h9396e385a56f4bed
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_driver/src/lib.rs:1168:13
  12:     0x7fe0c363372b - std::panicking::rust_panic_with_hook::hf07e7a68ff83114b
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:628:17
  13:     0x7fe0c36332b0 - std::panicking::begin_panic_handler::{{closure}}::hf8e7f92619198c2b
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:521:13
  14:     0x7fe0c362f664 - std::sys_common::backtrace::__rust_end_short_backtrace::h4cd128bb5d2e259a
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:141:18
  15:     0x7fe0c3633219 - rust_begin_unwind
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:517:5
  16:     0x7fe0c36331cb - std::panicking::begin_panic_fmt::h9ad69cd7d22cbb05
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:460:5
  17:     0x7fe0c7cc3da7 - rustc_errors::HandlerInner::flush_delayed::he9f0b08e429b4120
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_errors/src/lib.rs:1134:13
  18:     0x7fe0c7cc0056 - <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop::he27b7550640b91da
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_errors/src/lib.rs:457:13
  19:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<rustc_errors::HandlerInner>::h1fddd6a10951ea39
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  20:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<core::cell::UnsafeCell<rustc_errors::HandlerInner>>::h8089e4af87ba1561
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  21:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<core::cell::RefCell<rustc_errors::HandlerInner>>::h959277301dfb91dd
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  22:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<rustc_data_structures::sync::Lock<rustc_errors::HandlerInner>>::h4cf921d64fa7e127
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  23:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<rustc_errors::Handler>::hb0abdabff159574f
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  24:     0x7fe0c4ba08b6 - core::ptr::drop_in_place<rustc_session::parse::ParseSess>::h250de9d6f08eddd1
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  25:     0x7fe0c4badb0a - core::ptr::drop_in_place<rustc_session::session::Session>::h15edd041ec15c7aa
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  26:     0x7fe0c4badb0a - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::hac422ce34a73cf8b
                               at /usr/local/google/home/richkadel/rust/library/alloc/src/rc.rs:1447:17
  27:     0x7fe0c4bd391c - core::ptr::drop_in_place<alloc::rc::Rc<rustc_session::session::Session>>::h333742e4c00bd2a4
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  28:     0x7fe0c4bd391c - core::ptr::drop_in_place<rustc_interface::interface::Compiler>::ha295b459177efaef
                               at /usr/local/google/home/richkadel/rust/library/core/src/ptr/mod.rs:192:1
  29:     0x7fe0c4bd0161 - core::mem::drop::h66d5d49438019dda
                               at /usr/local/google/home/richkadel/rust/library/core/src/mem/mod.rs:898:24
  30:     0x7fe0c4bd0161 - rustc_interface::interface::create_compiler_and_run::{{closure}}::{{closure}}::hb35f7751fe9465ba
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/interface.rs:213:60
  31:     0x7fe0c4bd0161 - rustc_data_structures::profiling::TimingGuard::run::he109aac39e941265
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_data_structures/src/profiling.rs:590:9
  32:     0x7fe0c4bd0161 - rustc_interface::interface::create_compiler_and_run::{{closure}}::h758b9b7a8ec1284d
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/interface.rs:213:9
  33:     0x7fe0c4bd0161 - rustc_span::with_source_map::hda971ba47a0aae9d
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_span/src/lib.rs:911:5
  34:     0x7fe0c4c2829d - rustc_interface::interface::create_compiler_and_run::ha68ef7270ae1e9cb
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/interface.rs:203:5
  35:     0x7fe0c4c1b514 - rustc_interface::interface::run_compiler::{{closure}}::h7cb658bac73132a1
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/interface.rs:225:12
  36:     0x7fe0c4c1b514 - rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}::{{closure}}::he2c991290c9086da
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/util.rs:157:13
  37:     0x7fe0c4c1b514 - scoped_tls::ScopedKey<T>::set::haeb10a9e94d49d73
                               at /usr/local/google/home/richkadel/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:137:9
  38:     0x7fe0c4bd0a9b - rustc_span::create_session_globals_then::h662e3754c17324ec
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_span/src/lib.rs:105:5
  39:     0x7fe0c4bd0a9b - rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}::h1a7bf06b03cda53c
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/util.rs:155:9
  40:     0x7fe0c4bd0a9b - rustc_interface::util::scoped_thread::{{closure}}::hd3ccbf65bc3be109
                               at /usr/local/google/home/richkadel/rust/compiler/rustc_interface/src/util.rs:130:24
  41:     0x7fe0c4bd0a9b - std::sys_common::backtrace::__rust_begin_short_backtrace::h1e0b27d30679266c
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys_common/backtrace.rs:125:18
  42:     0x7fe0c4bc3153 - std::thread::Builder::spawn_unchecked::{{closure}}::{{closure}}::hbb1d7f396bfa357d
                               at /usr/local/google/home/richkadel/rust/library/std/src/thread/mod.rs:481:17
  43:     0x7fe0c4bc3153 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h5f8fe49cb0f0f6ef
                               at /usr/local/google/home/richkadel/rust/library/core/src/panic/unwind_safe.rs:271:9
  44:     0x7fe0c4bc3153 - std::panicking::try::do_call::h60b73e9676461207
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:403:40
  45:     0x7fe0c4bc3153 - std::panicking::try::h1780424951bc2eb7
                               at /usr/local/google/home/richkadel/rust/library/std/src/panicking.rs:367:19
  46:     0x7fe0c4bc3153 - std::panic::catch_unwind::h12ff5482189241be
                               at /usr/local/google/home/richkadel/rust/library/std/src/panic.rs:129:14
  47:     0x7fe0c4bc3153 - std::thread::Builder::spawn_unchecked::{{closure}}::h8c61c171cefb6690
                               at /usr/local/google/home/richkadel/rust/library/std/src/thread/mod.rs:480:30
  48:     0x7fe0c4bc3153 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h100cbb8448bf0f09
                               at /usr/local/google/home/richkadel/rust/library/core/src/ops/function.rs:227:5
  49:     0x7fe0c3640c03 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h111b7c61a75bd788
                               at /usr/local/google/home/richkadel/rust/library/alloc/src/boxed.rs:1636:9
  50:     0x7fe0c3640c03 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha3e1896aed4c42e9
                               at /usr/local/google/home/richkadel/rust/library/alloc/src/boxed.rs:1636:9
  51:     0x7fe0c3640c03 - std::sys::unix::thread::Thread::new::thread_start::ha0bb87288a6a01a3
                               at /usr/local/google/home/richkadel/rust/library/std/src/sys/unix/thread.rs:106:17
  52:     0x7fe0bf85aea7 - start_thread
  53:     0x7fe0c3437def - clone

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.56.0-nightly (2a6fb9a4c 2021-08-16) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=s

query stack during panic:
end of query stack

@richkadel richkadel added the C-bug Category: This is a bug. label Aug 24, 2021
@richkadel
Copy link
Contributor Author

@c410-f3r @matthewjasper - We're encountering this new ICE since early last week, and it looks like it started when #80357 landed. Can you take a look?

@tmandry
Copy link
Member

tmandry commented Aug 24, 2021

Playground, ICE happens in release builds.

@c410-f3r
Copy link
Contributor

c410-f3r commented Aug 24, 2021

Reduced. Wildcard and primitive values don't trigger ICE.

fn main() {
    if let Some(_val) = None as Option<String> {
    }
}

I will do my best to fix this issue but I might not be the fastest

@tmandry tmandry added the regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. label Aug 24, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Aug 24, 2021
@c410-f3r
Copy link
Contributor

Looks like a joined block is dropping state only initialized on the "true/then" side.

@richkadel
Copy link
Contributor Author

FYI, I confirmed the problem was introduced in #80357.

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 26, 2021
@apiraino
Copy link
Contributor

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Aug 26, 2021
@camsteffen
Copy link
Contributor

camsteffen commented Aug 27, 2021

MIR diff of unoptimized to optimized
 fn main() -> () {
     let mut _0: ();                      // return place in scope 0 at src/test/ui/issues/issue-88307.rs:4:11: 4:11
     let mut _1: std::option::Option<std::string::String>; // in scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:47
     let _2: std::string::String;         // in scope 0 at src/test/ui/issues/issue-88307.rs:5:17: 5:21
     let mut _3: bool;                    // in scope 0 at src/test/ui/issues/issue-88307.rs:6:5: 6:6
     let mut _4: bool;                    // in scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     let mut _5: isize;                   // in scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     let mut _6: isize;                   // in scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     scope 1 {
         debug _val => _2;                // in scope 1 at src/test/ui/issues/issue-88307.rs:5:17: 5:21
     }
 
     bb0: {
         _4 = const false;                // scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:47
         _3 = const false;                // scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:47
+        StorageLive(_1);                 // scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:47
         _4 = const true;                 // scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:29
         discriminant(_1) = 0;            // scope 0 at src/test/ui/issues/issue-88307.rs:5:25: 5:29
         switchInt(_3) -> [false: bb1, otherwise: bb4]; // scope 0 at src/test/ui/issues/issue-88307.rs:6:5: 6:6
     }
 
     bb1: {
         _3 = const false;                // scope 0 at src/test/ui/issues/issue-88307.rs:6:5: 6:6
+        StorageDead(_2);                 // scope 0 at src/test/ui/issues/issue-88307.rs:6:5: 6:6
         _5 = discriminant(_1);           // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
         switchInt(move _5) -> [1_isize: bb6, otherwise: bb5]; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 
     bb2 (cleanup): {
         _6 = discriminant(_1);           // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
         switchInt(move _6) -> [1_isize: bb8, otherwise: bb3]; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 
     bb3 (cleanup): {
         resume;                          // scope 0 at src/test/ui/issues/issue-88307.rs:4:1: 7:2
     }
 
     bb4: {
         drop(_2) -> [return: bb1, unwind: bb2]; // scope 0 at src/test/ui/issues/issue-88307.rs:6:5: 6:6
     }
 
     bb5: {
         _4 = const false;                // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
+        StorageDead(_1);                 // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
         return;                          // scope 0 at src/test/ui/issues/issue-88307.rs:7:2: 7:2
     }
 
     bb6: {
         switchInt(_4) -> [false: bb5, otherwise: bb7]; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 
     bb7: {
         drop(((_1 as Some).0: std::string::String)) -> bb5; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 
     bb8 (cleanup): {
         switchInt(_4) -> [false: bb3, otherwise: bb9]; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 
     bb9 (cleanup): {
         drop(((_1 as Some).0: std::string::String)) -> bb3; // scope 0 at src/test/ui/issues/issue-88307.rs:7:1: 7:2
     }
 }

@richkadel
Copy link
Contributor Author

@c410-f3r - Can you offer an update on the fix for this? Thanks.

@c410-f3r
Copy link
Contributor

c410-f3r commented Aug 30, 2021

After several days and several failed attempts, I couldn't solve the issue on my own but Matthew kindly applied to look into the underlying problem. FWICT, he was far more MIR knowledge and Rustc experience so I guess a final outcome probably won't take long.

@flip1995
Copy link
Member

flip1995 commented Sep 1, 2021

@c410-f3r @matthewjasper (I assume you meant this Matthew?) Do you know if you can provide a fix for this in-time or if the revert has to go through? We want to sync Clippy one more time before the beta branching on Friday and this is kind of blocking that (since we don't want to make a revert harder, if it should come to that).

@c410-f3r
Copy link
Contributor

c410-f3r commented Sep 1, 2021

As for myself, I did everything I could.
I guess it is now mostly up to Matthew but if there is anything I can help, then I will surely help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. P-high High priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
7 participants