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

Avoid suggest adding self in visibility spec #91798

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
.get(0)
.map(|p| (p.span.shrink_to_lo(), "&self, "))
.unwrap_or_else(|| {
// Try to look for the "(" after the function name, if possible.
// This avoids placing the suggestion into the visibility specifier.
let span = fn_kind
.ident()
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
(
self.r
.session
.source_map()
.span_through_char(*span, '(')
.span_through_char(span, '(')
.shrink_to_hi(),
"&self",
)
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/suggest-add-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct X(i32);

impl X {
pub(crate) fn f() {
self.0
//~^ ERROR expected value, found module `self`
}

pub fn g() {
self.0
//~^ ERROR expected value, found module `self`
}
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/suggestions/suggest-add-self.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0424]: expected value, found module `self`
--> $DIR/suggest-add-self.rs:5:9
|
LL | pub(crate) fn f() {
| - this function doesn't have a `self` parameter
LL | 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
|
LL | pub(crate) fn f(&self) {
| +++++

error[E0424]: expected value, found module `self`
--> $DIR/suggest-add-self.rs:10:9
|
LL | pub fn g() {
| - this function doesn't have a `self` parameter
LL | 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
|
LL | pub fn g(&self) {
| +++++

error: aborting due to 2 previous errors

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