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

Inconsistent handling of associated constants in patterns #72602

Open
sportzer opened this issue May 26, 2020 · 0 comments
Open

Inconsistent handling of associated constants in patterns #72602

sportzer opened this issue May 26, 2020 · 0 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-patterns Relating to patterns and pattern matching C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sportzer
Copy link

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

@sportzer sportzer added the C-bug Category: This is a bug. label May 26, 2020
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) A-patterns Relating to patterns and pattern matching T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-patterns Relating to patterns and pattern matching C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants