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 using a constant as a pattern #138048

Closed
MoSal opened this issue Mar 5, 2025 · 2 comments · Fixed by #138284
Closed

ICE when using a constant as a pattern #138048

MoSal opened this issue Mar 5, 2025 · 2 comments · Fixed by #138284
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. 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

@MoSal
Copy link

MoSal commented Mar 5, 2025

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

@MoSal MoSal 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 5, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 5, 2025
@cyrgani
Copy link
Contributor

cyrgani commented Mar 5, 2025

a bit smaller:

struct Foo;

impl<'b> Foo {
    fn bar<const V: u8>() {
        let V;
    }
}

@Eclips4
Copy link
Contributor

Eclips4 commented Mar 6, 2025

Bisects to #137617

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Mar 9, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 11, 2025
…otation, r=BoxyUwU

Do not write user type annotation for const param value path

As I noted in the code comment, `DefKind::ConstParam` isn't actually *generic* over its own args, we just use the identity args from the body when lowering the value path so we have something to plug into the `EarlyBinder` we get back from `type_of` for the const param. So skip over it in `write_user_type_annotation_from_args`.

Somewhat unrelated, but I left an explanation for a somewhat mysterious quirk in the THIR lowering of user type annotations for patterns having to do with ctors and their `type_of` not actually being the type of the pattern node it's ascribing.

Fixes rust-lang#138048

r? `@BoxyUwU`
@bors bors closed this as completed in 16ff824 Mar 12, 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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. 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
5 participants