about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-08-09 21:02:58 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-08-10 12:04:10 -0700
commit089810a1cb28cfb0262b62f1d36c75ea790b0739 (patch)
treec41c13a294beb77c7e4f2354cdb2b6770fc6f2d3
parenteef284be59bbbe50e4d5369542a80e1ccd2ba7ab (diff)
downloadrust-089810a1cb28cfb0262b62f1d36c75ea790b0739.tar.gz
rust-089810a1cb28cfb0262b62f1d36c75ea790b0739.zip
Do not suggest similarly named enclosing item
-rw-r--r--src/librustc_resolve/diagnostics.rs47
-rw-r--r--src/test/ui/const-generics/struct-with-invalid-const-param.stderr5
-rw-r--r--src/test/ui/issues/issue-31845.stderr4
-rw-r--r--src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr11
-rw-r--r--src/test/ui/privacy/legacy-ctor-visibility.stderr4
-rw-r--r--src/test/ui/proc-macro/attributes-on-modules-fail.stderr12
-rw-r--r--src/test/ui/resolve/privacy-enum-ctor.stderr7
-rw-r--r--src/test/ui/ui-testing-optout.stderr5
8 files changed, 41 insertions, 54 deletions
diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs
index 9502be728de..de92204a7c2 100644
--- a/src/librustc_resolve/diagnostics.rs
+++ b/src/librustc_resolve/diagnostics.rs
@@ -942,17 +942,6 @@ impl<'a> Resolver<'a> {
             Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
             Some(suggestion) => suggestion,
         };
-        let msg = format!(
-            "{} {} with a similar name exists",
-            suggestion.res.article(),
-            suggestion.res.descr()
-        );
-        err.span_suggestion(
-            span,
-            &msg,
-            suggestion.candidate.to_string(),
-            Applicability::MaybeIncorrect,
-        );
         let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
             LOCAL_CRATE => self.opt_span(def_id),
             _ => Some(
@@ -961,9 +950,30 @@ impl<'a> Resolver<'a> {
                     .guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
             ),
         });
-        if let Some(span) = def_span {
+        if let Some(def_span) = def_span {
+            if span.overlaps(def_span) {
+                // Don't suggest typo suggestion for itself like in the followoing:
+                // error[E0423]: expected function, tuple struct or tuple variant, found struct `X`
+                //   --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14
+                //    |
+                // LL | struct X {}
+                //    | ----------- `X` defined here
+                // LL |
+                // LL | const Y: X = X("ö");
+                //    | -------------^^^^^^- similarly named constant `Y` defined here
+                //    |
+                // help: use struct literal syntax instead
+                //    |
+                // LL | const Y: X = X {};
+                //    |              ^^^^
+                // help: a constant with a similar name exists
+                //    |
+                // LL | const Y: X = Y("ö");
+                //    |              ^
+                return false;
+            }
             err.span_label(
-                self.session.source_map().guess_head_span(span),
+                self.session.source_map().guess_head_span(def_span),
                 &format!(
                     "similarly named {} `{}` defined here",
                     suggestion.res.descr(),
@@ -971,6 +981,17 @@ impl<'a> Resolver<'a> {
                 ),
             );
         }
+        let msg = format!(
+            "{} {} with a similar name exists",
+            suggestion.res.article(),
+            suggestion.res.descr()
+        );
+        err.span_suggestion(
+            span,
+            &msg,
+            suggestion.candidate.to_string(),
+            Applicability::MaybeIncorrect,
+        );
         true
     }
 
diff --git a/src/test/ui/const-generics/struct-with-invalid-const-param.stderr b/src/test/ui/const-generics/struct-with-invalid-const-param.stderr
index a968b26bc26..47617c7849f 100644
--- a/src/test/ui/const-generics/struct-with-invalid-const-param.stderr
+++ b/src/test/ui/const-generics/struct-with-invalid-const-param.stderr
@@ -2,10 +2,7 @@ error[E0573]: expected type, found const parameter `C`
   --> $DIR/struct-with-invalid-const-param.rs:4:23
    |
 LL | struct S<const C: u8>(C);
-   | ----------------------^--
-   | |                     |
-   | |                     help: a struct with a similar name exists: `S`
-   | similarly named struct `S` defined here
+   |                       ^ not a type
 
 warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
   --> $DIR/struct-with-invalid-const-param.rs:1:12
diff --git a/src/test/ui/issues/issue-31845.stderr b/src/test/ui/issues/issue-31845.stderr
index fe51fa0699f..56281938559 100644
--- a/src/test/ui/issues/issue-31845.stderr
+++ b/src/test/ui/issues/issue-31845.stderr
@@ -1,10 +1,8 @@
 error[E0425]: cannot find function `g` in this scope
   --> $DIR/issue-31845.rs:7:12
    |
-LL |         fn h() {
-   |         ------ similarly named function `h` defined here
 LL |            g();
-   |            ^ help: a function with a similar name exists: `h`
+   |            ^ not found in this scope
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr b/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr
index aa30a7e0d64..c3dda704b0e 100644
--- a/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr
+++ b/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr
@@ -5,16 +5,7 @@ LL | struct X {}
    | ----------- `X` defined here
 LL | 
 LL | const Y: X = X("ö");
-   | -------------^^^^^^- similarly named constant `Y` defined here
-   |
-help: use struct literal syntax instead
-   |
-LL | const Y: X = X {};
-   |              ^^^^
-help: a constant with a similar name exists
-   |
-LL | const Y: X = Y("ö");
-   |              ^
+   |              ^^^^^^ help: use struct literal syntax instead: `X {}`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/privacy/legacy-ctor-visibility.stderr b/src/test/ui/privacy/legacy-ctor-visibility.stderr
index 4f0d72de6f1..c8057d85ed1 100644
--- a/src/test/ui/privacy/legacy-ctor-visibility.stderr
+++ b/src/test/ui/privacy/legacy-ctor-visibility.stderr
@@ -1,10 +1,8 @@
 error[E0423]: expected function, tuple struct or tuple variant, found struct `S`
   --> $DIR/legacy-ctor-visibility.rs:9:13
    |
-LL |         fn f() {
-   |         ------ similarly named function `f` defined here
 LL |             S(10);
-   |             ^ help: a function with a similar name exists: `f`
+   |             ^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/proc-macro/attributes-on-modules-fail.stderr b/src/test/ui/proc-macro/attributes-on-modules-fail.stderr
index b37f1bd393c..97b2f22e161 100644
--- a/src/test/ui/proc-macro/attributes-on-modules-fail.stderr
+++ b/src/test/ui/proc-macro/attributes-on-modules-fail.stderr
@@ -44,12 +44,8 @@ error[E0412]: cannot find type `Y` in this scope
   --> $DIR/attributes-on-modules-fail.rs:10:14
    |
 LL |     type A = Y;
-   |     ---------^- similarly named type alias `A` defined here
+   |              ^ not found in this scope
    |
-help: a type alias with a similar name exists
-   |
-LL |     type A = A;
-   |              ^
 help: consider importing this struct
    |
 LL |     use Y;
@@ -59,12 +55,8 @@ error[E0412]: cannot find type `X` in this scope
   --> $DIR/attributes-on-modules-fail.rs:14:10
    |
 LL | type A = X;
-   | ---------^- similarly named type alias `A` defined here
-   |
-help: a type alias with a similar name exists
+   |          ^ not found in this scope
    |
-LL | type A = A;
-   |          ^
 help: consider importing this struct
    |
 LL | use m::X;
diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr
index f3cc338f13c..f1ed7aaa867 100644
--- a/src/test/ui/resolve/privacy-enum-ctor.stderr
+++ b/src/test/ui/resolve/privacy-enum-ctor.stderr
@@ -16,9 +16,6 @@ LL |         m::Z::Unit;
 error[E0423]: expected value, found enum `Z`
   --> $DIR/privacy-enum-ctor.rs:25:9
    |
-LL |     fn f() {
-   |     ------ similarly named function `f` defined here
-...
 LL |         Z;
    |         ^
    |
@@ -30,10 +27,6 @@ LL |         m::Z::Struct;
    |         ^^^^^^^^^^^^
 LL |         m::Z::Unit;
    |         ^^^^^^^^^^
-help: a function with a similar name exists
-   |
-LL |         f;
-   |         ^
 
 error[E0423]: expected value, found struct variant `Z::Struct`
   --> $DIR/privacy-enum-ctor.rs:29:20
diff --git a/src/test/ui/ui-testing-optout.stderr b/src/test/ui/ui-testing-optout.stderr
index f562bb74c11..652c472c0bc 100644
--- a/src/test/ui/ui-testing-optout.stderr
+++ b/src/test/ui/ui-testing-optout.stderr
@@ -2,10 +2,7 @@ error[E0412]: cannot find type `B` in this scope
  --> $DIR/ui-testing-optout.rs:4:10
   |
 4 | type A = B;
-  | ---------^-
-  | |        |
-  | |        help: a type alias with a similar name exists: `A`
-  | similarly named type alias `A` defined here
+  |          ^ not found in this scope
 
 error[E0412]: cannot find type `D` in this scope
  --> $DIR/ui-testing-optout.rs:7:10