-
Notifications
You must be signed in to change notification settings - Fork 13.3k
use structured suggestion for method calls #57291
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
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.
Looks good to me.
| ^^^^^^^ | ||
| | ||
= help: maybe a `()` to call it is missing? | ||
| ^^^^^^^ help: use parentheses to call the method: `collect()` |
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.
The suggestions in
src/test/ui/did_you_mean/issue-40396.stderr
are suboptimal. I could check if the containing expression isBinOp
, but I'm not sure if that's general enough. Any ideas?
Though this is a case that will need to be accounted for specifically, as the ideal case would be for the parser to actually be able to parse type setting without the turbofish and emit a customized relevant suggestion to use the turbofish. If we do that, then this error will not trigger.
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.
How would I distinguish this case from something like
struct Foo;
impl Foo {
fn bar(&self) -> i32 {
0
}
}
fn main() {
let foo = 1;
Foo.bar<foo>();
}
where we should still apply the suggestion?
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.
What would be the "perfect" suggestion in that case? IMO, that's borderline "unrecoverable": the code is attempting (assumption 1) to set a type argument without using turbofish and (assumption 2) calling that method. Because this requires not one but two assumptions, the likelihood of a suggestion being incorrect increases. In this case we would emit the following, which I think is "good enough" (at least for now):
error: chained comparison operators require parentheses
--> src/main.rs:11:12
|
11 | Foo.bar<foo>();
| ^^^^^^
|
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
= help: or use `(...)` if you meant to specify fn arguments
error[E0615]: attempted to take value of method `bar` on type `Foo`
--> src/main.rs:11:9
|
11 | Foo.bar<foo>();
| ^^^ help: use parentheses to call the method: `bar()`
error[E0308]: mismatched types
--> src/main.rs:11:17
|
11 | Foo.bar<foo>();
| ^^ expected bool, found ()
|
= note: expected type `bool`
found type `()`
In an ideal compiler, we could keep around enough information about parse recoveries like the above and figure out if the call and boolean comparison looked like a missing turbofish, and effectively elide errors:
error: chained comparison operators require parentheses
--> src/main.rs:11:12
|
11 | Foo.bar<foo>();
| ^^^^^^
|
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
= help: or use `(...)` if you meant to specify fn arguments
error[E0615]: attempted to take value of method `bar` on type `Foo`
--> src/main.rs:11:9
|
11 | Foo.bar<foo>();
| ^^^^^^^^^^ help: it seems like you tried to use type arguments in a method that doesn't require them: `bar()`
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 suppose a better example is this:
struct Foo;
impl Foo {
fn bar(&self) -> i32 {
0
}
}
fn main() {
let Vec = 1;
Foo.bar < Vec;
}
The user is attempting to compare the result of a method call, but forgot the parens. If we don't emit the suggestion because it's parsed as part of a BinOp
<
, it would be incorrect. How can I distinguish this from a missing turbofish?
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.
We'd need to keep track of either NodeId
s or Span
s where the turbofish note due to chained comparison operators in a set owned by the parser. We then need to keep that arround in the Sess
. When finding this case in the type checker, we look back at the sess
to see if the place where we're about to suggest adding a ()
is covered by the turbofish suggestion or not. All of this should be done in a separate PR, as it might quickly become very involved.
| ^^^^^^^^^^ | ||
| | ||
= help: maybe a `()` to call it is missing? | ||
| ^^^^^^^^^^ help: use parentheses to call the method: `filter_map(...)` |
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.
It would be amazing if we inspected the arguments of filter_map
to suggest even more (like |item| { /**/ }
), but that's definitely part of the scope of this PR :)
Furthermore, don't suggest calling the method if it is part of a place expression, as this is invalid syntax.
121d891
to
e3fe0ee
Compare
@estebank Turned the |
@bors r+ |
📌 Commit e3fe0ee has been approved by |
use structured suggestion for method calls Furthermore, don't suggest calling the method if it is part of a place expression, as this is invalid syntax. I'm thinking it might be worth putting a label on the method assignment span like "this is a method" and removing the span from the "methods are immutable" text so it isn't reported twice. The suggestions in `src/test/ui/did_you_mean/issue-40396.stderr` are suboptimal. I could check if the containing expression is `BinOp`, but I'm not sure if that's general enough. Any ideas? r? @estebank
☀️ Test successful - status-appveyor, status-travis |
Furthermore, don't suggest calling the method if it is part of a place
expression, as this is invalid syntax.
I'm thinking it might be worth putting a label on the method assignment span like "this is a method" and removing the span from the "methods are immutable" text so it isn't reported twice.
The suggestions in
src/test/ui/did_you_mean/issue-40396.stderr
are suboptimal. I could check if the containing expression isBinOp
, but I'm not sure if that's general enough. Any ideas?r? @estebank