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: None in compiler/rustc_mir_build/src/build/matches/mod.rs with feature(never_patterns) #130779

Closed
Naserume opened this issue Sep 24, 2024 · 0 comments · Fixed by #135409
Closed
Assignees
Labels
C-bug Category: This is a bug. F-never_patterns `#![feature(never_patterns)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Naserume
Copy link

Code

(reduced)

#![feature(never_patterns)]

enum E { A }

fn main() {
    match E::A {
        ! |
        if true => {}
    }
}

(original)

// In this regression test we check that a trailing `|` in an or-pattern just
// before the `if` token of a `match` guard will receive parser recovery with
// an appropriate error message.

enum E { A, B }

fn main() {
    match E::A {
        ! | //~ ERROR a trailing `|` is not allowed in an or-pattern
        if true => {
            let recovery_witness: bool = 0; //~ ERROR mismatched types
        }
    }
}

Command: rustc ./7FFF.rs

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (7042c269c 2024-09-23)
binary: rustc
commit-hash: 7042c269c166191cd5d8daf0409890903df7af57
commit-date: 2024-09-23
host: x86_64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.0

Error output

error: a trailing `|` is not allowed in an or-pattern
 --> ./7FFF.rs:7:11
  |
7 |         ! |
  |         - ^
  |         |
  |         while parsing this or-pattern starting here
  |
help: remove the `|`
  |
7 -         ! |
7 +         ! 
  |

warning: the feature `never_patterns` is incomplete and may not be safe to use and/or cause compiler crashes
 --> ./7FFF.rs:1:12
  |
1 | #![feature(never_patterns)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
  = note: `#[warn(incomplete_features)]` on by default

error: a never pattern is always unreachable
 --> ./7FFF.rs:8:20
  |
8 |         if true => {}
  |                    ^^
  |                    |
  |                    this will never be executed
  |                    help: remove this expression

error: mismatched types
 --> ./7FFF.rs:7:9
  |
7 |         ! |
  |         ^ a never pattern must be used on an uninhabited type
  |
  = note: the matched value is of type `E`

Backtrace

thread 'rustc' panicked at compiler/rustc_mir_build/src/build/matches/mod.rs:1425:56:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:        0x117eeb3a7 - std::backtrace::Backtrace::create::h20048774a7df5a42
   1:        0x117eeb2f5 - std::backtrace::Backtrace::force_capture::ha939d1bf254d9107
   2:        0x115d1610e - std[d09fc85cb84ad7b]::panicking::update_hook::<alloc[d02d8d2ad132a946]::boxed::Box<rustc_driver_impl[4d5b267f1f304c4d]::install_ice_hook::{closure#0}>>::{closure#0}
   3:        0x117f06958 - std::panicking::rust_panic_with_hook::he2dff332e785153d
   4:        0x117f06595 - std::panicking::begin_panic_handler::{{closure}}::h873e97cbcdcf63ab
   5:        0x117f03d29 - std::sys::backtrace::__rust_end_short_backtrace::h81ffb9aee4e709dc
   6:        0x117f0620c - _rust_begin_unwind
   7:        0x11ab2063f - core::panicking::panic_fmt::h5235a0c22d55122a
   8:        0x11ab206e4 - core::panicking::panic::h0adbbbe009b54c8c
   9:        0x11ab205b8 - core::option::unwrap_failed::h1124b7069689f6d9
  10:        0x116c21408 - rustc_mir_build[a43d6420dfdfbc61]::build::matches::traverse_candidate::<rustc_mir_build[a43d6420dfdfbc61]::build::matches::Candidate, alloc[d02d8d2ad132a946]::vec::Vec<rustc_mir_build[a43d6420dfdfbc61]::build::matches::PatternExtraData>, alloc[d02d8d2ad132a946]::vec::into_iter::IntoIter<rustc_mir_build[a43d6420dfdfbc61]::build::matches::Candidate>, <rustc_mir_build[a43d6420dfdfbc61]::build::matches::MatchTreeBranch>::from_candidate::{closure#0}, <rustc_mir_build[a43d6420dfdfbc61]::build::matches::MatchTreeBranch>::from_candidate::{closure#1}, <rustc_mir_build[a43d6420dfdfbc61]::build::matches::MatchTreeBranch>::from_candidate::{closure#2}>
  11:        0x116c22b41 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::lower_match_tree
  12:        0x116c67413 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest
  13:        0x116c696c1 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest::{closure#0}
  14:        0x116c64c02 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest
  15:        0x116c0459c - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::ast_block_stmts
  16:        0x116c6aed3 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::in_scope::<<rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::ast_block::{closure#0}, ()>
  17:        0x116c65406 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest
  18:        0x116c696c1 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest::{closure#0}
  19:        0x116c64c02 - <rustc_mir_build[a43d6420dfdfbc61]::build::Builder>::expr_into_dest
  20:        0x116bf5e59 - rustc_mir_build[a43d6420dfdfbc61]::build::construct_fn
  21:        0x116bf12ad - rustc_mir_build[a43d6420dfdfbc61]::build::mir_build
  22:        0x116ee9f1c - rustc_mir_transform[5a9fb0fb7b7274f7]::mir_built
  23:        0x11755a7aa - rustc_query_impl[f79aee8be8e599e2]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[f79aee8be8e599e2]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 8usize]>>
  24:        0x1173b45ae - rustc_query_system[7eae2b3ce34a8ac8]::query::plumbing::try_execute_query::<rustc_query_impl[f79aee8be8e599e2]::DynamicConfig<rustc_query_system[7eae2b3ce34a8ac8]::query::caches::VecCache<rustc_span[89bbbdd941a6c1aa]::def_id::LocalDefId, rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[f79aee8be8e599e2]::plumbing::QueryCtxt, false>
  25:        0x11756f1ca - rustc_query_impl[f79aee8be8e599e2]::query_impl::mir_built::get_query_non_incr::__rust_end_short_backtrace
  26:        0x116c39daf - rustc_mir_build[a43d6420dfdfbc61]::check_unsafety::check_unsafety
  27:        0x11755606a - rustc_query_impl[f79aee8be8e599e2]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[f79aee8be8e599e2]::query_impl::check_unsafety::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 0usize]>>
  28:        0x1173a46a8 - rustc_query_system[7eae2b3ce34a8ac8]::query::plumbing::try_execute_query::<rustc_query_impl[f79aee8be8e599e2]::DynamicConfig<rustc_query_system[7eae2b3ce34a8ac8]::query::caches::VecCache<rustc_span[89bbbdd941a6c1aa]::def_id::LocalDefId, rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[f79aee8be8e599e2]::plumbing::QueryCtxt, false>
  29:        0x117583cb0 - rustc_query_impl[f79aee8be8e599e2]::query_impl::check_unsafety::get_query_non_incr::__rust_end_short_backtrace
  30:        0x116638162 - <rustc_middle[5020b1062cd3ce07]::hir::map::Map>::par_body_owners::<rustc_interface[1a7e4363985fe64]::passes::run_required_analyses::{closure#2}::{closure#0}>::{closure#0}
  31:        0x1166e442c - rustc_interface[1a7e4363985fe64]::passes::run_required_analyses
  32:        0x1166e6e40 - rustc_interface[1a7e4363985fe64]::passes::analysis
  33:        0x11755a4aa - rustc_query_impl[f79aee8be8e599e2]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[f79aee8be8e599e2]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 1usize]>>
  34:        0x117313f4e - rustc_query_system[7eae2b3ce34a8ac8]::query::plumbing::try_execute_query::<rustc_query_impl[f79aee8be8e599e2]::DynamicConfig<rustc_query_system[7eae2b3ce34a8ac8]::query::caches::SingleCache<rustc_middle[5020b1062cd3ce07]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[f79aee8be8e599e2]::plumbing::QueryCtxt, false>
  35:        0x117566b37 - rustc_query_impl[f79aee8be8e599e2]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  36:        0x115cbf647 - <rustc_interface[1a7e4363985fe64]::queries::QueryResult<&rustc_middle[5020b1062cd3ce07]::ty::context::GlobalCtxt>>::enter::<core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>, rustc_driver_impl[4d5b267f1f304c4d]::run_compiler::{closure#0}::{closure#1}::{closure#5}>
  37:        0x115d1d29d - rustc_interface[1a7e4363985fe64]::interface::run_compiler::<core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>, rustc_driver_impl[4d5b267f1f304c4d]::run_compiler::{closure#0}>::{closure#1}
  38:        0x115d0825c - std[d09fc85cb84ad7b]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1a7e4363985fe64]::util::run_in_thread_with_globals<rustc_interface[1a7e4363985fe64]::util::run_in_thread_pool_with_globals<rustc_interface[1a7e4363985fe64]::interface::run_compiler<core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>, rustc_driver_impl[4d5b267f1f304c4d]::run_compiler::{closure#0}>::{closure#1}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>::{closure#0}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>
  39:        0x115d1f85a - <<std[d09fc85cb84ad7b]::thread::Builder>::spawn_unchecked_<rustc_interface[1a7e4363985fe64]::util::run_in_thread_with_globals<rustc_interface[1a7e4363985fe64]::util::run_in_thread_pool_with_globals<rustc_interface[1a7e4363985fe64]::interface::run_compiler<core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>, rustc_driver_impl[4d5b267f1f304c4d]::run_compiler::{closure#0}>::{closure#1}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>::{closure#0}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[43b28bbecb023e0a]::result::Result<(), rustc_span[89bbbdd941a6c1aa]::ErrorGuaranteed>>::{closure#1} as core[43b28bbecb023e0a]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  40:        0x117f1182b - std::sys::pal::unix::thread::Thread::new::thread_start::h1828de8d8adf3dc7
  41:     0x7ff801f5318b - __pthread_start


rustc version: 1.83.0-nightly (7042c269c 2024-09-23)
platform: x86_64-apple-darwin

query stack during panic:
#0 [mir_built] building MIR for `main`
#1 [check_unsafety] unsafety-checking `main`
#2 [analysis] running analysis passes on this crate
end of query stack

Note

ICE location:

impl<'tcx> MatchTreeSubBranch<'tcx> {
fn from_sub_candidate(
candidate: Candidate<'_, 'tcx>,
parent_data: &Vec<PatternExtraData<'tcx>>,
) -> Self {
debug_assert!(candidate.match_pairs.is_empty());
MatchTreeSubBranch {
span: candidate.extra_data.span,
success_block: candidate.pre_binding_block.unwrap(),
otherwise_block: candidate.otherwise_block.unwrap(),
bindings: parent_data
.iter()
.flat_map(|d| &d.bindings)
.chain(&candidate.extra_data.bindings)
.cloned()
.collect(),
ascriptions: parent_data
.iter()
.flat_map(|d| &d.ascriptions)
.cloned()
.chain(candidate.extra_data.ascriptions)
.collect(),
is_never: candidate.extra_data.is_never,
}
}
}

@rustbot label +F-never_patterns

@Naserume Naserume 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 Sep 24, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-never_patterns `#![feature(never_patterns)]` labels Sep 24, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 24, 2024
@Nadrieril Nadrieril self-assigned this Sep 27, 2024
@matthiaskrgr matthiaskrgr added S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. and removed 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. C-bug Category: This is a bug. labels Sep 29, 2024
@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. labels Oct 9, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 22, 2025
…lse-edge-start-block, r=Nadrieril

Fix ICE-133117: multiple never-pattern arm doesn't have false_edge_start_block

Fixes rust-lang#133117 , and close fixes rust-lang#133063 , fixes rust-lang#130779

In order to fix ICE-133117, at first I needed to tackle to ICE-133063 (this fixed 130779 as well).

### ICE-133063 and ICE-130779
This ICE is caused by those steps:
1. An arm has or-pattern, and all of the sub-candidates are never-pattern
2. In that case, all sub-candidates are removed in remove_never_subcandidates(). So the arm (candidate) has no sub-candidate.
3. In the current implementation, if there is no sub-candidate, the function assigns `pre_binding_block` into the candidate ([here](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/builder/matches/mod.rs#L2002-L2004)). However, otherwise_block should be assigned to the candidate as well, because the otherwise_block is unwrapped in multiple place (like in lower_match_tree()). As a result, it causes the panic.

I simply added the same block as pre_binding_block into otherwise_block, but I'm wondering if there is a better block to assign to otherwise_block (is it ok to assign the same block into pre_binding and otherwise?)

### ICE-133117
This is caused by those steps:
1. There are two arms, both are or-pattern and each has one match-pair (in the test code, both are `(!|!)`), and the second arm has a guard.
2. In match_candidate() for the first arm, it expands the second arm’s sub-candidates as well ([here](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/builder/matches/mod.rs#L1800-L1805)). As a result, the root candidate of the second arm is not evaluated/modified in match_candidate(). So a false_edge_start_block is not assigned to the candidate.
3. merge_trivial_subcandidates() is called against the candidate for the second arm. It just returns immediately because the candidate has a guard. So a flase_edge_start_block is not assigned to the candidate also in this function.
4. remove_never_subcandidates() is called against the candidate. Since all sub-candidates are never-pattern. they are removed.
5. In lower_match_tree(), since there is no sub-candidate for the candidate, the candidate itself is evaluated in visit_leave_rev ([here](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/builder/matches/mod.rs#L1532)). Because the candidate has no false_edge_start_block, it causes the panic.

So I modified the order of if blocks in merge_trivial_subcandidates() to assign a false_edge_start_block if the candidate doesn't have.
@bors bors closed this as completed in f875983 Jan 23, 2025
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. F-never_patterns `#![feature(never_patterns)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. 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.

5 participants