about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/errors.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-11-14 19:26:16 +0100
committerGitHub <noreply@github.com>2022-11-14 19:26:16 +0100
commita86bdb4c507f9f9651f7372075eb17009c7da4a6 (patch)
treef93cdfdf9d7c99308c71992682066e83b1828878 /compiler/rustc_parse/src/errors.rs
parent8c77da87d7451f275259c43c89ac3c9c24c1c8c8 (diff)
parentc2b906ba9a29591536042be4bbf8d0156225bb73 (diff)
downloadrust-a86bdb4c507f9f9651f7372075eb17009c7da4a6.tar.gz
rust-a86bdb4c507f9f9651f7372075eb17009c7da4a6.zip
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
Diffstat (limited to 'compiler/rustc_parse/src/errors.rs')
-rw-r--r--compiler/rustc_parse/src/errors.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs
index d59982f7063..a39398950a5 100644
--- a/compiler/rustc_parse/src/errors.rs
+++ b/compiler/rustc_parse/src/errors.rs
@@ -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,
+}