Skip to content
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

"did you mean" for misusing a field as a method is misleading #38321

Closed
whitequark opened this issue Dec 12, 2016 · 11 comments
Closed

"did you mean" for misusing a field as a method is misleading #38321

whitequark opened this issue Dec 12, 2016 · 11 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@whitequark
Copy link
Member

whitequark commented Dec 12, 2016

The error is currently:

error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope
   --> src/wire/ipv4.rs:409:34
    |
409 |         packet.set_src_addr(self.src_addr());
    |                                  ^^^^^^^^
    |
note: did you mean to write `self.src_addr`?
   --> src/wire/ipv4.rs:409:34
    |
409 |         packet.set_src_addr(self.src_addr());
    |                                  ^^^^^^^^

which is of course absurd.

To reproduce:

struct Foo {
    src_addr: usize
}

fn main() {
    let foo = Foo { src_addr: 10 };
    foo.src_addr()
}
@KalitaAlexey
Copy link
Contributor

Why is it misleading?

@frewsxcv
Copy link
Member

frewsxcv commented Dec 12, 2016

@whitequark Are you able to share the project where you encountered this error?

@whitequark
Copy link
Member Author

Why is it misleading?

Because it suggests to write the same thing as is already written.

@frewsxcv
Copy link
Member

Why is it misleading?

Because it suggests to write the same thing as is already written.

The suggestion is telling you to write self.src_addr instead of self.src_addr(), since there is presumably a field (not a method) on your structure called src_addr.

@whitequark
Copy link
Member Author

whitequark commented Dec 12, 2016

The suggestion is telling you to write self.src_addr instead of self.src_addr(), since there is presumably a field (not a method) on your structure called src_addr.

It points to src_addr (not src_addr()) and suggests to write src_addr. This makes no sense. It should at least highlight the parens so that it points to src_addr().

@whitequark
Copy link
Member Author

I've added a minimal repro to the issue text.

@frewsxcv frewsxcv added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 12, 2016
@whitequark
Copy link
Member Author

Also, ideally it would highlight the entire self.src_addr() expression, not just the single token, since that's what it talks about in the error anyhow.

@KalitaAlexey
Copy link
Contributor

I've got. It's a span problem.

@TheZoq2
Copy link
Contributor

TheZoq2 commented Jan 3, 2017

I just ran into this aswell. I agree that it would be more clear if it highlighted the whole src_addr() instead of just src_addr but adding a note saying something along the lines of

There is no method called src_addr, did you mean to access the field src_addr?

Would be even more helpfull

@zackmdavis
Copy link
Member

Yes, I found this confusing, too. I should submit a PR to fix this tomorrow (have code fix, but still need to correct compile-fail test expectations, but even more than that, need to sleep).

@zackmdavis
Copy link
Member

okay, not literally tomorrow, but soon

zackmdavis added a commit to zackmdavis/rust that referenced this issue Feb 1, 2017
The use of the `span_note` diagnostic method resulted in error messages
that some users (including the present author) found very confusing: the
code snippet and highlighted span displayed with the "did you mean" note
was easy to interpret as a suggested edit (as if it had been set by
`span_suggestion`), but was in fact identical to the snippet/span
displayed just above, indicating the error in the original code, making
it look as if the compiler was suggesting a no-op change.

To remedy this, we use `help` instead of `span_note`, and reword one of
the affected messages to emphasize the concept of accessing a field
being different from calling a method (when the message just said, "did
you mean to write `self.foo`", it was not immediately obvious that this
was meant in contrast to `self.foo()`, with method-call parens).

Resolves rust-lang#38321.
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 29, 2017
Clarify suggetion for field used as method

Instead of

```rust
error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope
   --> src/wire/ipv4.rs:409:34
    |
409 |         packet.set_src_addr(self.src_addr());
    |                                  ^^^^^^^^
    |
note: did you mean to write `self.src_addr`?
   --> src/wire/ipv4.rs:409:34
    |
409 |         packet.set_src_addr(self.src_addr());
    |                                  ^^^^^^^^
```

present

```rust
error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope
   --> src/wire/ipv4.rs:409:34
    |
409 |         packet.set_src_addr(self.src_addr());
    |                                  ^^^^^^^^ field, not a method
    |
    = help: did you mean to write `self.src_addr` instead of `self.src_addr(...)`?
```

Fix rust-lang#38321.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants