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 · 1 comment
Open

Inconsistent handling of associated constants in patterns #72602

sportzer opened this issue May 26, 2020 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-patterns Relating to patterns and pattern matching A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types 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
@fmease fmease added T-types Relevant to the types team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-trait-system Area: Trait system labels Dec 21, 2024
@fmease
Copy link
Member

fmease commented Dec 21, 2024

Marking A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) because const_eval_resolve_for_typeck returns TooGeneric here in unevaluated_to_pat but ultimately it's a T-types / A-trait-system question. I haven't thought much about this issue, so maybe it's pretty obvious why we fail to normalize here but anyway I presume the difference is CandidateSource::Impl vs. CandidateSource::ParamEnv (the latter shadowing the former even though the obligation/goal could be normalized "immediately" via the user-impl candidate, cc: #24066 maybe) and therefore the const projection (UnevaluatedConst) is considered / stays rigidTooGeneric. Just a hunch.

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-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-patterns Relating to patterns and pattern matching A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants