Skip to content

Inconsistent handling of associated constants in patterns #72602

Open
@sportzer

Description

@sportzer

The following code compiles:

trait Foo {
    const C: i32;
}

impl<T> Foo for T {
    const C: i32 = 0;
}

fn bar<T>() {
    match 0 {
        <T as Foo>::C => (),
        _ => (),
    }
}

but adding a trait bound of Foo to bar:

trait Foo {
    const C: i32;
}

impl<T> Foo for T {
    const C: i32 = 0;
}

fn bar<T: Foo>() {
    match 0 {
        <T as Foo>::C => (),
        _ => (),
    }
}

causes compilation to fail with:

error[E0158]: associated consts cannot be referenced in patterns
  --> src/main.rs:14:9
   |
14 |         <T as Foo>::C => (),
   |         ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0158`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

I would expect these two cases to behave identically instead of one working and the other failing. (it seems like both cases should be disallowed, but I don't have a good grasp of the current state of relevant features)

This is reproducible in the rust playground on the stable, beta, and nightly channels

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-patternsRelating to patterns and pattern matchingA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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