about summary refs log tree commit diff
diff options
context:
space:
mode:
authormibac138 <5672750+mibac138@users.noreply.github.com>2021-05-06 14:33:23 +0200
committermibac138 <5672750+mibac138@users.noreply.github.com>2021-05-07 18:29:25 +0200
commit4c72efc8167405ca1cc3002266a9bf15f70dafb3 (patch)
tree8442191074b8b2a1bffc030044ca565f3fb044be
parent1bb94fbbeb703806817a09806365e26a18f5daa4 (diff)
downloadrust-4c72efc8167405ca1cc3002266a9bf15f70dafb3.tar.gz
rust-4c72efc8167405ca1cc3002266a9bf15f70dafb3.zip
Fix impl type parameter suggestion involving consts
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs9
-rw-r--r--src/test/ui/const-generics/diagnostics.stderr4
-rw-r--r--src/test/ui/missing/missing-items/missing-type-parameter2.rs6
-rw-r--r--src/test/ui/missing/missing-items/missing-type-parameter2.stderr52
4 files changed, 59 insertions, 12 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index a4bf19aab95..28d636ef1b2 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -6,7 +6,10 @@ use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
 use crate::{PathResult, PathSource, Segment};
 
 use rustc_ast::visit::FnKind;
-use rustc_ast::{self as ast, Expr, ExprKind, Item, ItemKind, NodeId, Path, Ty, TyKind};
+use rustc_ast::{
+    self as ast, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind, NodeId, Path, Ty,
+    TyKind,
+};
 use rustc_ast_pretty::pprust::path_segment_to_string;
 use rustc_data_structures::fx::FxHashSet;
 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
@@ -1635,6 +1638,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
                     let (span, sugg) = if let [.., param] = &generics.params[..] {
                         let span = if let [.., bound] = &param.bounds[..] {
                             bound.span()
+                        } else if let GenericParam {
+                            kind: GenericParamKind::Const { ty, kw_span: _, default  }, ..
+                        } = param {
+                            default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
                         } else {
                             param.ident.span
                         };
diff --git a/src/test/ui/const-generics/diagnostics.stderr b/src/test/ui/const-generics/diagnostics.stderr
index 983cb52f3ff..2e3132c2eb7 100644
--- a/src/test/ui/const-generics/diagnostics.stderr
+++ b/src/test/ui/const-generics/diagnostics.stderr
@@ -31,8 +31,8 @@ LL | impl<const N: u8> Foo for C<N, A> {}
    |                                ^
 help: you might be missing a type parameter
    |
-LL | impl<const N, T: u8> Foo for C<N, T> {}
-   |             ^^^
+LL | impl<const N: u8, T> Foo for C<N, T> {}
+   |                 ^^^
 
 error[E0747]: unresolved item provided when a constant was expected
   --> $DIR/diagnostics.rs:7:16
diff --git a/src/test/ui/missing/missing-items/missing-type-parameter2.rs b/src/test/ui/missing/missing-items/missing-type-parameter2.rs
index e5d90bb6ff0..15dc5ef797b 100644
--- a/src/test/ui/missing/missing-items/missing-type-parameter2.rs
+++ b/src/test/ui/missing/missing-items/missing-type-parameter2.rs
@@ -11,7 +11,11 @@ impl<T, const A: u8 = 2> X<N> {}
 //~| ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
 //~| ERROR unresolved item provided when a constant was expected
 
-fn bar<const N: u8>(a: A) {}
+fn foo(_: T) where T: Send {}
+//~^ ERROR cannot find type `T` in this scope
+//~| ERROR cannot find type `T` in this scope
+
+fn bar<const N: u8>(_: A) {}
 //~^ ERROR cannot find type `A` in this scope
 
 fn main() {
diff --git a/src/test/ui/missing/missing-items/missing-type-parameter2.stderr b/src/test/ui/missing/missing-items/missing-type-parameter2.stderr
index 545be1c34fb..985a9bb2a3f 100644
--- a/src/test/ui/missing/missing-items/missing-type-parameter2.stderr
+++ b/src/test/ui/missing/missing-items/missing-type-parameter2.stderr
@@ -30,26 +30,62 @@ LL | impl<T, const A: u8 = 2> X<T> {}
    |                            ^
 help: you might be missing a type parameter
    |
-LL | impl<T, const A, N: u8 = 2> X<N> {}
-   |                ^^^
+LL | impl<T, const A: u8 = 2, N> X<N> {}
+   |                        ^^^
+
+error[E0412]: cannot find type `T` in this scope
+  --> $DIR/missing-type-parameter2.rs:14:20
+   |
+LL | struct X<const N: u8>();
+   | ------------------------ similarly named struct `X` defined here
+...
+LL | fn foo(_: T) where T: Send {}
+   |                    ^
+   |
+help: a struct with a similar name exists
+   |
+LL | fn foo(_: T) where X: Send {}
+   |                    ^
+help: you might be missing a type parameter
+   |
+LL | fn foo<T>(_: T) where T: Send {}
+   |       ^^^
+
+error[E0412]: cannot find type `T` in this scope
+  --> $DIR/missing-type-parameter2.rs:14:11
+   |
+LL | struct X<const N: u8>();
+   | ------------------------ similarly named struct `X` defined here
+...
+LL | fn foo(_: T) where T: Send {}
+   |           ^
+   |
+help: a struct with a similar name exists
+   |
+LL | fn foo(_: X) where T: Send {}
+   |           ^
+help: you might be missing a type parameter
+   |
+LL | fn foo<T>(_: T) where T: Send {}
+   |       ^^^
 
 error[E0412]: cannot find type `A` in this scope
-  --> $DIR/missing-type-parameter2.rs:14:24
+  --> $DIR/missing-type-parameter2.rs:18:24
    |
 LL | struct X<const N: u8>();
    | ------------------------ similarly named struct `X` defined here
 ...
-LL | fn bar<const N: u8>(a: A) {}
+LL | fn bar<const N: u8>(_: A) {}
    |                        ^
    |
 help: a struct with a similar name exists
    |
-LL | fn bar<const N: u8>(a: X) {}
+LL | fn bar<const N: u8>(_: X) {}
    |                        ^
 help: you might be missing a type parameter
    |
-LL | fn bar<const N, A: u8>(a: A) {}
-   |               ^^^
+LL | fn bar<const N: u8, A>(_: A) {}
+   |                   ^^^
 
 error[E0747]: unresolved item provided when a constant was expected
   --> $DIR/missing-type-parameter2.rs:6:8
@@ -79,7 +115,7 @@ help: if this generic argument was intended as a const parameter, surround it wi
 LL | impl<T, const A: u8 = 2> X<{ N }> {}
    |                            ^   ^
 
-error: aborting due to 6 previous errors
+error: aborting due to 8 previous errors
 
 Some errors have detailed explanations: E0412, E0747.
 For more information about an error, try `rustc --explain E0412`.