Skip to content
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

Add help message about function pointers #105552

Merged
merged 1 commit into from
Jan 25, 2023

Conversation

mattjperez
Copy link
Contributor

@mattjperez mattjperez commented Dec 11, 2022

Fix #102608

@rustbot
Copy link
Collaborator

rustbot commented Dec 11, 2022

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cjgillot (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 11, 2022
@mattjperez
Copy link
Contributor Author

r? rust-lang/diagnostics

@rustbot rustbot assigned TaKO8Ki and unassigned cjgillot Dec 11, 2022
@mattjperez
Copy link
Contributor Author

Ah, i forgot to run the test suite 😅.
Will fix it and ping the reviewer when it's ready.

@TaKO8Ki
Copy link
Member

TaKO8Ki commented Dec 11, 2022

@rustbot author

When you are ready for the review, please use @rustbot review :)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 11, 2022
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mattjperez mattjperez force-pushed the add-incompatible-types-note branch 3 times, most recently from 476adb8 to 59ebb65 Compare December 19, 2022 21:59
@mattjperez
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 19, 2022
@mattjperez mattjperez force-pushed the add-incompatible-types-note branch 2 times, most recently from 305abb8 to bfdfb85 Compare December 20, 2022 04:14
@compiler-errors
Copy link
Member

r? @compiler-errors I'll review this soon

Comment on lines 18 to 20
//~| 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did these notes go? Is there a good reason they disappeared?

Copy link
Contributor Author

@mattjperez mattjperez Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The note about fn items being unique is still there, however the rest of the deleted notes are making a suggestion that would not result in working code.

If you follow the advice of casting to function pointers (in this example), this is the result

error[E0308]: arguments to this function are incorrect
    --> cases/1.rs:19:5
     |
19   |     eq(foo::<u8> as fn(isize) -> isize, bar::<u8> as fn(isize) -> isize);
     |     ^^
     |
note: expected `*const _`, found fn pointer
    --> cases/1.rs:19:8
     |
19   |     eq(foo::<u8> as fn(isize) -> isize, bar::<u8> as fn(isize) -> isize);
     |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = note: expected raw pointer `*const _`
                 found fn pointer `fn(isize) -> isize`
note: expected `*const _`, found fn pointer
    --> cases/1.rs:19:41
     |
19   |     eq(foo::<u8> as fn(isize) -> isize, bar::<u8> as fn(isize) -> isize);
     |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = note: expected raw pointer `*const _`
                 found fn pointer `fn(isize) -> isize`
note: function defined here
    --> /home/mperez/projects/rustlang/library/core/src/ptr/mod.rs:1827:8
     |
1827 | pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
     |        ^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

So while the note might be useful for some cases, it wouldn't be a good suggestion in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll come up with a couple other similar cases real quick to see if it might be a good/useful suggestion there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm still having a hard time understanding why this suggestion is incorrect for this UI test. Why are you pointing at std::ptr::eq when the UI test defines its own fn eq<T>(x: T, y: T) {}?

For the record, this code works:

fn eq<T: Eq>(a: T, b: T) {}

fn a() {}
fn b() {}

fn main() {
    eq(a as fn(), b as fn());
    //   ^^^^^^^    ^^^^^^^
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right. I mistakenly didn't include the locally defined fn eq when verifying the output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can we bring this suggestion back then?

@mattjperez
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 21, 2023
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still pretty sad that we're not even vaguely suggesting to use as to turn a FnDef into a FnPtr. We should be nudging the user to do something even if it's not always a structured suggestion they can apply.

tests/ui/reify-intrinsic.stderr Show resolved Hide resolved
Comment on lines 18 to 20
//~| 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm still having a hard time understanding why this suggestion is incorrect for this UI test. Why are you pointing at std::ptr::eq when the UI test defines its own fn eq<T>(x: T, y: T) {}?

For the record, this code works:

fn eq<T: Eq>(a: T, b: T) {}

fn a() {}
fn b() {}

fn main() {
    eq(a as fn(), b as fn());
    //   ^^^^^^^    ^^^^^^^
}

@mattjperez mattjperez force-pushed the add-incompatible-types-note branch 2 times, most recently from 29caebf to a67f653 Compare January 24, 2023 15:52
@mattjperez
Copy link
Contributor Author

mattjperez commented Jan 24, 2023

@compiler-errors Please advise on

  • tests/ui/reify-intrinsic.stderr:8 : suggestion doesn't work
  • tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr: works without #[target_feature(enable = "sse2")]
  • tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr: works without #[target_feature(enable = "sse2")]

@mattjperez mattjperez force-pushed the add-incompatible-types-note branch 3 times, most recently from 9d3785b to 0e6e5d6 Compare January 24, 2023 17:37
- On compiler-error's suggestion of moving this lower down the stack,
along the path of `report_mismatched_types()`, which is used
by `rustc_hir_analysis` and `rustc_hir_typeck`.
- update ui tests, add test
- add suggestions for references to fn pointers
- modify `TypeErrCtxt::same_type_modulo_infer` to take `T: relate::Relate` instead of `Ty`
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now, though I left a few follow-up notes I'd like to see in a future PR, perhaps.

@@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When we have two FnDefs, we should mention that the user can use an as to turn them into pointers.

@@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When we expect a FnDef, and find a FnPtr, we should mention that the user can use an as to turn the def into a pointer. Since the span will be pointing at the ptr, not the def, this can't be a suggestion, but it's still worth noting.

@compiler-errors
Copy link
Member

I'm inclined to merge this as-is since it's been quite a lot of back-and-forth and I don't want to keep you waiting. If you're free, then please work on the follow-ups in an additional PR, and if you're not, let me know so I can make the changes myself 😃

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 24, 2023

📌 Commit 1e22280 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 24, 2023
@mattjperez
Copy link
Contributor Author

Not a problem! I appreciate all the help and patience you've given with this one. I can make the changes in a follow-up PR.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 25, 2023
Rollup of 9 pull requests

Successful merges:

 - rust-lang#105552 (Add help message about function pointers)
 - rust-lang#106583 (Suggest coercion of `Result` using `?`)
 - rust-lang#106767 (Allow setting CFG_DISABLE_UNSTABLE_FEATURES to 0)
 - rust-lang#106823 (Allow fmt::Arguments::as_str() to return more Some(_).)
 - rust-lang#107166 (rustc_metadata: Support non-`Option` nullable values in metadata tables)
 - rust-lang#107213 (Add suggestion to remove if in let..else block)
 - rust-lang#107223 (`sub_ptr()` is equivalent to `usize::try_from().unwrap_unchecked()`, not `usize::from().unwrap_unchecked()`)
 - rust-lang#107227 (`new_outside_solver` ->  `evaluate_root_goal`)
 - rust-lang#107232 (rustdoc: simplify settings popover DOM, CSS, JS)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c0930c4 into rust-lang:master Jan 25, 2023
@rustbot rustbot added this to the 1.69.0 milestone Jan 25, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 26, 2023
…s, r=compiler-errors

Improve fn pointer notes

continuation of rust-lang#105552

r? `@compiler-errors`
@mattjperez mattjperez deleted the add-incompatible-types-note branch January 26, 2023 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve diagnostics like "incompatible types: expected fn item, found a different fn item"
7 participants