about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-12-14 20:47:30 +0100
committerGitHub <noreply@github.com>2021-12-14 20:47:30 +0100
commit6a9491895f3aaa37f8b83c24ce01f97ff4716e45 (patch)
tree0ed715169f20c458dcb97c6c3d88ffd4f44dca3c
parenta38d668296476d5b7c44eb9131a468277bb95767 (diff)
parentf3a08fd8e78b567342d1f42cc17315eddea9b1e7 (diff)
downloadrust-6a9491895f3aaa37f8b83c24ce01f97ff4716e45.tar.gz
rust-6a9491895f3aaa37f8b83c24ce01f97ff4716e45.zip
Rollup merge of #91798 - bugadani:issue-91783, r=michaelwoerister
Avoid suggest adding `self` in visibility spec

Fixes #91783
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs7
-rw-r--r--src/test/ui/suggestions/suggest-add-self.rs15
-rw-r--r--src/test/ui/suggestions/suggest-add-self.stderr29
3 files changed, 50 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index feb4f82ce8d..3e1afdfa9a5 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -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",
                                 )
diff --git a/src/test/ui/suggestions/suggest-add-self.rs b/src/test/ui/suggestions/suggest-add-self.rs
new file mode 100644
index 00000000000..40692c8df20
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-add-self.rs
@@ -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() {}
diff --git a/src/test/ui/suggestions/suggest-add-self.stderr b/src/test/ui/suggestions/suggest-add-self.stderr
new file mode 100644
index 00000000000..a5e8f93deb6
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-add-self.stderr
@@ -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`.