Skip to content

Suggest adding Fn bound when calling a generic parameter #144193

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Kobzol
Copy link
Member

@Kobzol Kobzol commented Jul 19, 2025

The goal of the PR is to provide this diagnostic hint:

fn foo<T>(t: T) {
    let x: u32 = t(1);
}
//vvv
fn foo<T: Fn(i32) -> u32>(t: T) {
    let x: u32 = t(1);
}

I had to provide some input for suggest_restricting_param_bound, but I wasn't sure where to get the corresponding Fn trait for, because here it didn't seem like the code was already checking for trait impl matches, unlike e.g. in check_overloaded_binop, where the suggest function is also used. With the help of Claude, I managed to create the trait ref somehow, but I'm not at all sure if it's the right way to do (in particular around Binder::dummy).

The suggest_restricting_param_bound change is also super hacky - maybe there's a way to "attach" the associated Output type to the Fn trait ref, so that we don't have to manually render the output type?

@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Jul 19, 2025
@Kobzol Kobzol added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Jul 19, 2025
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

r? compiler-errors @bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jul 20, 2025

📌 Commit 9ff1376 has been approved by compiler-errors

It is now in the queue for this repository.

@rustbot rustbot assigned compiler-errors and unassigned davidtwco Jul 20, 2025
@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 Jul 20, 2025
@fmease
Copy link
Member

fmease commented Jul 20, 2025

CI is red (not just spellcheck) @bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 20, 2025
@compiler-errors
Copy link
Member

oops :D

@Kobzol Kobzol force-pushed the generic-param-fn-bound-help branch from 9ff1376 to 4f25693 Compare July 21, 2025 12:28
@Kobzol
Copy link
Member Author

Kobzol commented Jul 21, 2025

|

  • help: consider restricting type parameter U with trait Fn
  • |
  • LL | fn foo<U: Fn() -> _>(t: U) {

The suggestion here is valid, but since the return type is unknown (-> _), the suggestion ofc won't compile. And since we use MachineApplicable for this, maybe we should skip the return value if it is an unknown type? I tried to do that (not sure if ty.has_infer() is the right way to detect _ though).

@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 Jul 21, 2025
@Kobzol Kobzol force-pushed the generic-param-fn-bound-help branch from 4f25693 to a756da7 Compare July 21, 2025 12:32
@Kobzol Kobzol force-pushed the generic-param-fn-bound-help branch from a756da7 to 6f1c20d Compare July 21, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants