From 248fe949afbebb0df59d84d17b0e45762965238f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 17 Apr 2019 17:22:16 -0700 Subject: [PATCH 1/3] Change suggestion of field when not in self context --- src/librustc_resolve/diagnostics.rs | 22 ++++++++++--------- src/test/ui/class-missing-self.stderr | 2 +- src/test/ui/issues/issue-60057.rs | 17 ++++++++++++++ src/test/ui/issues/issue-60057.stderr | 20 +++++++++++++++++ src/test/ui/resolve/issue-14254.stderr | 8 +++---- src/test/ui/resolve/issue-2356.stderr | 12 +++------- .../resolve/resolve-assoc-suggestions.stderr | 2 +- .../resolve-speculative-adjustment.stderr | 2 +- .../unresolved_static_type_field.stderr | 5 +---- 9 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 src/test/ui/issues/issue-60057.rs create mode 100644 src/test/ui/issues/issue-60057.stderr diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 9e3894dab0d..6821921e454 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -166,16 +166,18 @@ impl<'a> Resolver<'a> { 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 => { diff --git a/src/test/ui/class-missing-self.stderr b/src/test/ui/class-missing-self.stderr index ec11f125399..25cb85dadb9 100644 --- a/src/test/ui/class-missing-self.stderr +++ b/src/test/ui/class-missing-self.stderr @@ -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 index 00000000000..3027d01c532 --- /dev/null +++ b/src/test/ui/issues/issue-60057.rs @@ -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 index 00000000000..0bee87a4372 --- /dev/null +++ b/src/test/ui/issues/issue-60057.stderr @@ -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`. diff --git a/src/test/ui/resolve/issue-14254.stderr b/src/test/ui/resolve/issue-14254.stderr index 783b3addfc2..97d42aa8ef4 100644 --- a/src/test/ui/resolve/issue-14254.stderr +++ b/src/test/ui/resolve/issue-14254.stderr @@ -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 diff --git a/src/test/ui/resolve/issue-2356.stderr b/src/test/ui/resolve/issue-2356.stderr index 01dff5dd82f..10fcbfc74f2 100644 --- a/src/test/ui/resolve/issue-2356.stderr +++ b/src/test/ui/resolve/issue-2356.stderr @@ -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 diff --git a/src/test/ui/resolve/resolve-assoc-suggestions.stderr b/src/test/ui/resolve/resolve-assoc-suggestions.stderr index fd97ce09d4a..87040015b8d 100644 --- a/src/test/ui/resolve/resolve-assoc-suggestions.stderr +++ b/src/test/ui/resolve/resolve-assoc-suggestions.stderr @@ -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 diff --git a/src/test/ui/resolve/resolve-speculative-adjustment.stderr b/src/test/ui/resolve/resolve-speculative-adjustment.stderr index c4a6ced1ca4..892b50309a9 100644 --- a/src/test/ui/resolve/resolve-speculative-adjustment.stderr +++ b/src/test/ui/resolve/resolve-speculative-adjustment.stderr @@ -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 diff --git a/src/test/ui/resolve/unresolved_static_type_field.stderr b/src/test/ui/resolve/unresolved_static_type_field.stderr index a5173243787..7008fadad81 100644 --- a/src/test/ui/resolve/unresolved_static_type_field.stderr +++ b/src/test/ui/resolve/unresolved_static_type_field.stderr @@ -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 From 7b050fe831b7cf49a3b3290992a2421990f81834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 18 Apr 2019 09:43:15 -0700 Subject: [PATCH 2/3] review comments: change wording --- src/librustc_resolve/diagnostics.rs | 2 +- src/test/ui/issues/issue-60057.stderr | 2 +- src/test/ui/resolve/issue-2356.stderr | 4 ++-- src/test/ui/resolve/unresolved_static_type_field.stderr | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 6821921e454..9b02f98164f 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -176,7 +176,7 @@ impl<'a> Resolver<'a> { } else { err.span_label( span, - format!("a field by this name exists in `Self::{}`", path_str), + "a field by this name exists in `Self`", ); } } diff --git a/src/test/ui/issues/issue-60057.stderr b/src/test/ui/issues/issue-60057.stderr index 0bee87a4372..bbe16d04919 100644 --- a/src/test/ui/issues/issue-60057.stderr +++ b/src/test/ui/issues/issue-60057.stderr @@ -2,7 +2,7 @@ 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` + | ^^^^^^ a field by this name exists in `Self` error[E0425]: cannot find value `banana` in this scope --> $DIR/issue-60057.rs:14:21 diff --git a/src/test/ui/resolve/issue-2356.stderr b/src/test/ui/resolve/issue-2356.stderr index 10fcbfc74f2..7790383843e 100644 --- a/src/test/ui/resolve/issue-2356.stderr +++ b/src/test/ui/resolve/issue-2356.stderr @@ -20,7 +20,7 @@ error[E0425]: cannot find value `whiskers` in this scope --> $DIR/issue-2356.rs:39:5 | LL | whiskers -= other; - | ^^^^^^^^ a field by this name exists in `Self::whiskers` + | ^^^^^^^^ a field by this name exists in `Self` error[E0425]: cannot find function `shave` in this scope --> $DIR/issue-2356.rs:41:5 @@ -86,7 +86,7 @@ error[E0425]: cannot find value `whiskers` in this scope --> $DIR/issue-2356.rs:84:5 | LL | whiskers = 4; - | ^^^^^^^^ a field by this name exists in `Self::whiskers` + | ^^^^^^^^ a field by this name exists in `Self` error[E0425]: cannot find function `purr_louder` in this scope --> $DIR/issue-2356.rs:86:5 diff --git a/src/test/ui/resolve/unresolved_static_type_field.stderr b/src/test/ui/resolve/unresolved_static_type_field.stderr index 7008fadad81..06926b53ddd 100644 --- a/src/test/ui/resolve/unresolved_static_type_field.stderr +++ b/src/test/ui/resolve/unresolved_static_type_field.stderr @@ -2,7 +2,7 @@ error[E0425]: cannot find value `cx` in this scope --> $DIR/unresolved_static_type_field.rs:9:11 | LL | f(cx); - | ^^ a field by this name exists in `Self::cx` + | ^^ a field by this name exists in `Self` error: aborting due to previous error From 2f4035d27eaebf24370303e1dadedf3bf148a4af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 18 Apr 2019 18:57:16 -0700 Subject: [PATCH 3/3] Fix rebase --- src/test/ui/issues/issue-60057.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/issues/issue-60057.stderr b/src/test/ui/issues/issue-60057.stderr index bbe16d04919..6b967204ce6 100644 --- a/src/test/ui/issues/issue-60057.stderr +++ b/src/test/ui/issues/issue-60057.stderr @@ -16,5 +16,5 @@ error[E0601]: `main` function not found in crate `issue_60057` error: aborting due to 3 previous errors -Some errors occurred: E0425, E0601. +Some errors have detailed explanations: E0425, E0601. For more information about an error, try `rustc --explain E0425`.