]> git.lizzy.rs Git - rust.git/commitdiff
Avoid suggesting self in visibility spec
authorDániel Buga <bugadani@gmail.com>
Sat, 11 Dec 2021 16:32:15 +0000 (17:32 +0100)
committerDániel Buga <bugadani@gmail.com>
Sun, 12 Dec 2021 10:14:10 +0000 (11:14 +0100)
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
compiler/rustc_resolve/src/late/diagnostics.rs
src/test/ui/suggestions/suggest-add-self.rs [new file with mode: 0644]
src/test/ui/suggestions/suggest-add-self.stderr [new file with mode: 0644]

index 72ba3f7b980cbc464de015724d1e2d31d27e7db0..f3891723e97b767b752fc7817b5b6e1b7d4cc7a5 100644 (file)
@@ -298,11 +298,16 @@ pub(crate) fn smart_resolve_report_errors(
                             .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 (file)
index 0000000..40692c8
--- /dev/null
@@ -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 (file)
index 0000000..a5e8f93
--- /dev/null
@@ -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`.