Suggest adding Fn bound when calling a generic parameter#144193
Suggest adding Fn bound when calling a generic parameter#144193Kobzol wants to merge 1 commit intorust-lang:mainfrom
Fn bound when calling a generic parameter#144193Conversation
|
r? @davidtwco rustbot has assigned @davidtwco. Use |
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
The suggestion here is valid, but since the return type is unknown ( @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 |
|
@bors r+ |
Ah sry that didn't get this in time @bors r- |
There was a problem hiding this comment.
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.
| .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); |
There was a problem hiding this comment.
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.
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: I was specifically aiming at the use-case where the bound doesn't contain |
We can think of suggesting So maybe we can lift the suggestion of this PR to statements which calling |
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. |
|
☔ The latest upstream changes (presumably #145993) made this pull request unmergeable. Please resolve the merge conflicts. |
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 correspondingFntrait 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_boundchange is also super hacky - maybe there's a way to "attach" the associatedOutputtype to theFntrait ref, so that we don't have to manually render the output type?