forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
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 rust-lang#133382 - mu001999-contrib:diag/fnitem, r=lcnr Suggest considering casting fn item as fn pointer in more cases Fixes rust-lang#132648
- Loading branch information
Showing
8 changed files
with
114 additions
and
6 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
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,9 @@ | ||
//@ edition: 2021 | ||
|
||
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 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 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