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

Wrong position of inserted &self in suggestion of compile error #91783

Closed
rhysd opened this issue Dec 11, 2021 · 2 comments · Fixed by #91798
Closed

Wrong position of inserted &self in suggestion of compile error #91783

rhysd opened this issue Dec 11, 2021 · 2 comments · Fixed by #91798
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@rhysd
Copy link
Contributor

rhysd commented Dec 11, 2021

I tried this code:

struct X(i32);

impl X {
    pub(crate) fn f() {
        self.0
    }
}

fn main() {}

I expected to see this happen: Correct compile error due to lack of self receiver.

Instead, this happened: Compile error happens. But its suggestion is not correct:

error[E0424]: expected value, found module `self`
 --> bar.rs:5:9
  |
4 |     pub(crate) fn f() {
  |                   - this function doesn't have a `self` parameter
5 |         self.0
  |         ^^^^ `self` value is a keyword only available in methods with a `self` parameter
  |
help: add a `self` receiver parameter to make the associated `fn` a method
  |
4 |     pub(&selfcrate) fn f() {
  |         +++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0424`.

&self is inserted to just after first (. But in this case, the first ( is not for method parameter. So the position is not correct.

4 |     pub(&selfcrate) fn f() {

This line should be:

4 |     pub(crate) fn f(&self) {

Meta

rustc --version --verbose:

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-apple-darwin
release: 1.57.0
LLVM version: 13.0.0
@rhysd rhysd added the C-bug Category: This is a bug. label Dec 11, 2021
@rhysd
Copy link
Contributor Author

rhysd commented Dec 11, 2021

I think the logic here causes this issue:

let (span, sugg) = fn_kind
.decl()
.inputs
.get(0)
.map(|p| (p.span.shrink_to_lo(), "&self, "))
.unwrap_or_else(|| {
(
self.r
.session
.source_map()
.span_through_char(*span, '(')
.shrink_to_hi(),
"&self",
)
});

@hkmatsumoto hkmatsumoto added A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 11, 2021
@bugadani
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2021
…ister

Avoid suggest adding `self` in visibility spec

Fixes rust-lang#91783
@bors bors closed this as completed in 6a94918 Dec 15, 2021
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 C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants