Provide a better error message for function pointers with generic parameters #103487
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-parser
Area: The parsing of Rust source code to an AST
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Newcomers might attempt to write a “generic” function pointer like
fn<T>(T) -> bool
. In such case, the compiler should provide a better error message. Inspired by a recent Reddit thread I sadly cannot find anymore.fn<'a>
Given the following code:
The compiler should mention that function pointers may not be generic or […] may not have generic parameters, suggest to move the lifetime(s) to a
for<>
parameter list (this should be machine-applicable in my judgement) and be able to recover:For later reference, the current output is:
fn<T>
/fn<const C: T>
Given the following code:
The compiler should mention that function pointers may not be generic or […] may not have generic parameters and be able to recover emitting two diagnostics, one for each line.
The compiler could also suggest moving the generic parameter list further up to the owning function, struct, etc. if available. Note that this might not be what the user intended (since there is a semantic difference between hypothetical higher-ranked generics and normal ones) and therefore shouldn't be machine-applicable.
For later reference, the current output is:
@rustbot label A-parser A-suggestion-diagnostics D-newcomer-roadblock
The text was updated successfully, but these errors were encountered: