-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add note when
FnPtr
vs. FnDef
impl trait
I encountered an instance where an `FnPtr` implemented a trait, but I was passing an `FnDef`. To the end user, there is really no way to differentiate each of them, but it is necessary to cast to the generic function in order to compile. It is thus useful to suggest `as` in the help note, (even if the Fn output implements the trait).
- Loading branch information
1 parent
c0b8735
commit 2de9d67
Showing
4 changed files
with
111 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// There are two different instances to check that even if | ||
// the trait is implemented for the output of a function, | ||
// it will still be displayed if the function itself implements a trait. | ||
trait Foo {} | ||
|
||
impl Foo for fn() -> bool {} | ||
impl Foo for bool {} | ||
|
||
fn example() -> bool { | ||
true | ||
} | ||
|
||
trait NoOtherFoo {} | ||
|
||
impl NoOtherFoo for fn() -> bool {} | ||
|
||
fn do_on_foo(v: impl Foo) {} | ||
fn do_on_single_foo(v: impl NoOtherFoo) {} | ||
|
||
fn main() { | ||
do_on_foo(example); | ||
//~^ ERROR the trait bound | ||
|
||
do_on_single_foo(example); | ||
//~^ ERROR the trait bound | ||
} |
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,43 @@ | ||
error[E0277]: the trait bound `fn() -> bool {example}: Foo` is not satisfied | ||
--> $DIR/fn-trait-cast-diagnostic.rs:21:15 | ||
| | ||
LL | do_on_foo(example); | ||
| --------- ^^^^^^^ the trait `Foo` is not implemented for fn item `fn() -> bool {example}` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `do_on_foo` | ||
--> $DIR/fn-trait-cast-diagnostic.rs:17:22 | ||
| | ||
LL | fn do_on_foo(v: impl Foo) {} | ||
| ^^^ required by this bound in `do_on_foo` | ||
help: use parentheses to call this function | ||
| | ||
LL | do_on_foo(example()); | ||
| ++ | ||
help: the trait `Foo` is implemented for fn pointer `fn() -> bool`, try casting using `as` | ||
| | ||
LL | do_on_foo(example as fn() -> bool); | ||
| +++++++++++++++ | ||
|
||
error[E0277]: the trait bound `fn() -> bool {example}: NoOtherFoo` is not satisfied | ||
--> $DIR/fn-trait-cast-diagnostic.rs:24:22 | ||
| | ||
LL | do_on_single_foo(example); | ||
| ---------------- ^^^^^^^ the trait `NoOtherFoo` is not implemented for fn item `fn() -> bool {example}` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `do_on_single_foo` | ||
--> $DIR/fn-trait-cast-diagnostic.rs:18:29 | ||
| | ||
LL | fn do_on_single_foo(v: impl NoOtherFoo) {} | ||
| ^^^^^^^^^^ required by this bound in `do_on_single_foo` | ||
help: the trait `NoOtherFoo` is implemented for fn pointer `fn() -> bool`, try casting using `as` | ||
| | ||
LL | do_on_single_foo(example as fn() -> bool); | ||
| +++++++++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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