-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
Conversation
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
This comment has been minimized.
This comment has been minimized.
r? compiler-errors @bors r+ rollup
CI is red (not just spellcheck) @bors r-
oops :D
9ff1376
to
4f25693
Compare
|
- help: consider restricting type parameter
U
with traitFn
- |
- 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
4f25693
to
a756da7
Compare
a756da7
to
6f1c20d
Compare
@rustbot ready
Hey errs 👋 Just wanted to ask if I should reroll? I know you have a lot of PRs assigned.
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
@bors r+
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-
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
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
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.
Uh oh!
There was an error while loading. Please reload this page.
The goal of the PR is to provide this diagnostic hint:
I had to provide some input for
suggest_restricting_param_bound
, but I wasn't sure where to get the correspondingFn
trait for, because here it didn't seem like the code was already checking for trait impl matches, unlike e.g. incheck_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 aroundBinder::dummy
).The
suggest_restricting_param_bound
change is also super hacky - maybe there's a way to "attach" the associatedOutput
type to theFn
trait ref, so that we don't have to manually render the output type?