-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add help message about function pointers #105552
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
+273
−96
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
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 |
---|---|---|
@@ -1,53 +1,52 @@ | ||
// Test that the types of distinct fn items are not compatible by | ||
// default. See also `run-pass/fn-item-type-*.rs`. | ||
|
||
fn foo<T>(x: isize) -> isize { x * 2 } | ||
fn bar<T>(x: isize) -> isize { x * 4 } | ||
fn foo<T>(x: isize) -> isize { | ||
x * 2 | ||
} | ||
fn bar<T>(x: isize) -> isize { | ||
x * 4 | ||
} | ||
|
||
fn eq<T>(x: T, y: T) { } | ||
fn eq<T>(x: T, y: T) {} | ||
|
||
trait Foo { fn foo() { /* this is a default fn */ } } | ||
impl<T> Foo for T { /* `foo` is still default here */ } | ||
trait Foo { | ||
fn foo() { /* this is a default fn */ | ||
} | ||
} | ||
impl<T> Foo for T { | ||
/* `foo` is still default here */ | ||
} | ||
|
||
fn main() { | ||
eq(foo::<u8>, bar::<u8>); | ||
//~^ ERROR mismatched types | ||
//~| expected fn item `fn(_) -> _ {foo::<u8>}` | ||
//~| found fn item `fn(_) -> _ {bar::<u8>}` | ||
//~| expected fn item, found a different fn item | ||
//~| different `fn` items always have unique types, even if their signatures are the same | ||
//~| change the expected type to be function pointer | ||
//~| if the expected type is due to type inference, cast the expected `fn` to a function pointer | ||
//~| different fn items have unique types, even if their signatures are the same | ||
|
||
eq(foo::<u8>, foo::<i8>); | ||
//~^ ERROR mismatched types | ||
//~| expected `u8`, found `i8` | ||
//~| different `fn` items always have unique types, even if their signatures are the same | ||
//~| change the expected type to be function pointer | ||
//~| if the expected type is due to type inference, cast the expected `fn` to a function pointer | ||
//~| different fn items have unique types, even if their signatures are the same | ||
|
||
eq(bar::<String>, bar::<Vec<u8>>); | ||
//~^ ERROR mismatched types | ||
//~| found fn item `fn(_) -> _ {bar::<Vec<u8>>}` | ||
//~| expected struct `String`, found struct `Vec` | ||
//~| different `fn` items always have unique types, even if their signatures are the same | ||
//~| change the expected type to be function pointer | ||
//~| if the expected type is due to type inference, cast the expected `fn` to a function pointer | ||
//~| different fn items have unique types, even if their signatures are the same | ||
|
||
// Make sure we distinguish between trait methods correctly. | ||
eq(<u8 as Foo>::foo, <u16 as Foo>::foo); | ||
//~^ ERROR mismatched types | ||
//~| expected `u8`, found `u16` | ||
//~| different `fn` items always have unique types, even if their signatures are the same | ||
//~| change the expected type to be function pointer | ||
//~| if the expected type is due to type inference, cast the expected `fn` to a function pointer | ||
//~| different fn items have unique types, even if their signatures are the same | ||
|
||
eq(foo::<u8>, bar::<u8> as fn(isize) -> isize); | ||
//~^ ERROR mismatched types | ||
//~| found fn pointer `fn(_) -> _` | ||
//~| expected fn item, found fn pointer | ||
//~| change the expected type to be function pointer | ||
//~| if the expected type is due to type inference, cast the expected `fn` to a function pointer | ||
|
||
eq(foo::<u8> as fn(isize) -> isize, bar::<u8>); // ok! | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/fn-item-type.rs:13:19 | ||
--> $DIR/fn-item-type.rs:22:19 | ||
| | ||
LL | eq(foo::<u8>, bar::<u8>); | ||
| -- ^^^^^^^^^ expected fn item, found a different fn item | ||
|
@@ -8,17 +8,15 @@ LL | eq(foo::<u8>, bar::<u8>); | |
| | ||
= note: expected fn item `fn(_) -> _ {foo::<u8>}` | ||
found fn item `fn(_) -> _ {bar::<u8>}` | ||
= note: different `fn` items always have unique types, even if their signatures are the same | ||
= help: change the expected type to be function pointer `fn(isize) -> isize` | ||
= help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
= note: different fn items have unique types, even if their signatures are the same | ||
note: function defined here | ||
--> $DIR/fn-item-type.rs:7:4 | ||
--> $DIR/fn-item-type.rs:11:4 | ||
| | ||
LL | fn eq<T>(x: T, y: T) { } | ||
LL | fn eq<T>(x: T, y: T) {} | ||
| ^^ ---- | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/fn-item-type.rs:22:19 | ||
--> $DIR/fn-item-type.rs:29:19 | ||
| | ||
LL | eq(foo::<u8>, foo::<i8>); | ||
| -- ^^^^^^^^^ expected `u8`, found `i8` | ||
|
@@ -27,17 +25,15 @@ LL | eq(foo::<u8>, foo::<i8>); | |
| | ||
= note: expected fn item `fn(_) -> _ {foo::<u8>}` | ||
found fn item `fn(_) -> _ {foo::<i8>}` | ||
= note: different `fn` items always have unique types, even if their signatures are the same | ||
= help: change the expected type to be function pointer `fn(isize) -> isize` | ||
= help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` | ||
= note: different fn items have unique types, even if their signatures are the same | ||
note: function defined here | ||
--> $DIR/fn-item-type.rs:7:4 | ||
--> $DIR/fn-item-type.rs:11:4 | ||
| | ||
LL | fn eq<T>(x: T, y: T) { } | ||
LL | fn eq<T>(x: T, y: T) {} | ||
| ^^ ---- | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/fn-item-type.rs:29:23 | ||
--> $DIR/fn-item-type.rs:34:23 | ||
| | ||
LL | eq(bar::<String>, bar::<Vec<u8>>); | ||
| -- ^^^^^^^^^^^^^^ expected struct `String`, found struct `Vec` | ||
|
@@ -46,17 +42,15 @@ LL | eq(bar::<String>, bar::<Vec<u8>>); | |
| | ||
= note: expected fn item `fn(_) -> _ {bar::<String>}` | ||
found fn item `fn(_) -> _ {bar::<Vec<u8>>}` | ||
= note: different `fn` items always have unique types, even if their signatures are the same | ||
= help: change the expected type to be function pointer `fn(isize) -> isize` | ||
= help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `bar::<String> as fn(isize) -> isize` | ||
= note: different fn items have unique types, even if their signatures are the same | ||
note: function defined here | ||
--> $DIR/fn-item-type.rs:7:4 | ||
--> $DIR/fn-item-type.rs:11:4 | ||
| | ||
LL | fn eq<T>(x: T, y: T) { } | ||
LL | fn eq<T>(x: T, y: T) {} | ||
| ^^ ---- | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/fn-item-type.rs:38:26 | ||
--> $DIR/fn-item-type.rs:41:26 | ||
| | ||
LL | eq(<u8 as Foo>::foo, <u16 as Foo>::foo); | ||
| -- ^^^^^^^^^^^^^^^^^ expected `u8`, found `u16` | ||
|
@@ -65,17 +59,15 @@ LL | eq(<u8 as Foo>::foo, <u16 as Foo>::foo); | |
| | ||
= note: expected fn item `fn() {<u8 as Foo>::foo}` | ||
found fn item `fn() {<u16 as Foo>::foo}` | ||
= note: different `fn` items always have unique types, even if their signatures are the same | ||
= help: change the expected type to be function pointer `fn()` | ||
= help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `<u8 as Foo>::foo as fn()` | ||
= note: different fn items have unique types, even if their signatures are the same | ||
note: function defined here | ||
--> $DIR/fn-item-type.rs:7:4 | ||
--> $DIR/fn-item-type.rs:11:4 | ||
| | ||
LL | fn eq<T>(x: T, y: T) { } | ||
LL | fn eq<T>(x: T, y: T) {} | ||
| ^^ ---- | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/fn-item-type.rs:45:19 | ||
--> $DIR/fn-item-type.rs:46:19 | ||
| | ||
LL | eq(foo::<u8>, bar::<u8> as fn(isize) -> isize); | ||
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn item, found fn pointer | ||
|
@@ -84,12 +76,11 @@ LL | eq(foo::<u8>, bar::<u8> as fn(isize) -> isize); | |
| | ||
= note: expected fn item `fn(_) -> _ {foo::<u8>}` | ||
found fn pointer `fn(_) -> _` | ||
= help: change the expected type to be function pointer `fn(isize) -> isize` | ||
= help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
= note: fn items are distinct from fn pointers | ||
note: function defined here | ||
--> $DIR/fn-item-type.rs:7:4 | ||
--> $DIR/fn-item-type.rs:11:4 | ||
| | ||
LL | fn eq<T>(x: T, y: T) { } | ||
LL | fn eq<T>(x: T, y: T) {} | ||
| ^^ ---- | ||
|
||
error: aborting due to 5 previous errors | ||
|
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.