rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.1k stars 12.55k forks source link

ICE: variable should be placed in scope earlier #78115

Closed attente closed 3 years ago

attente commented 3 years ago

Code

From https://github.com/rust-lang/rust/issues/78115#issuecomment-712400626:

fn main() {
    async {
        match foo {
            Foo { a } | Foo { a } if true => {}
        }
    }
}

Meta

It fails at version:

rustc 1.49.0-nightly (adef9da30 2020-10-13)
binary: rustc
commit-hash: adef9da30f1ecbfeb81312d01ed94ac53f7161ba
commit-date: 2020-10-13
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0

It worked before at version:

rustc 1.49.0-nightly (8dae8cdcc 2020-10-12)
binary: rustc
commit-hash: 8dae8cdcc8fa879cea6a4bbbfa5b32e97be4c306
commit-date: 2020-10-12
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0

Error output

thread 'rustc' panicked at 'variable should be placed in scope earlier', compiler/rustc_typeck/src/check/generator_interior.rs:253:17
Backtrace

``` thread 'rustc' panicked at 'variable should be placed in scope earlier', compiler/rustc_typeck/src/check/generator_interior.rs:253:17 stack backtrace: 0: 0x7f4d75a73410 - std::backtrace_rs::backtrace::libunwind::trace::h622bab51c72c4e69 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/../../backtrace/src/backtrace/libunwind.rs:100:5 1: 0x7f4d75a73410 - std::backtrace_rs::backtrace::trace_unsynchronized::h7e820b882ebd41ee at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f4d75a73410 - std::sys_common::backtrace::_print_fmt::h64d46258114db92f at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/sys_common/backtrace.rs:79:5 3: 0x7f4d75a73410 - ::fmt::h47111d0c1b5f0da5 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/sys_common/backtrace.rs:58:22 4: 0x7f4d75ae1dfc - core::fmt::write::h1857a60b204f1b6a at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/core/src/fmt/mod.rs:1076:17 5: 0x7f4d75a65222 - std::io::Write::write_fmt::h4604516fed3e5731 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/io/mod.rs:1516:15 6: 0x7f4d75a7820d - std::sys_common::backtrace::_print::h785e7a78d5ef272c at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/sys_common/backtrace.rs:61:5 7: 0x7f4d75a7820d - std::sys_common::backtrace::print::h108047ac5c4555d5 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/sys_common/backtrace.rs:48:9 8: 0x7f4d75a7820d - std::panicking::default_hook::{{closure}}::h1d0c271f0d087ebf at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/panicking.rs:208:50 9: 0x7f4d75a77eb8 - std::panicking::default_hook::h692753e26f11b7b7 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/panicking.rs:227:9 10: 0x7f4d76373788 - rustc_driver::report_ice::h94e1b9f0f92bafae 11: 0x7f4d632fd346 - as core::ops::function::Fn>::call::hf18d60de8bdf947c at /nix/store/f3vnjjsamvv7y08favd91v5b49rrdh7j-rust-1.49.0-nightly-2020-10-13-adef9da30/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1056:9 12: 0x7f4d6337362b - proc_macro::bridge::client::::enter::{{closure}}::{{closure}}::h04750261f92a2f76 at /nix/store/f3vnjjsamvv7y08favd91v5b49rrdh7j-rust-1.49.0-nightly-2020-10-13-adef9da30/lib/rustlib/src/rust/library/proc_macro/src/bridge/client.rs:320:21 13: 0x7f4d75a78a56 - std::panicking::rust_panic_with_hook::h74ddc20305301cd9 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/panicking.rs:581:17 14: 0x7f4d77054d74 - std::panicking::begin_panic::{{closure}}::h4a2658fa82966a9a 15: 0x7f4d77054d4c - std::sys_common::backtrace::__rust_end_short_backtrace::hed4d41b3585d3722 16: 0x7f4d77028bec - std::panicking::begin_panic::h95fdecc40cfa9239 17: 0x7f4d77071857 - ::visit_arm::h94b7f6b234a9b274 18: 0x7f4d76fdd75d - rustc_hir::intravisit::walk_expr::hd498cca018a87b6a 19: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 20: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 21: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 22: 0x7f4d7707182d - ::visit_arm::h94b7f6b234a9b274 23: 0x7f4d76fdd75d - rustc_hir::intravisit::walk_expr::hd498cca018a87b6a 24: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 25: 0x7f4d7707182d - ::visit_arm::h94b7f6b234a9b274 26: 0x7f4d76fdd75d - rustc_hir::intravisit::walk_expr::hd498cca018a87b6a 27: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 28: 0x7f4d7707182d - ::visit_arm::h94b7f6b234a9b274 29: 0x7f4d76fdd75d - rustc_hir::intravisit::walk_expr::hd498cca018a87b6a 30: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 31: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 32: 0x7f4d76fbd2ba - rustc_hir::intravisit::walk_block::hc189e1162b2ae4e8 33: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 34: 0x7f4d7707182d - ::visit_arm::h94b7f6b234a9b274 35: 0x7f4d76fdd75d - rustc_hir::intravisit::walk_expr::hd498cca018a87b6a 36: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 37: 0x7f4d76fbd28a - rustc_hir::intravisit::walk_block::hc189e1162b2ae4e8 38: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 39: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 40: 0x7f4d77071bdc - ::visit_expr::h32f763fae026a435 41: 0x7f4d77070bac - rustc_typeck::check::generator_interior::resolve_interior::h9f6503f796e4811d 42: 0x7f4d76e1df4d - rustc_typeck::check::fn_ctxt::FnCtxt::resolve_generator_interiors::h47b7925cdb3a4a76 43: 0x7f4d770b6755 - rustc_infer::infer::InferCtxtBuilder::enter::hb26652f4cc0a3585 44: 0x7f4d77160ead - rustc_typeck::check::typeck::hfc75fba7d10c080a 45: 0x7f4d791c02f8 - rustc_middle::dep_graph::::with_deps::h625c205064841b32 46: 0x7f4d79300a52 - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::hd5d7e4be807854ff 47: 0x7f4d78ee3eb4 - rustc_data_structures::stack::ensure_sufficient_stack::hf0717cfa324b58a6 48: 0x7f4d78ce3012 - rustc_query_system::query::plumbing::get_query_impl::hda910c4b131c45f9 49: 0x7f4d78faf977 - rustc_middle::ty::context::TyCtxt::typeck_opt_const_arg::h4499a05eb9ea36c5 50: 0x7f4d77630088 - rustc_mir_build::thir::cx::Cx::new::h74b89b479cffd7fd 51: 0x7f4d775f3f48 - rustc_infer::infer::InferCtxtBuilder::enter::h5f228aaae7bfc457 52: 0x7f4d77530d63 - rustc_mir_build::build::mir_built::h614ae3973841342e 53: 0x7f4d77ba5df1 - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::mir_built>::compute::hffb2094c003bb703 54: 0x7f4d779dbf95 - rustc_middle::dep_graph::::with_deps::h9c20e3f215a95c0b 55: 0x7f4d77a4be83 - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h48d83b53377cf2a4 56: 0x7f4d776bd541 - rustc_middle::ty::query::plumbing::::start_query::{{closure}}::{{closure}}::{{closure}}::h7c679e4ccd6303cb 57: 0x7f4d77891fb4 - rustc_query_system::query::plumbing::get_query_impl::h6e6e1ee3c4581973 58: 0x7f4d77bdd72f - rustc_mir::transform::check_unsafety::unsafety_check_result::hcfe9711888c89ead 59: 0x7f4d77bb8bc5 - core::ops::function::FnOnce::call_once::hd09548458b941eee 60: 0x7f4d77ba6bce - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::unsafety_check_result>::compute::h78ac1dfa46e35df8 61: 0x7f4d779dd6f8 - rustc_middle::dep_graph::::with_deps::hb896adda9b6da41d 62: 0x7f4d77a4769a - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h25d1ae9814596de3 63: 0x7f4d776d0e71 - rustc_data_structures::stack::ensure_sufficient_stack::hd7bf926590dab0d9 64: 0x7f4d778a4c82 - rustc_query_system::query::plumbing::get_query_impl::h853be6621a53bed1 65: 0x7f4d778e45bd - rustc_query_system::query::plumbing::ensure_query_impl::h4f7ad11f1945d0eb 66: 0x7f4d77beceeb - rustc_mir::transform::mir_const::h6c08547191de2aa8 67: 0x7f4d77ba5ed1 - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::mir_const>::compute::h73d5e54d168e9c7b 68: 0x7f4d779dbf95 - rustc_middle::dep_graph::::with_deps::h9c20e3f215a95c0b 69: 0x7f4d77a4be83 - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h48d83b53377cf2a4 70: 0x7f4d776bd541 - rustc_middle::ty::query::plumbing::::start_query::{{closure}}::{{closure}}::{{closure}}::h7c679e4ccd6303cb 71: 0x7f4d77891fb4 - rustc_query_system::query::plumbing::get_query_impl::h6e6e1ee3c4581973 72: 0x7f4d77bed40e - rustc_mir::transform::mir_promoted::h0cc79eee2baede78 73: 0x7f4d77bfbc31 - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::mir_promoted>::compute::hf07dfd62fa1bd93f 74: 0x7f4d779dde25 - rustc_middle::dep_graph::::with_deps::hc0db1d632bfd9822 75: 0x7f4d77a46a4b - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h15a7ad4cc0a88ac4 76: 0x7f4d776c4c97 - rustc_data_structures::stack::ensure_sufficient_stack::h0d2458483a76bfbe 77: 0x7f4d778ab887 - rustc_query_system::query::plumbing::get_query_impl::h8833547dbc0131af 78: 0x7f4d77c30394 - rustc_mir::borrow_check::mir_borrowck::h6bb2526a5224ac7b 79: 0x7f4d77c01d55 - core::ops::function::FnOnce::call_once::ha0fc52f24667e21d 80: 0x7f4d77138dfe - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::mir_borrowck>::compute::heea38576bc2d13e0 81: 0x7f4d76f2b3c8 - rustc_middle::dep_graph::::with_deps::he5acd848f9358a3c 82: 0x7f4d770037da - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h2abd60179f78c7f2 83: 0x7f4d7701c611 - rustc_data_structures::stack::ensure_sufficient_stack::h2de0022b5b01d4a4 84: 0x7f4d76ebbe52 - rustc_query_system::query::plumbing::get_query_impl::hba937082ff871197 85: 0x7f4d77167be4 - rustc_typeck::collect::type_of::type_of::h74383c484653622d 86: 0x7f4d76f1fecf - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::type_of>::compute::hfd79d1381924d3f3 87: 0x7f4d76f27e21 - rustc_middle::dep_graph::::with_deps::h623ab536fd446e13 88: 0x7f4d77002b5f - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h26a5d8549648a6c6 89: 0x7f4d7701f611 - rustc_data_structures::stack::ensure_sufficient_stack::h61e1796ea93702d9 90: 0x7f4d76e80f3c - rustc_query_system::query::plumbing::get_query_impl::h558e4dad03e07b80 91: 0x7f4d76f4213c - rustc_typeck::check::check::check_item_type::h98773239642c80eb 92: 0x7f4d76ff45f9 - rustc_middle::hir::map::Map::visit_item_likes_in_module::h3b0059be633bc701 93: 0x7f4d76f4644d - rustc_typeck::check::check::check_mod_item_types::h05209c564fff9b9f 94: 0x7f4d76ff066e - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::check_mod_item_types>::compute::h688ec518ca5b3446 95: 0x7f4d76f27736 - rustc_middle::dep_graph::::with_deps::h5c9d5e4e9891623e 96: 0x7f4d77009f07 - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::h763820e87aeb42a6 97: 0x7f4d77026584 - rustc_data_structures::stack::ensure_sufficient_stack::hd02e3960b4765973 98: 0x7f4d76ed47fa - rustc_query_system::query::plumbing::get_query_impl::hd692e841feec859b 99: 0x7f4d76ee4fee - rustc_query_system::query::plumbing::ensure_query_impl::hfd8292610205e5d4 100: 0x7f4d76ff6c82 - rustc_session::utils::::time::hf8479066877269ae 101: 0x7f4d77079850 - rustc_typeck::check_crate::hfa26513dd6ccd57c 102: 0x7f4d765cea4f - rustc_interface::passes::analysis::h0de92c2792188299 103: 0x7f4d763c08b2 - rustc_middle::ty::query:: for rustc_middle::ty::query::queries::analysis>::compute::h00d2ec9ff8460c2c 104: 0x7f4d763022d8 - rustc_middle::dep_graph::::with_deps::hb4079a860e34ece0 105: 0x7f4d76380f67 - rustc_query_system::dep_graph::graph::DepGraph::with_task_impl::hc5cfe7a3bcd09e8e 106: 0x7f4d76397a70 - rustc_data_structures::stack::ensure_sufficient_stack::h1e73fd3769ac2d2b 107: 0x7f4d76331e21 - rustc_query_system::query::plumbing::get_query_impl::h7845c9072f9f47fa 108: 0x7f4d763c371f - rustc_interface::passes::QueryContext::enter::hf289a5ab7388ed19 109: 0x7f4d76394c82 - rustc_interface::queries::::enter::h75641ba8f2c3ef50 110: 0x7f4d76377c67 - rustc_span::with_source_map::h62a26cc1f538b576 111: 0x7f4d763903f1 - scoped_tls::ScopedKey::set::h851e47b28a99a947 112: 0x7f4d76399015 - std::sys_common::backtrace::__rust_begin_short_backtrace::hcc10758615a842a8 113: 0x7f4d7631f9de - core::ops::function::FnOnce::call_once{{vtable.shim}}::hdee37546948bd4b7 114: 0x7f4d75a8786a - as core::ops::function::FnOnce>::call_once::h588ce842808b3fd0 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/alloc/src/boxed.rs:1042:9 115: 0x7f4d75a8786a - as core::ops::function::FnOnce>::call_once::h46bc655f05471a92 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/alloc/src/boxed.rs:1042:9 116: 0x7f4d75a8786a - std::sys::unix::thread::Thread::new::thread_start::hb4b79d379b730058 at /rustc/adef9da30f1ecbfeb81312d01ed94ac53f7161ba/library/std/src/sys/unix/thread.rs:87:17 117: 0x7f4d759c5ead - start_thread 118: 0x7f4d758e5d2f - __clone 119: 0x0 - 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.49.0-nightly (adef9da30 2020-10-13) running on x86_64-unknown-linux-gnu note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type lib note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [typeck] type-checking `player_action::run_player_action` #1 [mir_built] building MIR for `player_action::run_player_action` #2 [unsafety_check_result] unsafety-checking `player_action::run_player_action` #3 [mir_const] processing MIR for `player_action::run_player_action` #4 [mir_promoted] processing `player_action::run_player_action` #5 [mir_borrowck] borrow-checking `player_action::run_player_action` #6 [type_of] computing type of `player_action::run_player_action::{opaque#0}` #7 [check_mod_item_types] checking item types in module `player_action` #8 [analysis] running analysis passes on this crate end of query stack ```

attente commented 3 years ago

Sorry there's no code, but I've narrowed it to somewhere between 8dae8cdcc8fa879cea6a4bbbfa5b32e97be4c306 (working) and adef9da30f1ecbfeb81312d01ed94ac53f7161ba (broken).

jonas-schievink commented 3 years ago

Likely cause: https://github.com/rust-lang/rust/pull/75213

cc @dingxiangfei2009 @tmandry

arilotter commented 3 years ago

MCVE:

fn main() {
    async {
        match foo {
            Foo { a } | Foo { a } if true => {}
        }
    }
}

The ICE occurs (inside async blocks or functions) if you're using a match guard and binding the same variable name in both sides of an "or" in a match pattern.

LeSeulArtichaut commented 3 years ago

cargo-bisect-rustc finds a regression in adef9da30f1ecbfeb81312d01ed94ac53f7161ba which does correspond to #75213

LeSeulArtichaut commented 3 years ago

Assigning P-critical as discussed as part of the Prioritization WG procedure.

LeSeulArtichaut commented 3 years ago

This ICE is caused here:

https://github.com/rust-lang/rust/blob/a85e94927622665a9e9022de0d33a890a2e32d43/compiler/rustc_typeck/src/check/generator_interior.rs#L371-L374

According to the docs:

The HirId is the canonical ID for the variable being bound, (e.g., in Ok(x) | Err(x), both x use the same canonical ID), which is the pattern ID of the first x.

As we're using a set and a vec in parallel, the ID is pushed twice but stored "only once". Then, we remove it once, and it still exists in the vec but not in the set:

https://github.com/rust-lang/rust/blob/a85e94927622665a9e9022de0d33a890a2e32d43/compiler/rustc_typeck/src/check/generator_interior.rs#L252-L257