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 pattern matching an enum on a let binding within a closure, with --edition 2021 #95271

Closed
saiintbrisson opened this issue Mar 24, 2022 · 4 comments · Fixed by #97325
Closed
Labels
C-bug Category: This is a bug. 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

@saiintbrisson
Copy link
Contributor

saiintbrisson commented Mar 24, 2022

I was messing around with the async_trait crate and an ICE was raised when I tried to pattern match an enum in the function parameter. I narrowed it down to trying to pattern match a variable that exists outside the scope from within a closure.

Code

enum Foo {
    Foo(i32),
}

fn bar(foo: Foo) {
    || {
        // `let foo = foo;` makes the ICE disappear
        let Foo::Foo(baz) = foo;
    };
}

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3465e1787d6792e07772f31154877280

Meta

Present in all three channels, beta, nightly and stable. Only present on 2021 edition.

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0

Error output

error: internal compiler error: broken MIR in DefId(0:8 ~ playground[20f3]::bar::{closure#0}) (((*((*_1).0: &i32)).0: i32)): can't project out of PlaceTy { ty: i32, variant_index: None }
 --> src/lib.rs:7:22
  |
7 |         let Foo::Foo(baz) = foo;
  |                      ^^^
  |
  = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:319:27

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:794:20

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/compiler/rustc_middle/src/ty/relate.rs:390:59

error: internal compiler error: broken MIR in DefId(0:7 ~ playground[20f3]::bar) ((_1.0: i32)): can't project out of PlaceTy { ty: Foo, variant_index: None }
 --> src/lib.rs:6:5
  |
6 | /     || {
7 | |         let Foo::Foo(baz) = foo;
8 | |     };
  | |_____^
  |
  = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:319:27
Backtrace

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1188:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14
   2: core::panicking::panic_display::<&str>
   3: <rustc_errors::HandlerInner>::flush_delayed
   4: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
   5: core::ptr::drop_in_place::<rustc_session::parse::ParseSess>
   6: <alloc::rc::Rc<rustc_session::session::Session> as core::ops::drop::Drop>::drop
   7: core::ptr::drop_in_place::<rustc_interface::interface::Compiler>
   8: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
   9: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>
  10: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@saiintbrisson saiintbrisson 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 Mar 24, 2022
@saiintbrisson saiintbrisson changed the title ICE when pattern matching a enum within a closure ICE when pattern matching an enum within a closure Mar 24, 2022
@saiintbrisson
Copy link
Contributor Author

I'm willing to work on it if it turns out to be a simple fix, hints are appreciated!

@matthiaskrgr
Copy link
Member

Does this still crash on nightly? I was not able to repro with rustc 1.61.0-nightly (9f4dc0b4d 2022-03-23)

@saiintbrisson
Copy link
Contributor Author

saiintbrisson commented Mar 24, 2022

It does here, same code as above. But the backtrace has changed.

rustc 1.61.0-nightly (9f4dc0b4d 2022-03-23)
binary: rustc
commit-hash: 9f4dc0b4db892271cd0dada6e072775b5b5d6b1e
commit-date: 2022-03-23
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0
Backtrace
warning: Error finalizing incremental compilation session directory `/home/luiz/code/tcp-server/target/debug/incremental/server-15un5yx1wtpo7/s-g87sgl5zk4-16n2tdn-working`: No such file or directory (os error 2)

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

error: internal compiler error: broken MIR in DefId(0:186 ~ server[4c72]::streams::client::bar::{closure#0}) (((*((*_1).0: &i32)).0: i32)): can't project out of PlaceTy { ty: i32, variant_index: None }
  --> server/src/streams/client.rs:72:22
   |
72 |         let Foo::Foo(baz) = foo;
   |                      ^^^
   |
   = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:870:31

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:805:20

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/compiler/rustc_middle/src/ty/relate.rs:390:59

error: internal compiler error: broken MIR in DefId(0:185 ~ server[4c72]::streams::client::bar) ((_1.0: i32)): can't project out of PlaceTy { ty: streams::client::Foo, variant_index: None }
  --> server/src/streams/client.rs:70:5
   |
70 | /     || {
71 | |         // `let foo = foo;` makes the ICE disappear
72 | |         let Foo::Foo(baz) = foo;
73 | |     };
   | |_____^
   |
   = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:870:31

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1291:13
stack backtrace:
   0:     0x7fc7f49d380d - std::backtrace_rs::backtrace::libunwind::trace::he1bc69317b314d2e
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fc7f49d380d - std::backtrace_rs::backtrace::trace_unsynchronized::h0be7bfa7bff7f915
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fc7f49d380d - std::sys_common::backtrace::_print_fmt::h3daff2ba590cbc63
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7fc7f49d380d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h82b295b0737f92f8
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7fc7f4a2d80c - core::fmt::write::hc116e04c8b781f0a
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/core/src/fmt/mod.rs:1190:17
   5:     0x7fc7f49c4e01 - std::io::Write::write_fmt::hdf2c5cf46a74c282
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/io/mod.rs:1655:15
   6:     0x7fc7f49d68f5 - std::sys_common::backtrace::_print::ha38935cfbe56cf33
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7fc7f49d68f5 - std::sys_common::backtrace::print::h5a555af4c956e490
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7fc7f49d68f5 - std::panicking::default_hook::{{closure}}::h7210e8b2ea136c6a
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/panicking.rs:295:22
   9:     0x7fc7f49d65a9 - std::panicking::default_hook::he0b6748cad6a9b81
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/panicking.rs:314:9
  10:     0x7fc7f51722b1 - rustc_driver[15b9156f6b24bb3]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7fc7e508a263 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h005d1be707c1bc91
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/alloc/src/boxed.rs:1875:9
  12:     0x7fc7e50909fc - proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter::{{closure}}::{{closure}}::h684827b3cee6988a
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/proc_macro/src/bridge/client.rs:319:21
  13:     0x7fc7e50ab450 - std::panicking::update_hook::{{closure}}::hff6f21b944627197
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/panicking.rs:258:41
  14:     0x7fc7f49d7040 - std::panicking::rust_panic_with_hook::h7d90789c41819678
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/panicking.rs:702:17
  15:     0x7fc7f6213c81 - std[7af56082c5a9adab]::panicking::begin_panic::<rustc_errors[afdb89a768619fcf]::ExplicitBug>::{closure#0}
  16:     0x7fc7f6212cc6 - std[7af56082c5a9adab]::sys_common::backtrace::__rust_end_short_backtrace::<std[7af56082c5a9adab]::panicking::begin_panic<rustc_errors[afdb89a768619fcf]::ExplicitBug>::{closure#0}, !>
  17:     0x7fc7f6218d4f - std[7af56082c5a9adab]::panicking::begin_panic::<rustc_errors[afdb89a768619fcf]::ExplicitBug>
  18:     0x7fc7f6225386 - std[7af56082c5a9adab]::panic::panic_any::<rustc_errors[afdb89a768619fcf]::ExplicitBug>
  19:     0x7fc7f78b7e6c - <rustc_errors[afdb89a768619fcf]::HandlerInner as core[ec6d764e948e0586]::ops::drop::Drop>::drop
  20:     0x7fc7f6ff5c98 - core[ec6d764e948e0586]::ptr::drop_in_place::<rustc_session[6f79c38a04c315ff]::parse::ParseSess>
  21:     0x7fc7f6ff841a - <alloc[c291d8ee38d8ad41]::rc::Rc<rustc_session[6f79c38a04c315ff]::session::Session> as core[ec6d764e948e0586]::ops::drop::Drop>::drop
  22:     0x7fc7f6fe117d - core[ec6d764e948e0586]::ptr::drop_in_place::<rustc_interface[f459bdc7c39711c]::interface::Compiler>
  23:     0x7fc7f6fe0734 - rustc_span[37126335ebbd5d87]::with_source_map::<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_interface[f459bdc7c39711c]::interface::create_compiler_and_run<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_driver[15b9156f6b24bb3]::run_compiler::{closure#1}>::{closure#1}>
  24:     0x7fc7f6fce184 - rustc_interface[f459bdc7c39711c]::interface::create_compiler_and_run::<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_driver[15b9156f6b24bb3]::run_compiler::{closure#1}>
  25:     0x7fc7f6fcaf02 - <scoped_tls[838f77cdbb45d46c]::ScopedKey<rustc_span[37126335ebbd5d87]::SessionGlobals>>::set::<rustc_interface[f459bdc7c39711c]::interface::run_compiler<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_driver[15b9156f6b24bb3]::run_compiler::{closure#1}>::{closure#0}, core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>>
  26:     0x7fc7f6fc925f - std[7af56082c5a9adab]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f459bdc7c39711c]::util::run_in_thread_pool_with_globals<rustc_interface[f459bdc7c39711c]::interface::run_compiler<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_driver[15b9156f6b24bb3]::run_compiler::{closure#1}>::{closure#0}, core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>>::{closure#0}, core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>>
  27:     0x7fc7f6fe14f2 - <<std[7af56082c5a9adab]::thread::Builder>::spawn_unchecked_<rustc_interface[f459bdc7c39711c]::util::run_in_thread_pool_with_globals<rustc_interface[f459bdc7c39711c]::interface::run_compiler<core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>, rustc_driver[15b9156f6b24bb3]::run_compiler::{closure#1}>::{closure#0}, core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>>::{closure#0}, core[ec6d764e948e0586]::result::Result<(), rustc_errors[afdb89a768619fcf]::ErrorGuaranteed>>::{closure#1} as core[ec6d764e948e0586]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  28:     0x7fc7f49e1233 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h547c45aa46469d29
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/alloc/src/boxed.rs:1861:9
  29:     0x7fc7f49e1233 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he0a17443e7e0f241
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/alloc/src/boxed.rs:1861:9
  30:     0x7fc7f49e1233 - std::sys::unix::thread::Thread::new::thread_start::hd0dd84768f409ab9
                               at /rustc/9f4dc0b4db892271cd0dada6e072775b5b5d6b1e/library/std/src/sys/unix/thread.rs:108:17
  31:     0x7fc7f477a947 - start_thread
                               at ./nptl/./nptl/pthread_create.c:435:8
  32:     0x7fc7f480aa44 - __GI___clone
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:100
  33:                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.61.0-nightly (9f4dc0b4d 2022-03-23) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental

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

query stack during panic:
end of query stack
warning: `server` (bin "server") generated 5 warnings
error: could not compile `server`; 5 warnings emitted

@matthiaskrgr
Copy link
Member

Ah I missed the --edition 2021 🙃 👍

@matthiaskrgr matthiaskrgr changed the title ICE when pattern matching an enum within a closure ICE when pattern matching an enum within a closure, with --edition 2021 Mar 24, 2022
@saiintbrisson saiintbrisson changed the title ICE when pattern matching an enum within a closure, with --edition 2021 ICE when pattern matching an enum on a let binding within a closure, with --edition 2021 Mar 24, 2022
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Mar 26, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 7, 2022
…-aman

Fix precise field capture of univariant enums

When constructing a MIR from a THIR field expression, introduce an
additional downcast projection before accessing a field of an enum.

When rebasing a place builder on top of a captured place, account for
the fact that a single HIR enum field projection corresponds to two MIR
projection elements: a downcast element and a field element.

Fixes rust-lang#95271.
Fixes rust-lang#96299.
Fixes rust-lang#96512.
Fixes rust-lang#97378.

r? `@nikomatsakis` `@arora-aman`
@bors bors closed this as completed in fd76e0e Jun 7, 2022
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. 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.

3 participants