about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-06-13 12:08:32 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-06-16 17:36:55 -0700
commitaf45d8a5bb4909e6908c4d7bba80eedcdcd7f961 (patch)
tree40105e5401f40a50683d17ec752289e7cda8fcf5
parent4606168dd508007fb1014b6ab12b27e320e07038 (diff)
downloadrust-af45d8a5bb4909e6908c4d7bba80eedcdcd7f961.tar.gz
rust-af45d8a5bb4909e6908c4d7bba80eedcdcd7f961.zip
Suggest new type param on single char ident
Suggest new type parameter on single char uppercase ident even if it
doesn't appear in a field's type parameter.

Address comment in #72641.
-rw-r--r--src/librustc_resolve/late/diagnostics.rs41
-rw-r--r--src/test/ui/associated-types/associated-types-eq-1.stderr11
-rw-r--r--src/test/ui/suggestions/type-not-found-in-adt-field.rs8
-rw-r--r--src/test/ui/suggestions/type-not-found-in-adt-field.stderr16
4 files changed, 61 insertions, 15 deletions
diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs
index 28ff89f6692..694e2ac2a36 100644
--- a/src/librustc_resolve/late/diagnostics.rs
+++ b/src/librustc_resolve/late/diagnostics.rs
@@ -919,20 +919,45 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
         &self,
         path: &[Segment],
     ) -> Option<(Span, &'static str, String, Applicability)> {
-        let ident = match path {
-            [segment] if !segment.has_args => segment.ident,
+        let (ident, span) = match path {
+            [segment] if !segment.has_args => (segment.ident.to_string(), segment.ident.span),
             _ => return None,
         };
-        match (
-            self.diagnostic_metadata.current_item,
-            self.diagnostic_metadata.currently_processing_generics,
-        ) {
-            (Some(Item { kind: ItemKind::Fn(..), ident, .. }), true) if ident.name == sym::main => {
+        let mut iter = ident.chars().map(|c| c.is_uppercase());
+        let single_uppercase_char =
+            matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
+        if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
+            return None;
+        }
+        match (self.diagnostic_metadata.current_item, single_uppercase_char) {
+            (Some(Item { kind: ItemKind::Fn(..), ident, .. }), _) if ident.name == sym::main => {
                 // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
             }
-            (Some(Item { kind, .. }), true) => {
+            (
+                Some(Item {
+                    kind:
+                        kind @ ItemKind::Fn(..)
+                        | kind @ ItemKind::Enum(..)
+                        | kind @ ItemKind::Struct(..)
+                        | kind @ ItemKind::Union(..),
+                    ..
+                }),
+                true,
+            )
+            | (Some(Item { kind, .. }), false) => {
                 // Likely missing type parameter.
                 if let Some(generics) = kind.generics() {
+                    if span.overlaps(generics.span) {
+                        // Avoid the following:
+                        // error[E0405]: cannot find trait `A` in this scope
+                        //  --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
+                        //   |
+                        // L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
+                        //   |           ^- help: you might be missing a type parameter: `, A`
+                        //   |           |
+                        //   |           not found in this scope
+                        return None;
+                    }
                     let msg = "you might be missing a type parameter";
                     let (span, sugg) = if let [.., param] = &generics.params[..] {
                         let span = if let [.., bound] = &param.bounds[..] {
diff --git a/src/test/ui/associated-types/associated-types-eq-1.stderr b/src/test/ui/associated-types/associated-types-eq-1.stderr
index 66c5f34644c..53a45cf4e4f 100644
--- a/src/test/ui/associated-types/associated-types-eq-1.stderr
+++ b/src/test/ui/associated-types/associated-types-eq-1.stderr
@@ -4,7 +4,16 @@ error[E0412]: cannot find type `A` in this scope
 LL | fn foo2<I: Foo>(x: I) {
    |         - similarly named type parameter `I` defined here
 LL |     let _: A = x.boo();
-   |            ^ help: a type parameter with a similar name exists: `I`
+   |            ^
+   |
+help: a type parameter with a similar name exists
+   |
+LL |     let _: I = x.boo();
+   |            ^
+help: you might be missing a type parameter
+   |
+LL | fn foo2<I: Foo, A>(x: I) {
+   |               ^^^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/suggestions/type-not-found-in-adt-field.rs b/src/test/ui/suggestions/type-not-found-in-adt-field.rs
index 6bd42472f5a..4cbfe58d357 100644
--- a/src/test/ui/suggestions/type-not-found-in-adt-field.rs
+++ b/src/test/ui/suggestions/type-not-found-in-adt-field.rs
@@ -1,5 +1,9 @@
-struct S {
-    m: Vec<Hashmap<String, ()>>, //~ ERROR cannot find type `Hashmap` in this scope
+struct Struct {
+    m: Vec<Someunknownname<String, ()>>, //~ ERROR cannot find type `Someunknownname` in this scope
+    //~^ NOTE not found in this scope
+}
+struct OtherStruct { //~ HELP you might be missing a type parameter
+    m: K, //~ ERROR cannot find type `K` in this scope
     //~^ NOTE not found in this scope
 }
 fn main() {}
diff --git a/src/test/ui/suggestions/type-not-found-in-adt-field.stderr b/src/test/ui/suggestions/type-not-found-in-adt-field.stderr
index cfad8c689d0..e990fb5ba12 100644
--- a/src/test/ui/suggestions/type-not-found-in-adt-field.stderr
+++ b/src/test/ui/suggestions/type-not-found-in-adt-field.stderr
@@ -1,9 +1,17 @@
-error[E0412]: cannot find type `Hashmap` in this scope
+error[E0412]: cannot find type `Someunknownname` in this scope
   --> $DIR/type-not-found-in-adt-field.rs:2:12
    |
-LL |     m: Vec<Hashmap<String, ()>>,
-   |            ^^^^^^^ not found in this scope
+LL |     m: Vec<Someunknownname<String, ()>>,
+   |            ^^^^^^^^^^^^^^^ not found in this scope
 
-error: aborting due to previous error
+error[E0412]: cannot find type `K` in this scope
+  --> $DIR/type-not-found-in-adt-field.rs:6:8
+   |
+LL | struct OtherStruct {
+   |                   - help: you might be missing a type parameter: `<K>`
+LL |     m: K,
+   |        ^ not found in this scope
+
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0412`.