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

Higher ranked types in trait bounds result in confusing diagnostics #124474

Open
WaffleLapkin opened this issue Apr 28, 2024 · 0 comments
Open

Higher ranked types in trait bounds result in confusing diagnostics #124474

WaffleLapkin opened this issue Apr 28, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

@WaffleLapkin
Copy link
Member

Code

trait Trait {}

impl Trait for for<'a> fn(&'a u32) {}

fn f() where for<'a> fn(&'a u32): Trait {}

fn main() {
    f();
}

Current output

error: implementation of `Trait` is not general enough
 --> ./t.rs:9:5
  |
9 |     f();
  |     ^^^ implementation of `Trait` is not general enough
  |
  = note: `for<'a> fn(&'a u32)` must implement `Trait`, for any lifetime `'0`...
  = note: ...but `Trait` is actually implemented for the type `for<'a> fn(&'a u32)`

Desired output

error: implementation of `Trait` is not general enough
 --> ./t.rs:9:5
  |
9 |     f();
  |     ^^^ implementation of `Trait` is not general enough
  |
  = note: `fn(&'0 u32)` must implement `Trait`, for any lifetime `'0`...
  = note: ...but `Trait` is actually implemented for the type `for<'a> fn(&'a u32)`
  = note: `where for<'a> fn(&'a u32): Trait` is parsed as `where for<'a> (fn(&'a u32): Trait)`, not `where (for<'a> fn(&'a u32)): Trait`

Rationale and extra context

for<'a> fn(&'a u32) in the note makes it seem like it is right (as it's the same as the type for which the trait is implemented), when we actually want to use the '0. Also it's a good idea to mention how the for binders in where clauses are parsed, since this is not that intuitive (the user could have literally pasted the type from the trait impl).

Other cases

No response

Rust Version

rustc 1.79.0-nightly (ef8b9dcf2 2024-04-24)
binary: rustc
commit-hash: ef8b9dcf23700f2e2265317611460d3a65c19eff
commit-date: 2024-04-24
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Anything else?

No response

@WaffleLapkin WaffleLapkin added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

1 participant