Skip to content

Commit 693e957

Browse files
committed
Suggest adding a type parameter for impls
1 parent 24acc38 commit 693e957

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,8 +1600,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16001600
if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
16011601
return None;
16021602
}
1603-
match (self.diagnostic_metadata.current_item, single_uppercase_char) {
1604-
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _) if ident.name == sym::main => {
1603+
match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generics) {
1604+
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
16051605
// Ignore `fn main()` as we don't want to suggest `fn main<T>()`
16061606
}
16071607
(
@@ -1613,9 +1613,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16131613
| kind @ ItemKind::Union(..),
16141614
..
16151615
}),
1616-
true,
1616+
true, _
16171617
)
1618-
| (Some(Item { kind, .. }), false) => {
1618+
// Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
1619+
| (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
1620+
| (Some(Item { kind, .. }), false, _) => {
16191621
// Likely missing type parameter.
16201622
if let Some(generics) = kind.generics() {
16211623
if span.overlaps(generics.span) {

src/test/ui/const-generics/diagnostics.stderr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ LL | struct A<const N: u8>;
55
| ---------------------- similarly named struct `A` defined here
66
LL | trait Foo {}
77
LL | impl Foo for A<N> {}
8-
| ^ help: a struct with a similar name exists: `A`
8+
| ^
9+
|
10+
help: a struct with a similar name exists
11+
|
12+
LL | impl Foo for A<A> {}
13+
| ^
14+
help: you might be missing a type parameter
15+
|
16+
LL | impl<N> Foo for A<N> {}
17+
| ^^^
918

1019
error[E0412]: cannot find type `T` in this scope
1120
--> $DIR/diagnostics.rs:16:32
@@ -14,7 +23,16 @@ LL | struct A<const N: u8>;
1423
| ---------------------- similarly named struct `A` defined here
1524
...
1625
LL | impl<const N: u8> Foo for C<N, T> {}
17-
| ^ help: a struct with a similar name exists: `A`
26+
| ^
27+
|
28+
help: a struct with a similar name exists
29+
|
30+
LL | impl<const N: u8> Foo for C<N, A> {}
31+
| ^
32+
help: you might be missing a type parameter
33+
|
34+
LL | impl<const N, T: u8> Foo for C<N, T> {}
35+
| ^^^
1836

1937
error[E0747]: unresolved item provided when a constant was expected
2038
--> $DIR/diagnostics.rs:7:16

src/test/ui/traits/issue-75627.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0412]: cannot find type `T` in this scope
22
--> $DIR/issue-75627.rs:3:26
33
|
44
LL | unsafe impl Send for Foo<T> {}
5-
| ^ not found in this scope
5+
| - ^ not found in this scope
6+
| |
7+
| help: you might be missing a type parameter: `<T>`
68

79
error: aborting due to previous error
810

src/test/ui/traits/issue-78372.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ error[E0412]: cannot find type `U` in this scope
1313
--> $DIR/issue-78372.rs:3:31
1414
|
1515
LL | impl<T> DispatchFromDyn<Smaht<U, MISC>> for T {}
16-
| - ^ help: a type parameter with a similar name exists: `T`
16+
| - ^
1717
| |
1818
| similarly named type parameter `T` defined here
19+
|
20+
help: a type parameter with a similar name exists
21+
|
22+
LL | impl<T> DispatchFromDyn<Smaht<T, MISC>> for T {}
23+
| ^
24+
help: you might be missing a type parameter
25+
|
26+
LL | impl<T, U> DispatchFromDyn<Smaht<U, MISC>> for T {}
27+
| ^^^
1928

2029
error[E0412]: cannot find type `MISC` in this scope
2130
--> $DIR/issue-78372.rs:3:34

0 commit comments

Comments
 (0)