rust-lang / rust-lang/rust

Generic patterns and generic array lengths can call const fn with impossible generics

Open
#147,721 13 comments 2 reactions 1 assignee View on GitHub

@BoxyUwU is already working on this.

Since Oct 21, 2025.

A-array A-const-eval A-patterns C-bug T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

// No type implements this trait
unsafe trait NotImplemented {}

const fn lol<T: NotImplemented>() -> usize {
    panic!("This should not be callable");
}

struct Dummy<T>(T);
impl<T: NotImplemented> Dummy<T> {
    const C: usize = lol::<T>();
}

fn foo<T: NotImplemented>() {
    if let Dummy::<T>::C = 1 {}
}

I expected the above code to not execute the panic, either at compile time or run time, since there is no T that implements NotImplemented. Instead, I got the following compiler error:

error[E0080]: evaluation panicked: This should not be callable
  --> src/lib.rs:10:22
   |
10 |     const C: usize = lol::<T>();
   |                      ^^^^^^^^^^ evaluation of `Dummy::<T>::C` failed inside this call
   |
note: inside `lol::<T>`
  --> src/lib.rs:5:5
   |
 5 |     panic!("This should not be callable");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here

For more information about this error, try `rustc --explain E0080`.

Note that the code compiles fine if I make fn lol return a value normally instead of panicking.

Also note that the code gives a reasonable compile error if I make fn lol return size_of::<T>()

Error with `size_of::()`
error[E0158]: constant pattern cannot depend on generic parameters
  --> src/lib.rs:14:12
   |
 9 | impl<T: NotImplemented> Dummy<T> {
   | --------------------------------
10 |     const C: usize = lol::<T>();
   |     -------------- constant defined here
...
13 | fn foo<T: NotImplemented>() {
   |        - constant depends on this generic parameter
14 |     if let Dummy::<T>::C = 1 {}
   |            ^^^^^^^^^^^^^ `const` depends on a generic parameter

For more information about this error, try `rustc --explain E0158`.

Using an array length instead of a pattern also runs into a similar issue:

Code and error using array length
// No type implements this trait
unsafe trait NotImplemented {}

const fn lol<T: NotImplemented>() -> usize {
    panic!("This should not be callable");
}

struct Dummy<T>(T);
impl<T: NotImplemented> Dummy<T> {
    const C: usize = lol::<T>();
}

fn foo<T: NotImplemented>() {
    let _x = [0u8; Dummy::<T>::C];
}
error[E0080]: evaluation panicked: This should not be callable
  --> src/lib.rs:10:22
   |
10 |     const C: usize = lol::<T>();
   |                      ^^^^^^^^^^ evaluation of `Dummy::<T>::C` failed inside this call
   |
note: inside `lol::<T>`
  --> src/lib.rs:5:5
   |
 5 |     panic!("This should not be callable");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here

note: erroneous constant encountered
  --> src/lib.rs:14:20
   |
14 |     let _x = [0u8; Dummy::<T>::C];
   |                    ^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.
Meta

Reproducible on the playground with version 1.92.0-nightly (2025-10-13 4b94758d2ba7d0ef71cc)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.