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

use structured suggestion for method calls #57291

Merged
merged 1 commit into from Jan 6, 2019

Conversation

Projects
None yet
4 participants
@euclio
Copy link
Contributor

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

@estebank
Copy link
Contributor

estebank left a comment

Looks good to me.

Show resolved Hide resolved src/librustc_typeck/check/mod.rs Outdated
| ^^^^^^^
|
= help: maybe a `()` to call it is missing?
| ^^^^^^^ help: use parentheses to call the method: `collect()`

This comment has been minimized.

@estebank

estebank Jan 2, 2019

Contributor

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.

This comment has been minimized.

@euclio

euclio Jan 3, 2019

Contributor

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?

This comment has been minimized.

@estebank

estebank Jan 3, 2019

Contributor

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

This comment has been minimized.

@euclio

euclio Jan 3, 2019

Contributor

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?

This comment has been minimized.

@estebank

estebank Jan 3, 2019

Contributor

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

This comment has been minimized.

@estebank

estebank Jan 2, 2019

Contributor

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 :)

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.

@euclio euclio force-pushed the euclio:method-call-suggestion branch from 121d891 to e3fe0ee Jan 4, 2019

@euclio

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

estebank commented Jan 4, 2019

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 4, 2019

📌 Commit e3fe0ee has been approved by estebank

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 6, 2019

⌛️ Testing commit e3fe0ee with merge e628196...

bors added a commit that referenced this pull request Jan 6, 2019

Auto merge of #57291 - euclio:method-call-suggestion, r=estebank
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

This comment has been minimized.

Copy link
Contributor

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

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details

@euclio euclio deleted the euclio:method-call-suggestion branch Jan 6, 2019

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