]> git.lizzy.rs Git - rust.git/commitdiff
Change suggestion of field when not in self context
authorEsteban Küber <esteban@kuber.com.ar>
Thu, 18 Apr 2019 00:22:16 +0000 (17:22 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 19 Apr 2019 01:39:56 +0000 (18:39 -0700)
src/librustc_resolve/diagnostics.rs
src/test/ui/class-missing-self.stderr
src/test/ui/issues/issue-60057.rs [new file with mode: 0644]
src/test/ui/issues/issue-60057.stderr [new file with mode: 0644]
src/test/ui/resolve/issue-14254.stderr
src/test/ui/resolve/issue-2356.stderr
src/test/ui/resolve/resolve-assoc-suggestions.stderr
src/test/ui/resolve/resolve-speculative-adjustment.stderr
src/test/ui/resolve/unresolved_static_type_field.stderr

index 9e3894dab0da0b40f1bc418edc006725a9385cd0..6821921e45467026898799f122bb2d76e3c067d8 100644 (file)
@@ -166,16 +166,18 @@ pub(crate) fn smart_resolve_report_errors(
                 let self_is_available = self.self_value_is_available(path[0].ident.span, span);
                 match candidate {
                     AssocSuggestion::Field => {
-                        err.span_suggestion(
-                            span,
-                            "try",
-                            format!("self.{}", path_str),
-                            Applicability::MachineApplicable,
-                        );
-                        if !self_is_available {
-                            err.span_label(span, format!("`self` value is a keyword \
-                                                         only available in \
-                                                         methods with `self` parameter"));
+                        if self_is_available {
+                            err.span_suggestion(
+                                span,
+                                "you might have meant to use the available field",
+                                format!("self.{}", path_str),
+                                Applicability::MachineApplicable,
+                            );
+                        } else {
+                            err.span_label(
+                                span,
+                                format!("a field by this name exists in `Self::{}`", path_str),
+                            );
                         }
                     }
                     AssocSuggestion::MethodWithSelf if self_is_available => {
index ec11f1253990f0ff542d7b2bd0dafd035e1944c0..25cb85dadb903dcae01f9193a74aad1b6d130157 100644 (file)
@@ -2,7 +2,7 @@ error[E0425]: cannot find value `meows` in this scope
   --> $DIR/class-missing-self.rs:9:7
    |
 LL |       meows += 1;
-   |       ^^^^^ help: try: `self.meows`
+   |       ^^^^^ help: you might have meant to use the available field: `self.meows`
 
 error[E0425]: cannot find function `sleep` in this scope
   --> $DIR/class-missing-self.rs:10:7
diff --git a/src/test/ui/issues/issue-60057.rs b/src/test/ui/issues/issue-60057.rs
new file mode 100644 (file)
index 0000000..3027d01
--- /dev/null
@@ -0,0 +1,17 @@
+struct A {
+    banana: u8,
+}
+
+impl A {
+    fn new(peach: u8) -> A {
+        A {
+            banana: banana //~ ERROR cannot find value `banana` in this scope
+        }
+    }
+
+    fn foo(&self, peach: u8) -> A {
+        A {
+            banana: banana //~ ERROR cannot find value `banana` in this scope
+        }
+    }
+}
diff --git a/src/test/ui/issues/issue-60057.stderr b/src/test/ui/issues/issue-60057.stderr
new file mode 100644 (file)
index 0000000..0bee87a
--- /dev/null
@@ -0,0 +1,20 @@
+error[E0425]: cannot find value `banana` in this scope
+  --> $DIR/issue-60057.rs:8:21
+   |
+LL |             banana: banana
+   |                     ^^^^^^ a field by this name exists in `Self::banana`
+
+error[E0425]: cannot find value `banana` in this scope
+  --> $DIR/issue-60057.rs:14:21
+   |
+LL |             banana: banana
+   |                     ^^^^^^ help: you might have meant to use the available field: `self.banana`
+
+error[E0601]: `main` function not found in crate `issue_60057`
+   |
+   = note: consider adding a `main` function to `$DIR/issue-60057.rs`
+
+error: aborting due to 3 previous errors
+
+Some errors occurred: E0425, E0601.
+For more information about an error, try `rustc --explain E0425`.
index 783b3addfc2ecf9967fe4c7b632debfb06767244..97d42aa8ef4a5e166b61bdfeeda38744b682d445 100644 (file)
@@ -20,13 +20,13 @@ error[E0425]: cannot find value `x` in this scope
   --> $DIR/issue-14254.rs:30:9
    |
 LL |         x;
-   |         ^ help: try: `self.x`
+   |         ^ help: you might have meant to use the available field: `self.x`
 
 error[E0425]: cannot find value `y` in this scope
   --> $DIR/issue-14254.rs:32:9
    |
 LL |         y;
-   |         ^ help: try: `self.y`
+   |         ^ help: you might have meant to use the available field: `self.y`
 
 error[E0425]: cannot find value `a` in this scope
   --> $DIR/issue-14254.rs:34:9
@@ -56,13 +56,13 @@ error[E0425]: cannot find value `x` in this scope
   --> $DIR/issue-14254.rs:47:9
    |
 LL |         x;
-   |         ^ help: try: `self.x`
+   |         ^ help: you might have meant to use the available field: `self.x`
 
 error[E0425]: cannot find value `y` in this scope
   --> $DIR/issue-14254.rs:49:9
    |
 LL |         y;
-   |         ^ help: try: `self.y`
+   |         ^ help: you might have meant to use the available field: `self.y`
 
 error[E0425]: cannot find value `a` in this scope
   --> $DIR/issue-14254.rs:51:9
index 01dff5dd82fd4eaf4c0054737325c6200fa7116c..10fcbfc74f2591d168c08dfa3e72f07ceeac4a71 100644 (file)
@@ -20,10 +20,7 @@ error[E0425]: cannot find value `whiskers` in this scope
   --> $DIR/issue-2356.rs:39:5
    |
 LL |     whiskers -= other;
-   |     ^^^^^^^^
-   |     |
-   |     `self` value is a keyword only available in methods with `self` parameter
-   |     help: try: `self.whiskers`
+   |     ^^^^^^^^ a field by this name exists in `Self::whiskers`
 
 error[E0425]: cannot find function `shave` in this scope
   --> $DIR/issue-2356.rs:41:5
@@ -83,16 +80,13 @@ error[E0425]: cannot find value `whiskers` in this scope
   --> $DIR/issue-2356.rs:79:5
    |
 LL |     whiskers = 0;
-   |     ^^^^^^^^ help: try: `self.whiskers`
+   |     ^^^^^^^^ help: you might have meant to use the available field: `self.whiskers`
 
 error[E0425]: cannot find value `whiskers` in this scope
   --> $DIR/issue-2356.rs:84:5
    |
 LL |     whiskers = 4;
-   |     ^^^^^^^^
-   |     |
-   |     `self` value is a keyword only available in methods with `self` parameter
-   |     help: try: `self.whiskers`
+   |     ^^^^^^^^ a field by this name exists in `Self::whiskers`
 
 error[E0425]: cannot find function `purr_louder` in this scope
   --> $DIR/issue-2356.rs:86:5
index fd97ce09d4a7083d3c2628bf6470fdf4654bdb37..87040015b8d805add6bd2b422e4429b97420abc5 100644 (file)
@@ -14,7 +14,7 @@ error[E0425]: cannot find value `field` in this scope
   --> $DIR/resolve-assoc-suggestions.rs:20:9
    |
 LL |         field;
-   |         ^^^^^ help: try: `self.field`
+   |         ^^^^^ help: you might have meant to use the available field: `self.field`
 
 error[E0412]: cannot find type `Type` in this scope
   --> $DIR/resolve-assoc-suggestions.rs:23:16
index c4a6ced1ca4c099b8b8585384b3d57dd0179d53b..892b50309a905daeab7848418c6196bb9332729b 100644 (file)
@@ -14,7 +14,7 @@ error[E0425]: cannot find value `field` in this scope
   --> $DIR/resolve-speculative-adjustment.rs:23:9
    |
 LL |         field;
-   |         ^^^^^ help: try: `self.field`
+   |         ^^^^^ help: you might have meant to use the available field: `self.field`
 
 error[E0425]: cannot find function `method` in this scope
   --> $DIR/resolve-speculative-adjustment.rs:25:9
index a5173243787726492b5f9f2e89ddb616c42e6e87..7008fadad8170f845c8f799c77b8824bd5128470 100644 (file)
@@ -2,10 +2,7 @@ error[E0425]: cannot find value `cx` in this scope
   --> $DIR/unresolved_static_type_field.rs:9:11
    |
 LL |         f(cx);
-   |           ^^
-   |           |
-   |           `self` value is a keyword only available in methods with `self` parameter
-   |           help: try: `self.cx`
+   |           ^^ a field by this name exists in `Self::cx`
 
 error: aborting due to previous error