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

Broken MIR ICE with let chains #104843

Closed
est31 opened this issue Nov 24, 2022 · 12 comments · Fixed by #119077
Closed

Broken MIR ICE with let chains #104843

est31 opened this issue Nov 24, 2022 · 12 comments · Fixed by #119077
Assignees
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-validate Area: MIR validator (-Z validate-mir) C-bug Category: This is a bug. F-let_chains `#![feature(let_chains)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@est31
Copy link
Member

est31 commented Nov 24, 2022

Originally reported by @flip1995 in #99938 (comment) .

Code

#![feature(let_chains)]

struct F(Box<()>);

impl F {
    fn s(&self) -> Option<&str> {
        None
    }
}

fn cex() -> Option<F> {
    None
}

pub fn main() {
    if false
        && let Some(ce) = cex()
        && let Some(_ce) = ce.s()
    {
    }
}

Error output

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in Item(WithOptConstParam { did: DefId(0:9 ~ a[8fb6]::main), const_param_did: None }) (after phase change to runtime-optimized) at bb3[0]:
                                use of local _1, which has no storage here
  --> /tmp/a.rs:21:1
   |
21 | }
   | ^
   |
   = note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:88:36

error: internal compiler error: broken MIR in Item(WithOptConstParam { did: DefId(0:9 ~ a[8fb6]::main), const_param_did: None }) (after phase change to runtime-optimized) at bb4[0]:
                                use of local _1, which has no storage here
  --> /tmp/a.rs:21:1
   |
21 | }
   | ^
   |
   = note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:88:36

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1594:13

Backtrace

   0:     0x7f4776d010da - std::backtrace_rs::backtrace::libunwind::trace::h06e8f32b98d440be
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f4776d010da - std::backtrace_rs::backtrace::trace_unsynchronized::h8a56795adafc3159
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f4776d010da - std::sys_common::backtrace::_print_fmt::h7ba19acb3601abb5
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7f4776d010da - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha37df576307c1a49
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f4772c6fbae - core::fmt::write::hd3f2e7e755b45b01
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/core/src/fmt/mod.rs:1208:17
   5:     0x7f4776cf53a5 - std::io::Write::write_fmt::hc1388e059aac0a41
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/io/mod.rs:1682:15
   6:     0x7f4776d00ea5 - std::sys_common::backtrace::_print::h429db1082d921ab8
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x7f4776d00ea5 - std::sys_common::backtrace::print::h8d611e0005d3ccb9
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x7f4776d0320f - std::panicking::default_hook::{{closure}}::h07380c607f829a79
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/panicking.rs:267:22
   9:     0x7f4776d02f4b - std::panicking::default_hook::h78998b3f3926502a
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/panicking.rs:286:9
  10:     0x7f4775f5b284 - <rustc_driver[153e38e44d5410f9]::DEFAULT_HOOK::{closure#0}::{closure#0} as core[1d8afb50f4e7a45]::ops::function::FnOnce<(&core[1d8afb50f4e7a45]::panic::panic_info::PanicInfo,)>>::call_once::{shim:vtable#0}
  11:     0x7f4776d03a0d - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hbe7c77b7cbfe1f21
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/alloc/src/boxed.rs:2032:9
  12:     0x7f4776d03a0d - std::panicking::rust_panic_with_hook::hda16cab0ff009a11
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/panicking.rs:692:13
  13:     0x7f4775f93ab1 - std[2b5db76a972e300f]::panicking::begin_panic::<rustc_errors[6e875e03adfb8e66]::ExplicitBug>::{closure#0}
  14:     0x7f4775f923e6 - std[2b5db76a972e300f]::sys_common::backtrace::__rust_end_short_backtrace::<std[2b5db76a972e300f]::panicking::begin_panic<rustc_errors[6e875e03adfb8e66]::ExplicitBug>::{closure#0}, !>
  15:     0x7f4775f8ab76 - std[2b5db76a972e300f]::panicking::begin_panic::<rustc_errors[6e875e03adfb8e66]::ExplicitBug>
  16:     0x7f4775f8e306 - std[2b5db76a972e300f]::panic::panic_any::<rustc_errors[6e875e03adfb8e66]::ExplicitBug>
  17:     0x7f4775465556 - <rustc_errors[6e875e03adfb8e66]::HandlerInner>::flush_delayed::<alloc[eb468f42ab80a7c0]::vec::Vec<rustc_errors[6e875e03adfb8e66]::diagnostic::Diagnostic>, &str>
  18:     0x7f477546485b - <rustc_errors[6e875e03adfb8e66]::HandlerInner as core[1d8afb50f4e7a45]::ops::drop::Drop>::drop
  19:     0x7f477521271e - core[1d8afb50f4e7a45]::ptr::drop_in_place::<rustc_session[729ed7505232e216]::parse::ParseSess>
  20:     0x7f47751cb42f - core[1d8afb50f4e7a45]::ptr::drop_in_place::<rustc_session[729ed7505232e216]::session::Session>
  21:     0x7f47751cb160 - core[1d8afb50f4e7a45]::ptr::drop_in_place::<rustc_interface[d2ac3f3b3bfa4614]::interface::Compiler>
  22:     0x7f47751ca26e - rustc_span[cdf434c68ce75247]::with_source_map::<core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>, rustc_interface[d2ac3f3b3bfa4614]::interface::run_compiler<core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>, rustc_driver[153e38e44d5410f9]::run_compiler::{closure#1}>::{closure#0}::{closure#1}>
  23:     0x7f47751c9ba5 - <scoped_tls[2ec6d95ce5fd8213]::ScopedKey<rustc_span[cdf434c68ce75247]::SessionGlobals>>::set::<rustc_interface[d2ac3f3b3bfa4614]::interface::run_compiler<core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>, rustc_driver[153e38e44d5410f9]::run_compiler::{closure#1}>::{closure#0}, core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>>
  24:     0x7f47751c9192 - std[2b5db76a972e300f]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[d2ac3f3b3bfa4614]::util::run_in_thread_pool_with_globals<rustc_interface[d2ac3f3b3bfa4614]::interface::run_compiler<core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>, rustc_driver[153e38e44d5410f9]::run_compiler::{closure#1}>::{closure#0}, core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>>
  25:     0x7f47751c8ea8 - <<std[2b5db76a972e300f]::thread::Builder>::spawn_unchecked_<rustc_interface[d2ac3f3b3bfa4614]::util::run_in_thread_pool_with_globals<rustc_interface[d2ac3f3b3bfa4614]::interface::run_compiler<core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>, rustc_driver[153e38e44d5410f9]::run_compiler::{closure#1}>::{closure#0}, core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d8afb50f4e7a45]::result::Result<(), rustc_errors[6e875e03adfb8e66]::ErrorGuaranteed>>::{closure#1} as core[1d8afb50f4e7a45]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  26:     0x7f4776d0ac43 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h0c8cfd64ff347c2f
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/alloc/src/boxed.rs:2000:9
  27:     0x7f4776d0ac43 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h5243ef33e0bc55ac
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/alloc/src/boxed.rs:2000:9
  28:     0x7f4776d0ac43 - std::sys::unix::thread::Thread::new::thread_start::h248bc665ee6b9bda
                               at /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library/std/src/sys/unix/thread.rs:108:17
  29:     0x7f4772985ff2 - start_thread
  30:     0x7f4772a08bfc - clone3
  31:                0x0 - <unknown>

The ICE requires -O to be passed to rustc, or --release be passed to cargo, the param to validate MIR is not enough. It's also a regression, on nightly-2022-07-30 it does not reproduce.

cc #99938 and #99852 related issues with similar error messages, but they are fixed and this is reproducible.

@rustbot label E-needs-bisection

@est31 est31 added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 24, 2022
@rustbot rustbot added the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Nov 24, 2022
@est31
Copy link
Member Author

est31 commented Nov 24, 2022

@rustbot label F-let_chains

@rustbot rustbot added the F-let_chains `#![feature(let_chains)]` label Nov 24, 2022
@lukas-code
Copy link
Contributor

searched nightlies: from nightly-2022-07-20 to nightly-2022-11-23
regressed nightly: nightly-2022-10-20
searched commit range: a24a020...4b8f431
regressed commit: d7dd01f

bisected with cargo-bisect-rustc v0.6.4

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --end=2022-11-23 --regress=ice -- rustc -- -Zvalidate-mir -O 

@Nilstrieb
Copy link
Member

Nilstrieb commented Nov 24, 2022

Looks like #103034 didn't fix everything after all..... @nathanwhit
this feature just keeps on giving

@est31
Copy link
Member Author

est31 commented Nov 24, 2022

@rustbot label -E-needs-bisection

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Nov 24, 2022
@flip1995
Copy link
Member

Thanks for opening an issue for this! I put that on my todo list, but my todo list is just way to long...

@dingxiangfei2009
Copy link
Contributor

@rustbot claim

@dingxiangfei2009
Copy link
Contributor

I have a feeling that the if-then scope tracking could be relevant to this issue. Zooming onto that.

@dingxiangfei2009
Copy link
Contributor

dingxiangfei2009 commented Jan 15, 2023

Here is a summary of the cause. The scrutinees of the let expressions has a temporary lifetime of the whole if expression, enclosing the IfThen scope as well. The consequence is that when the condition test fails and the control continues to the block built by break_for_else, it is breaking out of the scope of IfThen but the scrutinees are still considered living, even though some of them are still dead.

I am going to try moving their temporary lifetimes to IfThens and see it solves this issue or breaks anything else.

The following example passes -Z validate-mir checks with -O optimization level, but inspection of the dumped MIR shows that the "temporary" from calling ce.s() is dropped even though its storage _4 was never live.

The issue that the MIR validation does not catch this shall be addressed separately and I am very intrigued.

#![feature(let_chains)]

struct F(Box<()>);
struct G;
impl Drop for G {
    fn drop(&mut self) {}
}

impl F {
    fn s(&self) -> Option<G> {
        None
    }
}

fn cex() -> Option<F> {
    None
}

pub fn main() {
    if let Some(ce) = cex() && let Some(ce2) = ce.s()
    {
    }
}

@est31
Copy link
Member Author

est31 commented Jan 15, 2023

Note that regarding drop order, for now, until #103108 is addressed, we should drop all the let temporaries after the else has terminated. This is for consistency with if let. See #103248 and especially the message I linked at the end that closed the issue. Not sure how one can represent that with scopes though.

@BlueskyFR
Copy link

This is still happening on the nightly release, @dingxiangfei2009 did you came up with something? This seems to be blocking the let-chaining features from stabilizing

@est31
Copy link
Member Author

est31 commented Sep 29, 2023

There is #107251 which would solve this. I haven't had the time to push it forward, but the next logical step would be to file PRs to the repos broken by the PR, not suggesting that this will break eventually, as the only time when this is 100% sure is when this PR is merged, but that there is the chance it might break. See the thread of #107251.

The other blocker is formatting support, which depends on the style team.

Edit: feel free to file the PRs if you want.

@dingxiangfei2009
Copy link
Contributor

I will update the draft that @est31 mentioned.

@tmiasko tmiasko added A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-validate Area: MIR validator (-Z validate-mir) labels Dec 9, 2023
@bors bors closed this as completed in 7dd0955 Dec 23, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 23, 2023
Rollup merge of rust-lang#119077 - tmiasko:lint, r=cjgillot

Separate MIR lints from validation

Add a MIR lint pass, enabled with -Zlint-mir, which identifies undefined or
likely erroneous behaviour.

The initial implementation mostly migrates existing checks of this nature from
MIR validator, where they did not belong (those checks have false positives and
there is nothing inherently invalid about MIR with undefined behaviour).

Fixes rust-lang#104736
Fixes rust-lang#104843
Fixes rust-lang#116079
Fixes rust-lang#116736
Fixes rust-lang#118990
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-validate Area: MIR validator (-Z validate-mir) C-bug Category: This is a bug. F-let_chains `#![feature(let_chains)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants