-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest considering casting fn item as fn pointer in more cases #133382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
//@ edition: 2021 | ||
mu001999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn foo() {} | ||
|
||
fn main() { | ||
let _: Vec<(&str, fn())> = [("foo", foo)].into_iter().collect(); //~ ERROR | ||
let _: Vec<fn()> = [foo].into_iter().collect(); //~ ERROR | ||
let _: Vec<fn()> = Vec::from([foo]); //~ ERROR | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/ui/did_you_mean/casting-fn-item-to-fn-pointer.stderr
This file contains hidden or 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,59 @@ | ||
error[E0277]: a value of type `Vec<(&str, fn())>` cannot be built from an iterator over elements of type `(&str, fn() {foo})` | ||
--> $DIR/casting-fn-item-to-fn-pointer.rs:6:59 | ||
| | ||
LL | let _: Vec<(&str, fn())> = [("foo", foo)].into_iter().collect(); | ||
| ^^^^^^^ value of type `Vec<(&str, fn())>` cannot be built from `std::iter::Iterator<Item=(&str, fn() {foo})>` | ||
| | ||
= help: the trait `FromIterator<(&_, fn() {foo})>` is not implemented for `Vec<(&str, fn())>` | ||
but trait `FromIterator<(&_, fn())>` is implemented for it | ||
= help: for that trait implementation, expected `fn()`, found `fn() {foo}` | ||
= note: fn items are distinct from fn pointers | ||
= help: consider casting the fn item to a fn pointer: `foo as fn()` | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/casting-fn-item-to-fn-pointer.rs:6:47 | ||
| | ||
LL | let _: Vec<(&str, fn())> = [("foo", foo)].into_iter().collect(); | ||
| -------------- ^^^^^^^^^^^ `Iterator::Item` is `(&str, fn() {foo})` here | ||
| | | ||
| this expression has type `[(&str, fn() {foo}); 1]` | ||
note: required by a bound in `collect` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
|
||
error[E0277]: a value of type `Vec<fn()>` cannot be built from an iterator over elements of type `fn() {foo}` | ||
--> $DIR/casting-fn-item-to-fn-pointer.rs:7:42 | ||
| | ||
LL | let _: Vec<fn()> = [foo].into_iter().collect(); | ||
| ^^^^^^^ value of type `Vec<fn()>` cannot be built from `std::iter::Iterator<Item=fn() {foo}>` | ||
| | ||
= help: the trait `FromIterator<fn() {foo}>` is not implemented for `Vec<fn()>` | ||
but trait `FromIterator<fn()>` is implemented for it | ||
= help: for that trait implementation, expected `fn()`, found `fn() {foo}` | ||
= note: fn items are distinct from fn pointers | ||
= help: consider casting the fn item to a fn pointer: `foo as fn()` | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/casting-fn-item-to-fn-pointer.rs:7:30 | ||
| | ||
LL | let _: Vec<fn()> = [foo].into_iter().collect(); | ||
| ----- ^^^^^^^^^^^ `Iterator::Item` is `fn() {foo}` here | ||
| | | ||
| this expression has type `[fn() {foo}; 1]` | ||
note: required by a bound in `collect` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/casting-fn-item-to-fn-pointer.rs:8:24 | ||
| | ||
LL | let _: Vec<fn()> = Vec::from([foo]); | ||
| --------- ^^^^^^^^^^^^^^^^ expected `Vec<fn()>`, found `Vec<fn() {foo}>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected struct `Vec<fn()>` | ||
found struct `Vec<fn() {foo}>` | ||
= note: fn items are distinct from fn pointers | ||
= help: consider casting the fn item to a fn pointer: `foo as fn()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0308. | ||
For more information about an error, try `rustc --explain E0277`. |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.