Skip to content

ICE when using a constant as a pattern #138048

Description

@MoSal

Bisected to 8c39296 (#137848).

Code

struct Foo<const V: u8>;

trait Bar<'b> {
    fn bar(n: u8);
}

// ICE *STILL* triggered if Foo doesn't have a const param
// ICE *NOT* if Bar doesn't have a lifetime param
// ICE *NOT* triggered obviously with correct `v if v == V`
impl<'b, const V: u8> Bar<'b> for Foo<V> {
    fn bar(n: u8) {
        match n {
            V => (),
            _ => (),
        }
    }
}

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f9e0239a7 2025-03-04)
binary: rustc
commit-hash: f9e0239a7bc813b4aceffc7f069f4797cde3175c
commit-date: 2025-03-04
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Error output

error[E0158]: constant parameters cannot be referenced in patterns
  --> src/lib.rs:13:13
   |
10 | impl<'b, const V: u8> Bar<'b> for Foo<V> {
   |          ----------- constant defined here
...
13 |             V => (),
   |             ^ can't be used in patterns


Backtrace

thread 'rustc' panicked at compiler/rustc_mir_build/src/thir/util.rs:25:14:
Box<dyn Any>
stack backtrace:
   0:     0x7fc2987e4da5 - std::backtrace::Backtrace::create::hfef1a8f8aac33464
   1:     0x7fc296bc9ff5 - std::backtrace::Backtrace::force_capture::h8687f78d397650db
   2:     0x7fc295d30dee - std[2cce8494628f4585]::panicking::update_hook::<alloc[ea6342a0a5a8b9b1]::boxed::Box<rustc_driver_impl[e161daf9ab2d11d7]::install_ice_hook::{closure#1}>>::{closure#0}
   3:     0x7fc296be2903 - std::panicking::rust_panic_with_hook::h2c8894f8997fcf6a
   4:     0x7fc295d6d471 - std[2cce8494628f4585]::panicking::begin_panic::<rustc_errors[e90c28bb776fdfc0]::ExplicitBug>::{closure#0}
   5:     0x7fc295d61886 - std[2cce8494628f4585]::sys::backtrace::__rust_end_short_backtrace::<std[2cce8494628f4585]::panicking::begin_panic<rustc_errors[e90c28bb776fdfc0]::ExplicitBug>::{closure#0}, !>
   6:     0x7fc295d5e209 - std[2cce8494628f4585]::panicking::begin_panic::<rustc_errors[e90c28bb776fdfc0]::ExplicitBug>
   7:     0x7fc295d76e61 - <rustc_errors[e90c28bb776fdfc0]::diagnostic::BugAbort as rustc_errors[e90c28bb776fdfc0]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   8:     0x7fc296352236 - rustc_middle[2d5b20c791c56ad0]::util::bug::opt_span_bug_fmt::<rustc_span[6699764a695b2e79]::span_encoding::Span>::{closure#0}
   9:     0x7fc296339b5a - rustc_middle[2d5b20c791c56ad0]::ty::context::tls::with_opt::<rustc_middle[2d5b20c791c56ad0]::util::bug::opt_span_bug_fmt<rustc_span[6699764a695b2e79]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  10:     0x7fc2963399cb - rustc_middle[2d5b20c791c56ad0]::ty::context::tls::with_context_opt::<rustc_middle[2d5b20c791c56ad0]::ty::context::tls::with_opt<rustc_middle[2d5b20c791c56ad0]::util::bug::opt_span_bug_fmt<rustc_span[6699764a695b2e79]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  11:     0x7fc2946fb380 - rustc_middle[2d5b20c791c56ad0]::util::bug::bug_fmt
  12:     0x7fc298d70e37 - rustc_mir_build[e0917d4cd90e2920]::thir::util::user_args_applied_to_ty_of_hir_id.cold
  13:     0x7fc297d1d777 - <rustc_mir_build[e0917d4cd90e2920]::thir::pattern::PatCtxt>::lower_variant_or_leaf
  14:     0x7fc297d1dfb2 - <rustc_mir_build[e0917d4cd90e2920]::thir::pattern::PatCtxt>::lower_pat_expr
  15:     0x7fc297d1a272 - <rustc_mir_build[e0917d4cd90e2920]::thir::pattern::PatCtxt>::lower_pattern
  16:     0x7fc297d18e8a - <rustc_mir_build[e0917d4cd90e2920]::thir::cx::ThirBuildCx>::pattern_from_hir
  17:     0x7fc297d12a06 - <rustc_mir_build[e0917d4cd90e2920]::thir::cx::ThirBuildCx>::mirror_expr
  18:     0x7fc297d12801 - <rustc_mir_build[e0917d4cd90e2920]::thir::cx::ThirBuildCx>::mirror_expr
  19:     0x7fc297d2714b - rustc_mir_build[e0917d4cd90e2920]::thir::cx::thir_body
  20:     0x7fc297d26c28 - rustc_query_impl[b4e30529d2c77c57]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[b4e30529d2c77c57]::query_impl::thir_body::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 16usize]>>
  21:     0x7fc297f1d406 - rustc_query_system[cb43291f2de4de55]::query::plumbing::try_execute_query::<rustc_query_impl[b4e30529d2c77c57]::DynamicConfig<rustc_data_structures[38628b744a74ab57]::vec_cache::VecCache<rustc_span[6699764a695b2e79]::def_id::LocalDefId, rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 16usize]>, rustc_query_system[cb43291f2de4de55]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[b4e30529d2c77c57]::plumbing::QueryCtxt, true>
  22:     0x7fc2985d96a2 - rustc_query_impl[b4e30529d2c77c57]::plumbing::force_from_dep_node::<rustc_query_impl[b4e30529d2c77c57]::DynamicConfig<rustc_data_structures[38628b744a74ab57]::vec_cache::VecCache<rustc_span[6699764a695b2e79]::def_id::LocalDefId, rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 16usize]>, rustc_query_system[cb43291f2de4de55]::dep_graph::graph::DepNodeIndex>, false, false, false>>
  23:     0x7fc2985d956d - <rustc_query_impl[b4e30529d2c77c57]::plumbing::query_callback<rustc_query_impl[b4e30529d2c77c57]::query_impl::thir_body::QueryType>::{closure#0} as core[e3f8cccd6bff4ec3]::ops::function::FnOnce<(rustc_middle[2d5b20c791c56ad0]::ty::context::TyCtxt, rustc_query_system[cb43291f2de4de55]::dep_graph::dep_node::DepNode)>>::call_once
  24:     0x7fc2974259ba - <rustc_query_system[cb43291f2de4de55]::dep_graph::graph::DepGraphData<rustc_middle[2d5b20c791c56ad0]::dep_graph::DepsType>>::try_mark_previous_green::<rustc_query_impl[b4e30529d2c77c57]::plumbing::QueryCtxt>
  25:     0x7fc29789c7fd - rustc_query_system[cb43291f2de4de55]::query::plumbing::ensure_must_run::<rustc_query_impl[b4e30529d2c77c57]::DynamicConfig<rustc_data_structures[38628b744a74ab57]::vec_cache::VecCache<rustc_span[6699764a695b2e79]::def_id::LocalDefId, rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[cb43291f2de4de55]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[b4e30529d2c77c57]::plumbing::QueryCtxt>
  26:     0x7fc297899a13 - rustc_query_impl[b4e30529d2c77c57]::query_impl::check_unsafety::get_query_incr::__rust_end_short_backtrace
  27:     0x7fc297ad5ac8 - rustc_interface[5fc383e867bdd00e]::passes::run_required_analyses
  28:     0x7fc298332dba - rustc_interface[5fc383e867bdd00e]::passes::analysis
  29:     0x7fc298332d99 - rustc_query_impl[b4e30529d2c77c57]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[b4e30529d2c77c57]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 0usize]>>
  30:     0x7fc298697faa - rustc_query_system[cb43291f2de4de55]::query::plumbing::try_execute_query::<rustc_query_impl[b4e30529d2c77c57]::DynamicConfig<rustc_query_system[cb43291f2de4de55]::query::caches::SingleCache<rustc_middle[2d5b20c791c56ad0]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[b4e30529d2c77c57]::plumbing::QueryCtxt, true>
  31:     0x7fc2986979a2 - rustc_query_impl[b4e30529d2c77c57]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  32:     0x7fc29849fc7d - rustc_interface[5fc383e867bdd00e]::passes::create_and_enter_global_ctxt::<core[e3f8cccd6bff4ec3]::option::Option<rustc_interface[5fc383e867bdd00e]::queries::Linker>, rustc_driver_impl[e161daf9ab2d11d7]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  33:     0x7fc2984e66e0 - rustc_interface[5fc383e867bdd00e]::interface::run_compiler::<(), rustc_driver_impl[e161daf9ab2d11d7]::run_compiler::{closure#0}>::{closure#1}
  34:     0x7fc298337588 - std[2cce8494628f4585]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[5fc383e867bdd00e]::util::run_in_thread_with_globals<rustc_interface[5fc383e867bdd00e]::util::run_in_thread_pool_with_globals<rustc_interface[5fc383e867bdd00e]::interface::run_compiler<(), rustc_driver_impl[e161daf9ab2d11d7]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  35:     0x7fc298337e74 - <<std[2cce8494628f4585]::thread::Builder>::spawn_unchecked_<rustc_interface[5fc383e867bdd00e]::util::run_in_thread_with_globals<rustc_interface[5fc383e867bdd00e]::util::run_in_thread_pool_with_globals<rustc_interface[5fc383e867bdd00e]::interface::run_compiler<(), rustc_driver_impl[e161daf9ab2d11d7]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e3f8cccd6bff4ec3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  36:     0x7fc298339277 - std::sys::pal::unix::thread::Thread::new::thread_start::h6a5943b86725331d
  37:     0x7fc2924a370a - <unknown>
  38:     0x7fc292527aac - <unknown>
  39:                0x0 - <unknown>


rustc version: 1.87.0-nightly (f9e0239a7 2025-03-04)
platform: x86_64-unknown-linux-gnu

query stack during panic:
#0 [thir_body] building THIR for `<impl at src/lib.rs:10:1: 10:41>::bar`
#1 [analysis] running analysis passes on this crate
end of query stack

Activity

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) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.T-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