Skip to content

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

Description

@H2WO4

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

Activity

  1. added
    C-bugCategory: This is a bug.
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 28, 2022
  2. est31 commented on Jul 28, 2022

    @est31
    Member
  3. c410-f3r commented on Jul 28, 2022

    @c410-f3r
    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.

  4. est31 commented on Jul 28, 2022

    @est31
    Member

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

  5. Urgau commented on Jul 28, 2022

    @Urgau
    Member

    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.

  6. H2WO4 commented on Jul 28, 2022

    @H2WO4
    Author

    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!()
    }
  7. H2WO4 commented on Jul 28, 2022

    @H2WO4
    Author

    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!()
    }
  8. est31 commented on Jul 28, 2022

    @est31
    Member

    Minimized:

    fn lambda<T: Default>() -> T {
        if true && let Some(bar) = transform() {
            bar
        } else {
            T::default()
        }
    }
    
    fn transform<T>() -> Option<T> {
        None
    }
  9. c410-f3r commented on Jul 28, 2022

    @c410-f3r
    Contributor

    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).

  10. c410-f3r commented on Jul 28, 2022

    @c410-f3r
    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.

  11. est31 commented on Jul 28, 2022

    @est31
    Member

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

  12. 10 remaining items

  13. c410-f3r commented on Aug 14, 2022

    @c410-f3r
    Contributor

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

    Hum, I see. Thanks!

  14. c410-f3r commented on Aug 25, 2022

    @c410-f3r
    Contributor

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

  15. c410-f3r commented on Aug 26, 2022

    @c410-f3r
    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?

  16. reopened this on Aug 26, 2022
  17. apiraino commented on Oct 4, 2022

    @apiraino
    Contributor

    WG-prioritization assigning priority (Zulip discussion).

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

  18. added a commit that references this issue on Nov 24, 2022
  19. added a commit that references this issue on Nov 24, 2022
  20. added a commit that references this issue on Jan 6, 2023
    7ee48ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions