Skip to content

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

Merged
merged 1 commit into from
Jan 6, 2019

Conversation

euclio
Copy link
Contributor

@euclio euclio commented Jan 2, 2019

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 2, 2019
Copy link
Contributor

@estebank estebank left a 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()`
Copy link
Contributor

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 is BinOp, 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.

Copy link
Contributor Author

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?

Copy link
Contributor

@estebank estebank Jan 3, 2019

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()`

Copy link
Contributor Author

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?

Copy link
Contributor

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 NodeIds or Spans 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(...)`
Copy link
Contributor

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.
@euclio euclio force-pushed the method-call-suggestion branch from 121d891 to e3fe0ee Compare January 4, 2019 15:21
@euclio
Copy link
Contributor Author

euclio commented Jan 4, 2019

@estebank Turned the span_help into a help. Leaving the other comments to be addressed in a future PR.

@estebank
Copy link
Contributor

estebank commented Jan 4, 2019

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 4, 2019

📌 Commit e3fe0ee has been approved by estebank

@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 Jan 4, 2019
@bors
Copy link
Collaborator

bors commented Jan 6, 2019

⌛ Testing commit e3fe0ee with merge e628196...

bors added a commit that referenced this pull request Jan 6, 2019
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
@bors
Copy link
Collaborator

bors commented Jan 6, 2019

☀️ Test successful - status-appveyor, status-travis
Approved by: estebank
Pushing e628196 to master...

@bors bors merged commit e3fe0ee into rust-lang:master Jan 6, 2019
@euclio euclio deleted the method-call-suggestion branch January 6, 2019 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants