1
Fork 0

Rollup merge of #104223 - fmease:recover-fn-ptr-with-generics, 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 #103487.
``@rustbot`` label A-diagnostics
r? diagnostics
This commit is contained in:
Matthias Krüger 2022-11-14 19:26:16 +01:00 committed by GitHub
commit a86bdb4c50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 228 additions and 3 deletions

View file

@ -1280,3 +1280,24 @@ pub(crate) struct DoubleColonInBound {
#[suggestion(code = ": ", applicability = "machine-applicable")]
pub between: Span,
}
#[derive(Diagnostic)]
#[diag(parser_fn_ptr_with_generics)]
pub(crate) struct FnPtrWithGenerics {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sugg: Option<FnPtrWithGenericsSugg>,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(suggestion, applicability = "maybe-incorrect")]
pub(crate) struct FnPtrWithGenericsSugg {
#[suggestion_part(code = "{snippet}")]
pub left: Span,
pub snippet: String,
#[suggestion_part(code = "")]
pub right: Span,
pub arity: usize,
pub for_param_list_exists: bool,
}