Higher ranked types in trait bounds result in confusing diagnostics #124474
Open
Description
opened on Apr 28, 2024
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
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)Area: Lifetimes / regionsDiagnostics: Confusing error or lint that should be reworked.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Activity