E0158 Constant Pattern Generic Parameter Fix

Written December 3, 2026 · Tested with Rust 1.99.1 · 23 min read

By the end of this guide
  • The rule underneath: a pattern must be fixed before the generic code is specialized
  • How to read the diagnostic
  • The first failure: the index example
  • The same error in other positions and for a const generic parameter

Written December 3, 2026

Tested with Rust 1.99.1

What this guide is

Error E0158 means a constant pattern depends on a generic parameter - in the index's words, a generic parameter or static has been referenced in a pattern. The error codes index example matches a value against A::X, the associated constant of a trait, inside a function that is generic over A, and its advice is to compare against the constant in a match guard instead of using it as a pattern.

In the shapes probed in this guide, the index example verbatim drew exactly one E0158 and no other code - this premise is clean, with no ride-along to separate. The count follows the pattern positions, in the probed shapes: two generic constant patterns in two arms of one match drew exactly two E0158 reports, one per arm, while a range pattern built from two generic constants drew one report, its span covering the first endpoint. The position changes nothing in kind: the same generic constant as a pattern in an if let, a while let, a plain let binding, a function argument, a tuple pattern, an or-pattern, a nested match, a matches! call, and as Self::X in a trait default method each drew exactly one E0158 in the probed shapes, and a const generic parameter used as a pattern drew the same code under a different header wording in the probed shape. The ban is on the pattern position, not on the constant: the same generic constant used as a value - assigned, and compared with == - compiled and ran in the probed shape, and the same associated constant named through a concrete type compiled and ran as a pattern. One part of the index's summary does not survive contact with the compiler in the probed shapes: a static referenced in a pattern did not draw E0158 at all - a bare static name drew E0530, and a qualified static path drew E0532. This guide shows the E0158 shapes, the neighboring shapes, the boundary, and the repairs.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.

The rule underneath: a pattern must be fixed before the generic code is specialized

A pattern is checked while the generic function is still generic. To build a match, the compiler has to know what each constant pattern will compare against, and an associated constant like A::X has no value until A is known - which happens later, when the function is specialized for a concrete type. The Rust Reference states the underlying requirement directly: the value of a constant used as a pattern must be known at pattern-building time, which is pre-monomorphization, and that means associated constants that involve generic parameters cannot be used as patterns.

That is why the repair is a move, not a redesign. The constant itself is fine, and so is comparing against it: A::X works as a value anywhere a value is expected. What has to go is its seat in the pattern. Bind the matched value with a variable pattern and compare in the guard - c if c == A::X - or drop the match for that check and compare in an if condition, and the generic function compiles, because the comparison now runs at the point where values exist.

One nearby name has its own version of the same ban, under the same code but a different header. A const generic parameter - const Y: char - used directly as a pattern drew E0158 in the probed shape, with the header "constant parameters cannot be referenced in patterns" instead of the associated-constant wording. Read the header you have: both wordings are E0158, and the guard repair is the same shape for both.

How to read the diagnostic

For an associated constant, the E0158 header is fixed wording: "constant pattern cannot depend on generic parameters". The report carries three spans: the trait where the constant is defined, labelled "constant defined here", the generic parameter in the function's signature, labelled "constant depends on this generic parameter", and the pattern itself, underlined and labelled "const depends on a generic parameter". There is no suggested fix in the report itself; the guard advice lives in the index entry.

The count follows the pattern positions, in the probed shapes - one report per arm that carries a generic constant pattern. Two arms carrying one each drew two reports, ending with "aborting due to 2 previous errors":

Output
error[E0158]: constant pattern cannot depend on generic parameters
  --> fail_two_generic_patterns.rs:17:9
   |
 7 | trait Bar {
   | ---------
 8 |     const X: Foo;
   |     ------------ constant defined here
...
15 | fn test<A: Bar, B: Baz>(arg: Foo) {
   |         - constant depends on this generic parameter
16 |     match arg {
17 |         A::X => println!("x"),
   |         ^^^^ `const` depends on a generic parameter

error[E0158]: constant pattern cannot depend on generic parameters
  --> fail_two_generic_patterns.rs:18:9
   |
11 | trait Baz {
   | ---------
12 |     const Y: Foo;
   |     ------------ constant defined here
...
15 | fn test<A: Bar, B: Baz>(arg: Foo) {
   |                 - constant depends on this generic parameter
...
18 |         B::Y => println!("y"),
   |         ^^^^ `const` depends on a generic parameter

error: aborting due to 2 previous errors

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

Fix every generic constant pattern in the output before recompiling; a second one is already reported, not hidden behind the first. A single pattern position that mentions two generic constants still drew one report in the probed shape: the range pattern A::LO..=A::HI drew exactly one E0158, its span covering A::LO, the first endpoint, in the file fail_generic_range_pattern.rs. Position inside the match changes nothing: a generic constant pattern in the second arm, after a plain variant arm, drew exactly one E0158 in the probed shape, in the file fail_generic_second_match_arm_only.rs.

The first failure: the index example

The index's erroneous example, verbatim - the pattern is the associated constant of the generic parameter:

Rust
#![allow(unused)]
fn main() {
enum Foo {
    One,
    Two
}

trait Bar {
    const X: Foo;
}

fn test<A: Bar>(arg: Foo) {
    match arg {
        A::X => println!("A::X"), // error: E0158: constant pattern depends
                                  //        on a generic parameter
        Foo::Two => println!("Two")
    }
}
}
Output
error[E0158]: constant pattern cannot depend on generic parameters
  --> fail_index.rs:14:9
   |
 8 | trait Bar {
   | ---------
 9 |     const X: Foo;
   |     ------------ constant defined here
...
12 | fn test<A: Bar>(arg: Foo) {
   |         - constant depends on this generic parameter
13 |     match arg {
14 |         A::X => println!("A::X"), // error: E0158: constant pattern depends
   |         ^^^^ `const` depends on a generic parameter

error: aborting due to 1 previous error

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

Exactly one report, and it is the whole story: no second code rides along in this example. The separation checks confirm the generic constant pattern is the only defect. Replacing the pattern with a wildcard arm in the same function - keeping the trait, the generic parameter, and the matched type - compiled and ran, printing any, in the file sep_pattern_replaced_wildcard.rs. And the constant does not need the pattern seat to be the problem: naming the same associated constant through a concrete implementor, as S::X, compiled and ran as a pattern in the probed shape, shown in the boundary section below.

The same error in other positions and for a const generic parameter

The generic constant draws the same report wherever a pattern sits. An associated constant of a different type - A::X where the constant is a char - drew exactly one E0158 in the probed shape, in the file fail_generic_char.rs, and so did the pattern inside a tuple pattern, (A::X, 0), in the file fail_generic_tuple_pattern.rs, inside an or-pattern, A::X | Foo::Two, in the file fail_generic_or_pattern.rs, and inside a nested match in the file fail_generic_nested_match.rs. The pattern-bearing constructs outside match behave the same, in the probed shapes: an if let A::X = arg drew exactly one E0158 in the file fail_generic_in_if_let.rs, a while let drew one in the file fail_generic_in_while_let.rs, a function argument written as the pattern A::X: Foo drew one in the file fail_generic_arg_pattern.rs, and the matches! macro drew one in the file fail_generic_matches_macro.rs. A plain let binding drew the same single report in the probed shape - let A::X = arg; drew exactly one E0158 and no refutability report alongside it, in the file neighbor_refutable_let.rs.

Writing the constant through the trait itself changes nothing. A trait default method that matches against Self::X - the trait's own associated constant - drew exactly one E0158 in the probed shape:

Output
error[E0158]: constant pattern cannot depend on generic parameters
  --> fail_self_assoc_pattern.rs:11:13
   |
 7 | trait Bar {
   | --------- constant depends on this generic parameter
 8 |     const X: Foo;
   |     ------------ constant defined here
 9 |     fn is_x(arg: Foo) -> bool {
   |        ----
10 |         match arg {
11 |             Self::X => true,
   |             ^^^^^^^ `const` depends on a generic parameter

error: aborting due to 1 previous error

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

Self in a trait method is a generic parameter like any other, so its constant is unknown at pattern-building time in the same way.

A const generic parameter is the same code under the other header. The pattern Y, where Y is declared as const Y: char, drew exactly one E0158 in the probed shape, worded for parameters rather than for associated constants:

Output
error[E0158]: constant parameters cannot be referenced in patterns
 --> fail_const_generic_pattern.rs:3:9
  |
1 | fn test<const Y: char>(arg: char) {
  |         ------------- constant defined here
2 |     match arg {
3 |         Y => println!("why"),
  |         ^ can't be used in patterns

error: aborting due to 1 previous error

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

The index's own repair example leans on exactly this distinction: it moves Y out of the pattern and into a guard, c if c == Y, which is the shape that compiles in the repairs section below.

The shapes that are not this error

The index entry says the same restriction holds for statics, but a static in a pattern did not draw E0158 in either probed shape. A bare static name in a match arm is read as a new binding, and a binding may not shadow a static - the report is E0530, and it arrives with two warnings, an unreachable-pattern warning for the arm after it and an unused-variable warning for the binding itself:

Output
error[E0530]: match bindings cannot shadow statics
 --> fail_static_pattern.rs:6:9
  |
1 | static FOO: char = 'j';
  | ----------------------- the static `FOO` is defined here
...
6 |         FOO => println!("eff"),
  |         ^^^ cannot be named the same as a static

warning: unreachable pattern
 --> fail_static_pattern.rs:7:9
  |
6 |         FOO => println!("eff"),
  |         --- matches any value
7 |         _ => println!("other"),
  |         ^ no value can reach this
  |
  = note: `#[warn(unreachable_patterns)]` (part of `#[warn(unused)]`) on by default

warning: unused variable: `FOO`
 --> fail_static_pattern.rs:6:9
  |
6 |         FOO => println!("eff"),
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_FOO`
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

error: aborting due to 1 previous error; 2 warnings emitted

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

The warnings belong to the binding reading, not to the static: because FOO binds any value, the wildcard arm after it can never be reached. Naming the static through its module path is a different failure again - a path pattern must name a constant, a unit struct, or a unit variant, and a static is none of those, so the report is E0532 in the probed shape:

Output
error[E0532]: expected unit struct, unit variant or constant, found static `m::FOO`
 --> fail_static_qualified_pattern.rs:8:9
  |
8 |         m::FOO => println!("eff"),
  |         ^^^^^^ not a unit struct, unit variant or constant

error: aborting due to 1 previous error

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

Read which code you have before repairing. E0158 points at a constant that depends on a generic parameter; E0530 at a bare name that shadows a static; E0532 at a qualified path that names something a pattern cannot name at all. Only the first is repaired with a guard - the static shapes are repaired by comparing against the static in a guard or a condition, as the boundary section shows for the guard form, because the static cannot sit in the pattern under any spelling.

A generic constant whose type does not match the matched value is also a different failure. Where the trait's constant is a u32 and the matched value is the enum, the same A::X pattern drew exactly one E0308 in the probed shape - and no E0158:

Output
error[E0308]: mismatched types
  --> neighbor_type_mismatch_const.rs:13:9
   |
 8 |     const X: u32;
   |     ------------ associated constant defined here
...
12 |     match arg {
   |           --- this expression has type `Foo`
13 |         A::X => println!("x"),
   |         ^^^^
   |         |
   |         expected `Foo`, found `u32`
   |         `X` is interpreted as an associated constant, not a new binding
   |
help: introduce a new binding instead
   |
13 -         A::X => println!("x"),
13 +         A::other_x => println!("x"),
   |

error: aborting due to 1 previous error

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

The report even notes that X "is interpreted as an associated constant, not a new binding". Fix the type first in that shape; the generic-dependence problem is only reported once the pattern is at least the right type to be judged.

And the ordinary exhaustiveness failure is its own code, as expected. A match on the same enum with the generic pattern removed and one variant left uncovered drew exactly one E0004 in the probed shape, in the file neighbor_nonexhaustive.rs - the wildcard arm in the failing shapes is load-bearing, and dropping it while repairing trades E0158 for E0004.

The shape where a second code rides along

Other errors in the same program are reported alongside E0158. A program whose generic-pattern function shared the file with an unresolved name in the entry point drew E0158 and E0425 together in the probed shape, ending with "aborting due to 2 previous errors":

Output
error[E0425]: cannot find value `missing_name` in this scope
  --> ride_unresolved.rs:18:28
   |
18 | fn main() { println!("{}", missing_name); }
   |                            ^^^^^^^^^^^^ not found in this scope

error[E0158]: constant pattern cannot depend on generic parameters
  --> ride_unresolved.rs:13:9
   |
 7 | trait Bar {
   | ---------
 8 |     const X: Foo;
   |     ------------ constant defined here
...
11 | fn test<A: Bar>(arg: Foo) {
   |         - constant depends on this generic parameter
12 |     match arg {
13 |         A::X => println!("A::X"),
   |         ^^^^ `const` depends on a generic parameter

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0158, E0425.
For more information about an error, try `rustc --explain E0158`.

Fixing the pattern clears the E0158 in that shape and leaves the E0425 standing - the unresolved name is a separate problem that happens to share the output.

One warning rides along with a legal shape rather than with the error. A plain constant used as a pattern twice in one match compiles and runs - the second use drew an unreachable-pattern warning in the probed shape, in the file neighbor_unreachable_const_dup.rs, and the program printed first: a constant pattern is a real test, so a repeated one can never match again. The warning belongs to the repetition, not to the constant, and it never blocks the build.

The boundary: concrete constants, plain constants, and the constant as a value

The line is whether the constant's value is fixed before specialization. On the other side of it, constant patterns are ordinary code. The index example's own setup, with the pattern named through a concrete implementor instead of the generic parameter, compiled and ran in the probed shape, printing concrete:

Rust
#[derive(Debug, PartialEq)]
enum Foo {
    One,
    Two
}

trait Bar {
    const X: Foo;
}

struct S;
impl Bar for S {
    const X: Foo = Foo::One;
}

fn main() {
    let arg = Foo::One;
    match arg {
        S::X => println!("concrete"),
        Foo::Two => println!("Two"),
    }
}
Output
concrete

A plain constant item is the simplest legal form: a const FIVE: u32 = 5; used as a pattern compiled and ran in the probed shape, printing five, in the file boundary_local_const.rs, and the same constant named through its module path - m::FIVE - compiled and ran, printing five, in the file boundary_path_const_pattern.rs: a qualified path is fine when it names an actual constant, unlike the static path in the E0532 shape above. A constant item holding an enum value - const FAV: Foo = Foo::One; - compiled and ran as a pattern in the probed shape, printing fav, in the file boundary_enum_const_item.rs. And the variant patterns the index example already uses, Foo::One and Foo::Two covering the enum between them, compiled and ran in the probed shape, printing Two, in the file boundary_variant_patterns.rs.

The sharpest boundary is the generic constant itself, used as a value. Nothing about A::X is banned - only its seat in a pattern is. Assigning it and comparing the matched value against it compiled and ran in the probed shape, printing true:

Rust
#[derive(Debug, PartialEq)]
enum Foo {
    One,
    Two
}

trait Bar {
    const X: Foo;
}

struct S;
impl Bar for S {
    const X: Foo = Foo::One;
}

fn probe<A: Bar>(arg: Foo) -> bool {
    let wanted = A::X;
    arg == wanted
}

fn main() {
    println!("{}", probe::<S>(Foo::One));
}
Output
true

That value use is what every repair below is built from: the guard and the if condition are both places where a value, rather than a pattern, is expected.

For a match that leaves a value uncovered, the neighboring code is E0004. For a bare pattern name that shadows a static, the neighboring code is E0530; for a qualified path that names a static in a pattern, it is E0532; for a constant pattern of the wrong type for the matched value, it is E0308; and for an unresolved name elsewhere in a program that also carries a generic constant pattern, it is E0425.

The repairs

Every repair below starts from the same observation: the constant may keep its role in the comparison, but it must leave the pattern. Either bind the matched value and compare in a guard - the index's repair - or compare in a condition, or name the constant through a concrete type when the code is not really generic over it.

Use the index's guard form. This is the index's own repaired example, verbatim: the generic associated constant, the const generic parameter, and the static all move into guards, each arm binding the value first with c:

Rust
#![allow(unused)]
fn main() {
trait Trait {
    const X: char;
}

static FOO: char = 'j';

fn test<A: Trait, const Y: char>(arg: char) {
    match arg {
        c if c == A::X => println!("A::X"),
        c if c == Y => println!("Y"),
        c if c == FOO => println!("FOO"),
        _ => ()
    }
}
}

The shape compiled in the probed shape. It carries no entry-point call - the example defines the function inside the wrapper and never calls it - so there is no program output to show; the runnable version of the same guard, below, is the one that prints.

Use the guard in a runnable function. The same move in the index example's own setting - the trait constant compared in the guard of the arm that binds the value - compiled and ran in the probed shape, printing is-x for the value equal to the concrete implementor's constant:

Rust
trait Trait {
    const X: char;
}

struct S;
impl Trait for S {
    const X: char = 'j';
}

fn classify<A: Trait>(arg: char) -> &'static str {
    match arg {
        c if c == A::X => "is-x",
        _ => "other",
    }
}

fn main() {
    println!("{}", classify::<S>('j'));
}
Output
is-x

The guard form answers the static shapes too, in the probed shapes: a static compared in a guard - c if c == FOO - compiled and ran, printing eff, in the file boundary_static_guard.rs, and a const generic parameter compared in a guard - c if c == Y - compiled and ran, printing why, in the file boundary_const_generic_guard_only.rs. Neither the static nor the parameter ever sits in a pattern in those shapes; the guard is where all three kinds of name become legal at once.

Compare in an if condition instead. Where the match exists mainly to test the value against the constant, the match can go entirely: an if condition is a value context, so arg == A::X is ordinary code there. The shape compiled and ran in the probed shape, printing is-x:

Rust
#[derive(Debug, PartialEq)]
enum Foo {
    One,
    Two
}

trait Bar {
    const X: Foo;
}

struct S;
impl Bar for S {
    const X: Foo = Foo::One;
}

fn describe<A: Bar>(arg: Foo) -> &'static str {
    if arg == A::X {
        "is-x"
    } else if arg == Foo::Two {
        "Two"
    } else {
        "other"
    }
}

fn main() {
    println!("{}", describe::<S>(Foo::One));
}
Output
is-x

Callers are unaffected by either repair in the probed shapes - a generic constant pattern never reached a caller, because a function with one in it never compiled; what changes is only where inside the function the comparison happens.

Fixes in reach order

First, read the header. "Constant pattern cannot depend on generic parameters" is an associated constant of a generic parameter; "constant parameters cannot be referenced in patterns" is a const generic parameter itself. Both are E0158, and both take the guard repair.

Second, move each generic constant out of its pattern and into a guard - bind the value with a variable pattern and compare, c if c == A::X. That is the index's repair, and it clears every E0158 in the match at once while keeping the comparison identical.

Third, if the match is only testing against the constant, replace it with an if comparison - arg == A::X in a condition is a value use, and value uses of the same constant were never banned.

Fourth, check whether the code needs to be generic at that point at all: naming the constant through a concrete implementor - S::X - is a legal pattern in the probed shapes, so a helper written for one known type can keep the pattern form.

Fifth, if the name in the pattern is a static, do not reach for the E0158 repair first: a bare static name is E0530 and a qualified static path is E0532 in the probed shapes, and the working form for a static is the same guard comparison, reached by reading the code you actually have.

Sixth, if the output carries a second code, separate it before judging the program: an unresolved name elsewhere is an E0425 of its own, and a constant pattern of the wrong type is an E0308 that masks the E0158 until the type is fixed - each survives the pattern fix until it is fixed on its own terms.

Decision summary

E0158 fired in the probed shapes for the index example verbatim (exactly one E0158, no other code), for an associated constant of char type, for the pattern in a tuple pattern, an or-pattern, a nested match, an if let, a while let, a let binding, a function argument pattern, and a matches! call (exactly one each), for Self::X in a trait default method (exactly one), for a generic constant pattern in the second arm after a plain variant arm (exactly one), for two generic constant patterns in two arms of one match (exactly two, one per arm), for a range pattern built from two generic constants (exactly one, spanning the first endpoint), and for a const generic parameter used as a pattern (exactly one, under the header "constant parameters cannot be referenced in patterns"). It rode along, in the probed shapes, with E0425 for an unresolved name elsewhere in the program. It did not fire, in the probed shapes, for the same function with the pattern replaced by a wildcard (compiled and ran, printing any), for the associated constant named through a concrete implementor (printing concrete), for a plain constant item as a pattern (printing five), for the same constant named through its module path (printing five), for an enum-typed constant item as a pattern (printing fav), for the enum's variant patterns (printing Two), or for the generic constant used as a value - assigned and compared with == (printing true). The neighboring shapes drew other reports instead in the probed shapes: a bare static name in a pattern drew exactly one E0530 with an unreachable-pattern warning and an unused-variable warning, a qualified static path drew exactly one E0532, a generic constant whose type did not match the matched value drew exactly one E0308 and no E0158, and a match missing an arm drew exactly one E0004. The repairs that compiled and ran in the probed shapes were the index's guard form verbatim (compiled; the example never calls the function, so no output), the guard in a runnable function (printing is-x), a static compared in a guard (printing eff), a const generic parameter compared in a guard (printing why), and a comparison in an if condition (printing is-x).

Checklist

  • The test is dependence, in the probed shapes: a constant pattern is legal only when its value is fixed before specialization - a plain constant item, a qualified constant path, and a concrete type's associated constant all compile and run as patterns.
  • The count is per pattern position, in the probed shapes: two arms carrying a generic constant pattern drew two reports, and one position mentioning two generic constants - a range pattern - drew one.
  • The position changes nothing, in the probed shapes: match, if let, while let, let, a function argument, and matches! all drew E0158 for the same generic constant.
  • A const generic parameter is the same code with a different header, in the probed shapes: "constant parameters cannot be referenced in patterns" is also E0158.
  • A static is never E0158 in the probed shapes: a bare static name drew E0530 and a qualified static path drew E0532 - check which code you have before repairing.
  • The constant itself is never the problem, in the probed shapes: the same generic constant assigned and compared as a value compiled and ran, which is why the guard and if repairs work.

Sources

  • Rust error codes index, E0158 - a generic parameter or static has been referenced in a pattern; the erroneous example matches against A::X, the associated constant of the generic parameter A, and the repair moves the constant, a const generic parameter, and a static into match guards (c if c == A::X, c if c == Y, c if c == FOO).
  • Rust error codes index, E0530 - a binding shadowed something it should not, including a static; the neighboring code drawn in the probed shape by a bare static name used as a pattern.
  • Rust error codes index, E0532 - a pattern arm did not match the expected kind; the neighboring code drawn in the probed shape by a qualified path naming a static in a pattern, reported as "expected unit struct, unit variant or constant, found static".
  • Rust error codes index, E0308 - the expected type did not match the received type; the neighboring code drawn in the probed shape, alone and with no E0158, by a generic constant pattern whose constant has a different type from the matched value.
  • Rust error codes index, E0004 - the compiler cannot guarantee a matching pattern for one or more possible inputs to a match expression; the neighboring code drawn in the probed shape by the same enum match with an arm missing.
  • Rust error codes index, E0425 - an unresolved name was used; the neighboring code drawn in the probed shape by an unresolved name in a program that also carried a generic constant pattern.
  • The Rust Reference, Patterns - constant patterns: when a constant is used as a pattern, its type must have PartialEq and its value must have structural equality; the value must be known at pattern-building time, which is pre-monomorphization, so associated constants that involve generic parameters cannot be used as patterns, and the value must not contain references to mutable statics or extern statics.

Keep Exploring