Rollup merge of #106176 - compiler-errors:fn-kw-as-fn-trait, r=estebank

Recover `fn` keyword as `Fn` trait in bounds

`impl fn()` -> `impl Fn()`

Fixes #82515
This commit is contained in:
Matthias Krüger 2022-12-28 14:40:01 +01:00 committed by GitHub
commit d07be1a304
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 148 additions and 79 deletions

View file

@ -4,21 +4,13 @@ fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
//~^ ERROR expected identifier, found keyword `fn`
//~| ERROR expected identifier, found keyword `fn`
//~| ERROR expected identifier, found keyword `fn`
//~| ERROR cannot find trait `r#fn` in this scope
//~| ERROR cannot find trait `r#fn` in this scope
//~| ERROR cannot find trait `r#fn` in this scope
//~| HELP a trait with a similar name exists
//~| HELP a trait with a similar name exists
//~| HELP a trait with a similar name exists
//~| HELP escape `fn` to use it as an identifier
//~| HELP escape `fn` to use it as an identifier
//~| HELP escape `fn` to use it as an identifier
//~| HELP use `Fn` to refer to the trait
//~| HELP use `Fn` to refer to the trait
//~| HELP use `Fn` to refer to the trait
where
G: fn(),
//~^ ERROR expected identifier, found keyword `fn`
//~| ERROR cannot find trait `r#fn` in this scope
//~| HELP a trait with a similar name exists
//~| HELP escape `fn` to use it as an identifier
//~| HELP use `Fn` to refer to the trait
{}
fn _g<A: struct, B>(_: impl struct, _: &dyn struct)

View file

@ -2,48 +2,48 @@ error: expected identifier, found keyword `fn`
--> $DIR/kw-in-trait-bounds.rs:3:10
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ expected identifier, found keyword
| ^^
|
help: escape `fn` to use it as an identifier
help: use `Fn` to refer to the trait
|
LL | fn _f<F: r#fn(), G>(_: impl fn(), _: &dyn fn())
| ++
LL | fn _f<F: Fn(), G>(_: impl fn(), _: &dyn fn())
| ~~
error: expected identifier, found keyword `fn`
--> $DIR/kw-in-trait-bounds.rs:3:27
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ expected identifier, found keyword
| ^^
|
help: escape `fn` to use it as an identifier
help: use `Fn` to refer to the trait
|
LL | fn _f<F: fn(), G>(_: impl r#fn(), _: &dyn fn())
| ++
LL | fn _f<F: fn(), G>(_: impl Fn(), _: &dyn fn())
| ~~
error: expected identifier, found keyword `fn`
--> $DIR/kw-in-trait-bounds.rs:3:41
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ expected identifier, found keyword
| ^^
|
help: escape `fn` to use it as an identifier
help: use `Fn` to refer to the trait
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn r#fn())
| ++
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn Fn())
| ~~
error: expected identifier, found keyword `fn`
--> $DIR/kw-in-trait-bounds.rs:17:4
--> $DIR/kw-in-trait-bounds.rs:11:4
|
LL | G: fn(),
| ^^ expected identifier, found keyword
| ^^
|
help: escape `fn` to use it as an identifier
help: use `Fn` to refer to the trait
|
LL | G: r#fn(),
| ++
LL | G: Fn(),
| ~~
error: expected identifier, found keyword `struct`
--> $DIR/kw-in-trait-bounds.rs:24:10
--> $DIR/kw-in-trait-bounds.rs:16:10
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ expected identifier, found keyword
@ -54,7 +54,7 @@ LL | fn _g<A: r#struct, B>(_: impl struct, _: &dyn struct)
| ++
error: expected identifier, found keyword `struct`
--> $DIR/kw-in-trait-bounds.rs:24:29
--> $DIR/kw-in-trait-bounds.rs:16:29
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ expected identifier, found keyword
@ -65,7 +65,7 @@ LL | fn _g<A: struct, B>(_: impl r#struct, _: &dyn struct)
| ++
error: expected identifier, found keyword `struct`
--> $DIR/kw-in-trait-bounds.rs:24:45
--> $DIR/kw-in-trait-bounds.rs:16:45
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ expected identifier, found keyword
@ -76,7 +76,7 @@ LL | fn _g<A: struct, B>(_: impl struct, _: &dyn r#struct)
| ++
error: expected identifier, found keyword `struct`
--> $DIR/kw-in-trait-bounds.rs:38:8
--> $DIR/kw-in-trait-bounds.rs:30:8
|
LL | B: struct,
| ^^^^^^ expected identifier, found keyword
@ -86,44 +86,8 @@ help: escape `struct` to use it as an identifier
LL | B: r#struct,
| ++
error[E0405]: cannot find trait `r#fn` in this scope
--> $DIR/kw-in-trait-bounds.rs:3:10
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
= note: similarly named trait `Fn` defined here
error[E0405]: cannot find trait `r#fn` in this scope
--> $DIR/kw-in-trait-bounds.rs:17:4
|
LL | G: fn(),
| ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
= note: similarly named trait `Fn` defined here
error[E0405]: cannot find trait `r#fn` in this scope
--> $DIR/kw-in-trait-bounds.rs:3:27
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
= note: similarly named trait `Fn` defined here
error[E0405]: cannot find trait `r#fn` in this scope
--> $DIR/kw-in-trait-bounds.rs:3:41
|
LL | fn _f<F: fn(), G>(_: impl fn(), _: &dyn fn())
| ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
= note: similarly named trait `Fn` defined here
error[E0405]: cannot find trait `r#struct` in this scope
--> $DIR/kw-in-trait-bounds.rs:24:10
--> $DIR/kw-in-trait-bounds.rs:16:10
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct`
@ -132,7 +96,7 @@ LL | trait Struct {}
| ------------ similarly named trait `Struct` defined here
error[E0405]: cannot find trait `r#struct` in this scope
--> $DIR/kw-in-trait-bounds.rs:38:8
--> $DIR/kw-in-trait-bounds.rs:30:8
|
LL | B: struct,
| ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct`
@ -141,7 +105,7 @@ LL | trait Struct {}
| ------------ similarly named trait `Struct` defined here
error[E0405]: cannot find trait `r#struct` in this scope
--> $DIR/kw-in-trait-bounds.rs:24:29
--> $DIR/kw-in-trait-bounds.rs:16:29
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct`
@ -150,7 +114,7 @@ LL | trait Struct {}
| ------------ similarly named trait `Struct` defined here
error[E0405]: cannot find trait `r#struct` in this scope
--> $DIR/kw-in-trait-bounds.rs:24:45
--> $DIR/kw-in-trait-bounds.rs:16:45
|
LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
| ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct`
@ -158,6 +122,6 @@ LL | fn _g<A: struct, B>(_: impl struct, _: &dyn struct)
LL | trait Struct {}
| ------------ similarly named trait `Struct` defined here
error: aborting due to 16 previous errors
error: aborting due to 12 previous errors
For more information about this error, try `rustc --explain E0405`.

View file

@ -0,0 +1,12 @@
fn foo(_: impl fn() -> i32) {}
//~^ ERROR expected identifier, found keyword `fn`
fn foo2<T: fn(i32)>(_: T) {}
//~^ ERROR expected identifier, found keyword `fn`
fn main() {
foo(|| ());
//~^ mismatched types
foo2(|_: ()| {});
//~^ type mismatch in closure arguments
}

View file

@ -0,0 +1,48 @@
error: expected identifier, found keyword `fn`
--> $DIR/recover-fn-trait-from-fn-kw.rs:1:16
|
LL | fn foo(_: impl fn() -> i32) {}
| ^^
|
help: use `Fn` to refer to the trait
|
LL | fn foo(_: impl Fn() -> i32) {}
| ~~
error: expected identifier, found keyword `fn`
--> $DIR/recover-fn-trait-from-fn-kw.rs:4:12
|
LL | fn foo2<T: fn(i32)>(_: T) {}
| ^^
|
help: use `Fn` to refer to the trait
|
LL | fn foo2<T: Fn(i32)>(_: T) {}
| ~~
error[E0308]: mismatched types
--> $DIR/recover-fn-trait-from-fn-kw.rs:8:12
|
LL | foo(|| ());
| ^^ expected `i32`, found `()`
error[E0631]: type mismatch in closure arguments
--> $DIR/recover-fn-trait-from-fn-kw.rs:10:5
|
LL | foo2(|_: ()| {});
| ^^^^ ------- found signature defined here
| |
| expected due to this
|
= note: expected closure signature `fn(i32) -> _`
found closure signature `fn(()) -> _`
note: required by a bound in `foo2`
--> $DIR/recover-fn-trait-from-fn-kw.rs:4:12
|
LL | fn foo2<T: fn(i32)>(_: T) {}
| ^^^^^^^ required by this bound in `foo2`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0308, E0631.
For more information about an error, try `rustc --explain E0308`.