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

Provide a better error message for function pointers with generic parameters #103487

Closed
fmease opened this issue Oct 24, 2022 · 1 comment · Fixed by #104223
Closed

Provide a better error message for function pointers with generic parameters #103487

fmease opened this issue Oct 24, 2022 · 1 comment · Fixed by #104223
Assignees
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.

Comments

@fmease
Copy link
Member

fmease commented Oct 24, 2022

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:

fn main() {
    let _: fn<'a>(&'a str) -> bool;
}

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:

- fn<'a>(&'a str) -> bool
+ for<'a> fn(&'a str) -> bool

For later reference, the current output is:

error: expected `(`, found `<`
 --> src/main.rs:2:14
  |
2 |     let _: fn<'a>(&'a str) -> bool;
  |         -    ^ expected `(`
  |         |
  |         while parsing the type for `_`

fn<T> / fn<const C: T>

Given the following code:

fn main() {
    let _: fn<T>(T) -> bool;
    let _: fn<const N: usize>([u8; N]);
}

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:

error: expected `(`, found `<`
 --> src/main.rs:2:14
  |
2 |     let _: fn<T>(T) -> bool;
  |         -    ^ expected `(`
  |         |
  |         while parsing the type for `_`

@rustbot label A-parser A-suggestion-diagnostics D-newcomer-roadblock

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 24, 2022
@rustbot rustbot added 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. labels Oct 24, 2022
@fmease
Copy link
Member Author

fmease commented Oct 27, 2022

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 14, 2022
…s, r=estebank

Recover from function pointer types with generic parameter list

Give a more helpful error when encountering function pointer types with a generic parameter list like `fn<'a>(&'a str) -> bool` or `fn<T>(T) -> T` and suggest moving lifetime parameters to a `for<>` parameter list.

I've added a bunch of extra code to properly handle (unlikely?) corner cases like `for<'a> fn<'b>()` (where there already exists a `for<>` parameter list) correctly suggesting `for<'a, 'b> fn()` (merging the lists). If you deem this useless, I can simplify the code by suggesting nothing at all in this case.

I am quite open to suggestions regarding the wording of the diagnostic messages.

Fixes rust-lang#103487.
`@rustbot` label A-diagnostics
r? diagnostics
@bors bors closed this as completed in a86bdb4 Nov 15, 2022
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-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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants