Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Kobzol wants to merge 1 commit into rust-lang:master
base: master
Choose a base branch
Loading
from Kobzol:generic-param-fn-bound-help

Conversation

Copy link
Member

@Kobzol Kobzol commented Jul 19, 2025
edited
Loading

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?

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

This comment has been minimized.

Copy link
Member

r? compiler-errors @bors r+ rollup

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.

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

Kobzol commented Jul 24, 2025

@rustbot ready

Copy link
Member Author

Kobzol commented Aug 6, 2025

Hey errs 👋 Just wanted to ask if I should reroll? I know you have a lot of PRs assigned.

Copy link
Member

compiler-errors commented Aug 6, 2025
edited
Loading

Yeah, actually now that I think of it, I don't think this is the right way of doing it. I don't think we should be creating a new trait ref just to call into the existing code for suggestions -- this probably should be integrated in a more first class way.

I'm on vacation anyways so I will reroll.

@rustbot author
r? compiler

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

@bors r+

Copy link
Collaborator

bors commented Sep 1, 2025

📌 Commit 6f1c20d has been approved by SparrowLii

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 1, 2025
Copy link
Member

Yeah, actually now that I think of it, I don't think this is the right way of doing it. I don't think we should be creating a new trait ref just to call into the existing code for suggestions -- this probably should be integrated in a more first class way.

I'm on vacation anyways so I will reroll.

@rustbot author r? compiler

Ah sry that didn't get this in time

@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 Sep 1, 2025
Copy link
Member

@SparrowLii SparrowLii left a comment
edited
Loading

Choose a reason for hiding this comment

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

I guess the first step should be enhancing the diagnostics when a user has add the Fn-like traits with misused of parameters. Like this:

fn foo<F: Fn(u32, u32) -> u32>(f: F) -> u32 {
 f(1)
}

We can consider suggestions for the Fn-like traits based on scenarios with smaller distance of current implementation.

View changes since this review

.collect();
let args_tuple = Ty::new_tup(self.tcx(), &arg_types);

let fn_def_id = self.tcx().require_lang_item(LangItem::Fn, callee_expr.span);
Copy link
Member

@SparrowLii SparrowLii Sep 1, 2025
edited
Loading

Choose a reason for hiding this comment

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

There several function traits like Fn, Fnmut or FnOnce. It's hard to know which one is most suitable.

Copy link
Member Author

Choose a reason for hiding this comment

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

My original use-case was that there is no bound at all. In that case, we really can't guess much, I suppose, so using Fn as the most related option seemed reasonable to me.

Copy link
Member Author

Kobzol commented Sep 1, 2025

I think that the diagnostics for that are already pretty good:

 Compiling playground v0.0.1 (/playground)
error[E0057]: this function takes 2 arguments but 1 argument was supplied
 --> src/main.rs:2:5
 |
2 | f(1)
 | ^--- argument #2 of type `u32` is missing
 |
note: callable defined here
 --> src/main.rs:1:11
 |
1 | fn foo<F: Fn(u32, u32) -> u32>(f: F) -> u32 {
 | ^^^^^^^^^^^^^^^^^^^
help: provide the argument
 |
2 | f(1, /* u32 */)
 | +++++++++++

I was specifically aiming at the use-case where the bound doesn't contain Fn already.

Copy link
Member

SparrowLii commented Sep 1, 2025
edited
Loading

I think that the diagnostics for that are already pretty good:

 Compiling playground v0.0.1 (/playground)
error[E0057]: this function takes 2 arguments but 1 argument was supplied
 --> src/main.rs:2:5
 |
2 | f(1)
 | ^--- argument #2 of type `u32` is missing
 |
note: callable defined here
 --> src/main.rs:1:11
 |
1 | fn foo<F: Fn(u32, u32) -> u32>(f: F) -> u32 {
 | ^^^^^^^^^^^^^^^^^^^
help: provide the argument
 |
2 | f(1, /* u32 */)
 | +++++++++++

I was specifically aiming at the use-case where the bound doesn't contain Fn already.

We can think of suggesting F: Fn(u32) -> u32 instead of providing the argument here. However, this suggestion probably be inappropriate, as it is very likely to be a typeck error in statements where foo are called.

So maybe we can lift the suggestion of this PR to statements which calling foo and use type of the argument be provided. In this way we may avoid to make a new trait_ref

Copy link
Member Author

Kobzol commented Sep 3, 2025

So maybe we can lift the suggestion of this PR to statements which calling foo and use type of the argument be provided. In this way we may avoid to make a new trait_ref

Sorry, I didn't understand that suggestions. Nor do I fully grok what Michael originally meant 😆 I probably don't have enough experience with this subsystem to suggest a more "first-class" solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@compiler-errors compiler-errors compiler-errors approved these changes

@SparrowLii SparrowLii SparrowLii left review comments

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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /