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

ICE when returning value bound by if let ... && let ... #99852

Closed
H2WO4 opened this issue Jul 28, 2022 · 24 comments · Fixed by #102394
Closed

ICE when returning value bound by if let ... && let ... #99852

H2WO4 opened this issue Jul 28, 2022 · 24 comments · Fixed by #102394
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@H2WO4
Copy link

H2WO4 commented Jul 28, 2022

Code

fn lambda() -> U {
    let foo: Result<T, Error> = (...);
    let baz: U = (...);

    if let Ok(foo) = foo && let Ok(bar) = transform(foo) {
        bar
    } else {
        baz
    }
}

fn transform(input: T) -> Result<U, OtherError> {...}

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (6dbae3ad1 2022-07-25)
binary: rustc
commit-hash: 6dbae3ad19309bb541d9e76638e6aa4b5449f29a
commit-date: 2022-07-25
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

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:77 ~ rustpsycle[b484]::get_image), const_param_did: None }) (end of phase transition to Optimized) at bb14[0]:
                                use of local _9, which has no storage here
  --> src/lib.rs:30:2
   |
30 |     } else {
   |     ^
   |
   = note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:129:36

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

stack backtrace:
   0:     0x7fcbc12a37d0 - std::backtrace_rs::backtrace::libunwind::trace::h74e6731adcfb8adc
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   1:     0x7fcbc12a37d0 - std::backtrace_rs::backtrace::trace_unsynchronized::h22bd7e644b454809
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fcbc12a37d0 - std::sys_common::backtrace::_print_fmt::hec626af75cdf5d14
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7fcbc12a37d0 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hfe4102e485da4cf8
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7fcbc12fda5c - core::fmt::write::h78ef6088ea150cd5
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/core/src/fmt/mod.rs:1198:17
   5:     0x7fcbc1294eb5 - std::io::Write::write_fmt::hc440e7a0893fa000
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/io/mod.rs:1672:15
   6:     0x7fcbc12a64a1 - std::sys_common::backtrace::_print::h6f08412e91897b34
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7fcbc12a64a1 - std::sys_common::backtrace::print::he5a852bdc192e345
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7fcbc12a64a1 - std::panicking::default_hook::{{closure}}::hf308fab78552e6c7
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/panicking.rs:295:22
   9:     0x7fcbc12a6173 - std::panicking::default_hook::h66b19fed514ed5ed
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/panicking.rs:314:9
  10:     0x7fcbc1b3ff44 - rustc_driver[45495022f976cfcf]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7fcbc12a6c76 - std::panicking::rust_panic_with_hook::h9f418f85f12ababe
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/panicking.rs:702:17
  12:     0x7fcbc2ced881 - std[9611d438937dc38]::panicking::begin_panic::<rustc_errors[65e8676031e39f89]::ExplicitBug>::{closure#0}
  13:     0x7fcbc2ced5e6 - std[9611d438937dc38]::sys_common::backtrace::__rust_end_short_backtrace::<std[9611d438937dc38]::panicking::begin_panic<rustc_errors[65e8676031e39f89]::ExplicitBug>::{closure#0}, !>
  14:     0x7fcbc2ce69d6 - std[9611d438937dc38]::panicking::begin_panic::<rustc_errors[65e8676031e39f89]::ExplicitBug>
  15:     0x7fcbc2ce92f6 - std[9611d438937dc38]::panic::panic_any::<rustc_errors[65e8676031e39f89]::ExplicitBug>
  16:     0x7fcbc45bc513 - <rustc_errors[65e8676031e39f89]::HandlerInner as core[807c2f8b07026ed8]::ops::drop::Drop>::drop
  17:     0x7fcbc3cf0738 - core[807c2f8b07026ed8]::ptr::drop_in_place::<rustc_session[ee333364a5fb762c]::parse::ParseSess>
  18:     0x7fcbc3cf2ce3 - <alloc[65bf4ccb0f73bc84]::rc::Rc<rustc_session[ee333364a5fb762c]::session::Session> as core[807c2f8b07026ed8]::ops::drop::Drop>::drop
  19:     0x7fcbc3cd546d - core[807c2f8b07026ed8]::ptr::drop_in_place::<rustc_interface[21c2a0e765b4b7f4]::interface::Compiler>
  20:     0x7fcbc3cd36a9 - rustc_span[ae34330c9831655e]::with_source_map::<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_interface[21c2a0e765b4b7f4]::interface::create_compiler_and_run<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_driver[45495022f976cfcf]::run_compiler::{closure#1}>::{closure#1}>
  21:     0x7fcbc3cd7cb0 - rustc_interface[21c2a0e765b4b7f4]::interface::create_compiler_and_run::<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_driver[45495022f976cfcf]::run_compiler::{closure#1}>
  22:     0x7fcbc3d03542 - <scoped_tls[315da7db807c9876]::ScopedKey<rustc_span[ae34330c9831655e]::SessionGlobals>>::set::<rustc_interface[21c2a0e765b4b7f4]::interface::run_compiler<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_driver[45495022f976cfcf]::run_compiler::{closure#1}>::{closure#0}, core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>>
  23:     0x7fcbc3cd58bf - std[9611d438937dc38]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[21c2a0e765b4b7f4]::util::run_in_thread_pool_with_globals<rustc_interface[21c2a0e765b4b7f4]::interface::run_compiler<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_driver[45495022f976cfcf]::run_compiler::{closure#1}>::{closure#0}, core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>>::{closure#0}, core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>>
  24:     0x7fcbc3cef9e9 - <<std[9611d438937dc38]::thread::Builder>::spawn_unchecked_<rustc_interface[21c2a0e765b4b7f4]::util::run_in_thread_pool_with_globals<rustc_interface[21c2a0e765b4b7f4]::interface::run_compiler<core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>, rustc_driver[45495022f976cfcf]::run_compiler::{closure#1}>::{closure#0}, core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>>::{closure#0}, core[807c2f8b07026ed8]::result::Result<(), rustc_errors[65e8676031e39f89]::ErrorGuaranteed>>::{closure#1} as core[807c2f8b07026ed8]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  25:     0x7fcbc12b06c3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h269b15882926c1ec
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/alloc/src/boxed.rs:1935:9
  26:     0x7fcbc12b06c3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h58323387bc1c2642
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/alloc/src/boxed.rs:1935:9
  27:     0x7fcbc12b06c3 - std::sys::unix::thread::Thread::new::thread_start::h916fd4de50349453
                               at /rustc/6dbae3ad19309bb541d9e76638e6aa4b5449f29a/library/std/src/sys/unix/thread.rs:108:17
  28:     0x7fcbc106cb43 - start_thread
                               at ./nptl/./nptl/pthread_create.c:442:8
  29:     0x7fcbc10fea00 - clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  30:                0x0 - <unknown>

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.64.0-nightly (6dbae3ad1 2022-07-25) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type dylib -C opt-level=3 -C embed-bitcode=no

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack

@H2WO4 H2WO4 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 Jul 28, 2022
@est31
Copy link
Member

est31 commented Jul 28, 2022

cc @c410-f3r

@c410-f3r
Copy link
Contributor

Code does not compile. Here goes a local attempt that was OK.

use core::convert::Infallible;

fn lambda<U>() -> U
where
    U: Default
{
    let foo: Result<i32, ()> = Ok(1);
    let baz: U = <_>::default();

    if let Ok(foo) = foo && let Ok(bar) = transform(foo) {
        bar
    } else {
        baz
    }
}

fn transform<T, U>(_: T) -> Result<U, Infallible> {
    todo!()
}

IFAICT, compiler/rustc_const_eval/src/transform/validate.rs isn't very related to let_chains.

@est31
Copy link
Member

est31 commented Jul 28, 2022

@H2WO4 would it be possible to upload a self contained example that reproduces the issue?

@Urgau
Copy link
Member

Urgau commented Jul 28, 2022

It seams that the ICE only occurs with opt-level >= 1 not below. Using the example from @c410-f3r I got a very similar ICE as the issue.

@H2WO4
Copy link
Author

H2WO4 commented Jul 28, 2022

This panics for me:

fn lambda<T, U>() -> U
    where T: Default,
          U: Default
{
    let foo: Result<T, ()> = Ok(T::default());
    let baz: U = U::default();

    if let Ok(foo) = foo && let Ok(bar) = transform(foo) {
        bar
    } else {
        baz
    }
}

fn transform<T, U>(input: T) -> Result<U, ()> {
    todo!()
}

But removing the generic U in lambda allows it to compile:

fn lambda<T>() -> u32
    where T: Default
{
    let foo: Result<T, ()> = Ok(T::default());
    let baz: u32 = 0;

    if let Ok(foo) = foo && let Ok(bar) = transform(foo) {
        bar
    } else {
        baz
    }
}

fn transform<T, U>(input: T) -> Result<U, ()> {
    todo!()
}

@H2WO4
Copy link
Author

H2WO4 commented Jul 28, 2022

Additionally, it still panics if we remove the generic T:

fn lambda<U>() -> U
    where U: Default
{
    let foo: Result<i32, ()> = Ok(1);
    let baz: U = U::default();

    if let Ok(foo) = foo && let Ok(bar) = transform(foo) {
        bar
    } else {
        baz
    }
}

fn transform<T, U>(input: T) -> Result<U, ()> {
    todo!()
}

@est31
Copy link
Member

est31 commented Jul 28, 2022

Minimized:

fn lambda<T: Default>() -> T {
    if true && let Some(bar) = transform() {
        bar
    } else {
        T::default()
    }
}

fn transform<T>() -> Option<T> {
    None
}

@c410-f3r
Copy link
Contributor

c410-f3r commented Jul 28, 2022

From my playground tests, all the above snippets only happen in optimized ***** libraries *****.

I also couldn't reproduce locally using a slightly older compiler version (nightly-2022-07-15).

@c410-f3r
Copy link
Contributor

OK, just updated the compiler and the ICE happened. Since I've only worked in the parser these past weeks, it is possible that some recent MIR change caused this current behaviour.

@5225225
Copy link
Contributor

5225225 commented Jul 28, 2022

searched nightlies: from nightly-2022-06-01 to nightly-2022-07-28
regressed nightly: nightly-2022-07-08
searched commit range: 7665c35...1517f5d
regressed commit: 3e51277

bisected with cargo-bisect-rustc v0.6.1

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

cargo bisect-rustc --start 2022-06-01 --preserve --prompt -- build --release 

Another rollup.... But some of these are MIR related.

@est31
Copy link
Member

est31 commented Jul 28, 2022

From that list, #98930 seems to be the most likely cause (edit: note this is just a guess).

@5225225
Copy link
Contributor

5225225 commented Jul 28, 2022

I wonder if this has the same cause as #99866 .... Hmmm..... They're both broken MIR, but it's a different exact error.

@Noratrieb
Copy link
Member

I think this is caused by #96856 as well. My local revert of it didn't ICE on the snippet, but nightly did (both compiled with -O).

@Noratrieb
Copy link
Member

While it seems to be caused by the same PR, they don't appear to be caused by the same underlying issue.

@Noratrieb
Copy link
Member

@rustbot claim

@Noratrieb
Copy link
Member

@rustbot release

@Noratrieb
Copy link
Member

@rustbot release-assignment

@Noratrieb
Copy link
Member

While investigating this, I found out about #100513, which is the underlying cause for this and other let_chains problems. The real fix here is to fix the desugaring.

@c410-f3r
Copy link
Contributor

c410-f3r commented Aug 14, 2022

Well, something (related or not related to #100513) happened in #99014 to trigger this ICE but there are indeed drop variances between let_chains and nested if lets.

cc #100276 (comment)

@Noratrieb
Copy link
Member

That PR just enabled validation that cought the broken mir, it has already been broken before.

@c410-f3r
Copy link
Contributor

That PR just enabled validation that cought the broken mir, it has already been broken before.

Hum, I see. Thanks!

@c410-f3r
Copy link
Contributor

Does not ICE any more after #100571. Test added in #100998.

@H2WO4 H2WO4 closed this as completed Aug 25, 2022
@c410-f3r
Copy link
Contributor

Oooppsss... Sorry, this test still ICE.

Wasn't able to reproduce before but now it is known that the minimal compiler arguments are --crate-type lib --emit=link -C opt-level=1.

@H2WO4 Can you re-open?

@apiraino
Copy link
Contributor

apiraino commented Oct 4, 2022

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-high T-compiler

@rustbot rustbot added the P-high High priority label Oct 4, 2022
@bors bors closed this as completed in c97d02c Oct 5, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Nov 24, 2022
…, r=davidtwco

Add regression test for issue rust-lang#99938

That issue was a dupe of rust-lang#99852, and it got fixed since, but it's always better to have multiple regression tests rather than one.

closes rust-lang#99938
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 24, 2022
…, r=davidtwco

Add regression test for issue rust-lang#99938

That issue was a dupe of rust-lang#99852, and it got fixed since, but it's always better to have multiple regression tests rather than one.

closes rust-lang#99938
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…-obk

Fix unwind drop glue for if-then scopes

cc `@est31`

Fix rust-lang#102317
Fix rust-lang#99852

This PR fixes the drop glue for unwinding from a panic originated in a drop while breaking out for the else block in an `if-then` scope.
MIR validation does not fail for the synchronous versions of the test program, because `StorageDead` statements are skipped over in the unwinding process. It is only becoming a problem when it is inside a generator where `StorageDead` must be kept around.
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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
8 participants