-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #104223 - fmease:recover-fn-ptr-with-generics, r=este…
…bank 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
- Loading branch information
Showing
6 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
fn main() { | ||
type Predicate = fn<'a>(&'a str) -> bool; | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
type Identity = fn<T>(T) -> T; | ||
//~^ ERROR function pointer types may not have generic parameters | ||
//~| ERROR cannot find type `T` in this scope | ||
//~| ERROR cannot find type `T` in this scope | ||
|
||
let _: fn<const N: usize, 'e, Q, 'f>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
let _: for<'outer> fn<'inner>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
let _: for<> fn<'r>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
type Hmm = fn<>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
let _: extern fn<'a: 'static>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
//~| ERROR lifetime bounds cannot be used in this context | ||
|
||
let _: for<'any> extern "C" fn<'u>(); | ||
//~^ ERROR function pointer types may not have generic parameters | ||
|
||
type QuiteBroken = fn<const>(); | ||
//~^ ERROR expected identifier, found `>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:2:24 | ||
| | ||
LL | type Predicate = fn<'a>(&'a str) -> bool; | ||
| ^^^^ | ||
| | ||
help: consider moving the lifetime parameter to a `for` parameter list | ||
| | ||
LL - type Predicate = fn<'a>(&'a str) -> bool; | ||
LL + type Predicate = for<'a> fn(&'a str) -> bool; | ||
| | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:5:23 | ||
| | ||
LL | type Identity = fn<T>(T) -> T; | ||
| ^^^ | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:10:14 | ||
| | ||
LL | let _: fn<const N: usize, 'e, Q, 'f>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider moving the lifetime parameters to a `for` parameter list | ||
| | ||
LL - let _: fn<const N: usize, 'e, Q, 'f>(); | ||
LL + let _: for<'e, 'f> fn(); | ||
| | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:13:26 | ||
| | ||
LL | let _: for<'outer> fn<'inner>(); | ||
| ^^^^^^^^ | ||
| | ||
help: consider moving the lifetime parameter to the `for` parameter list | ||
| | ||
LL - let _: for<'outer> fn<'inner>(); | ||
LL + let _: for<'outer, 'inner> fn(); | ||
| | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:16:20 | ||
| | ||
LL | let _: for<> fn<'r>(); | ||
| ^^^^ | ||
| | ||
help: consider moving the lifetime parameter to the `for` parameter list | ||
| | ||
LL - let _: for<> fn<'r>(); | ||
LL + let _: for<'r> fn(); | ||
| | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:19:18 | ||
| | ||
LL | type Hmm = fn<>(); | ||
| ^^ | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:22:21 | ||
| | ||
LL | let _: extern fn<'a: 'static>(); | ||
| ^^^^^^^^^^^^^ | ||
| | ||
help: consider moving the lifetime parameter to a `for` parameter list | ||
| | ||
LL - let _: extern fn<'a: 'static>(); | ||
LL + let _: for<'a> extern fn(); | ||
| | ||
|
||
error: function pointer types may not have generic parameters | ||
--> $DIR/recover-fn-ptr-with-generics.rs:26:35 | ||
| | ||
LL | let _: for<'any> extern "C" fn<'u>(); | ||
| ^^^^ | ||
| | ||
help: consider moving the lifetime parameter to the `for` parameter list | ||
| | ||
LL - let _: for<'any> extern "C" fn<'u>(); | ||
LL + let _: for<'any, 'u> extern "C" fn(); | ||
| | ||
|
||
error: expected identifier, found `>` | ||
--> $DIR/recover-fn-ptr-with-generics.rs:29:32 | ||
| | ||
LL | type QuiteBroken = fn<const>(); | ||
| ^ expected identifier | ||
|
||
error: lifetime bounds cannot be used in this context | ||
--> $DIR/recover-fn-ptr-with-generics.rs:22:26 | ||
| | ||
LL | let _: extern fn<'a: 'static>(); | ||
| ^^^^^^^ | ||
|
||
error[E0412]: cannot find type `T` in this scope | ||
--> $DIR/recover-fn-ptr-with-generics.rs:5:27 | ||
| | ||
LL | type Identity = fn<T>(T) -> T; | ||
| ^ not found in this scope | ||
|
||
error[E0412]: cannot find type `T` in this scope | ||
--> $DIR/recover-fn-ptr-with-generics.rs:5:33 | ||
| | ||
LL | type Identity = fn<T>(T) -> T; | ||
| ^ not found in this scope | ||
|
||
error: aborting due to 12 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0412`. |