Skip to content

Commit 4c72efc

Browse files
committed
Fix impl type parameter suggestion involving consts
1 parent 1bb94fb commit 4c72efc

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
66
use crate::{PathResult, PathSource, Segment};
77

88
use rustc_ast::visit::FnKind;
9-
use rustc_ast::{self as ast, Expr, ExprKind, Item, ItemKind, NodeId, Path, Ty, TyKind};
9+
use rustc_ast::{
10+
self as ast, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind, NodeId, Path, Ty,
11+
TyKind,
12+
};
1013
use rustc_ast_pretty::pprust::path_segment_to_string;
1114
use rustc_data_structures::fx::FxHashSet;
1215
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
@@ -1635,6 +1638,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16351638
let (span, sugg) = if let [.., param] = &generics.params[..] {
16361639
let span = if let [.., bound] = &param.bounds[..] {
16371640
bound.span()
1641+
} else if let GenericParam {
1642+
kind: GenericParamKind::Const { ty, kw_span: _, default }, ..
1643+
} = param {
1644+
default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
16381645
} else {
16391646
param.ident.span
16401647
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ LL | impl<const N: u8> Foo for C<N, A> {}
3131
| ^
3232
help: you might be missing a type parameter
3333
|
34-
LL | impl<const N, T: u8> Foo for C<N, T> {}
35-
| ^^^
34+
LL | impl<const N: u8, T> Foo for C<N, T> {}
35+
| ^^^
3636

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

src/test/ui/missing/missing-items/missing-type-parameter2.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ impl<T, const A: u8 = 2> X<N> {}
1111
//~| ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
1212
//~| ERROR unresolved item provided when a constant was expected
1313

14-
fn bar<const N: u8>(a: A) {}
14+
fn foo(_: T) where T: Send {}
15+
//~^ ERROR cannot find type `T` in this scope
16+
//~| ERROR cannot find type `T` in this scope
17+
18+
fn bar<const N: u8>(_: A) {}
1519
//~^ ERROR cannot find type `A` in this scope
1620

1721
fn main() {

src/test/ui/missing/missing-items/missing-type-parameter2.stderr

+44-8
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,62 @@ LL | impl<T, const A: u8 = 2> X<T> {}
3030
| ^
3131
help: you might be missing a type parameter
3232
|
33-
LL | impl<T, const A, N: u8 = 2> X<N> {}
34-
| ^^^
33+
LL | impl<T, const A: u8 = 2, N> X<N> {}
34+
| ^^^
35+
36+
error[E0412]: cannot find type `T` in this scope
37+
--> $DIR/missing-type-parameter2.rs:14:20
38+
|
39+
LL | struct X<const N: u8>();
40+
| ------------------------ similarly named struct `X` defined here
41+
...
42+
LL | fn foo(_: T) where T: Send {}
43+
| ^
44+
|
45+
help: a struct with a similar name exists
46+
|
47+
LL | fn foo(_: T) where X: Send {}
48+
| ^
49+
help: you might be missing a type parameter
50+
|
51+
LL | fn foo<T>(_: T) where T: Send {}
52+
| ^^^
53+
54+
error[E0412]: cannot find type `T` in this scope
55+
--> $DIR/missing-type-parameter2.rs:14:11
56+
|
57+
LL | struct X<const N: u8>();
58+
| ------------------------ similarly named struct `X` defined here
59+
...
60+
LL | fn foo(_: T) where T: Send {}
61+
| ^
62+
|
63+
help: a struct with a similar name exists
64+
|
65+
LL | fn foo(_: X) where T: Send {}
66+
| ^
67+
help: you might be missing a type parameter
68+
|
69+
LL | fn foo<T>(_: T) where T: Send {}
70+
| ^^^
3571

3672
error[E0412]: cannot find type `A` in this scope
37-
--> $DIR/missing-type-parameter2.rs:14:24
73+
--> $DIR/missing-type-parameter2.rs:18:24
3874
|
3975
LL | struct X<const N: u8>();
4076
| ------------------------ similarly named struct `X` defined here
4177
...
42-
LL | fn bar<const N: u8>(a: A) {}
78+
LL | fn bar<const N: u8>(_: A) {}
4379
| ^
4480
|
4581
help: a struct with a similar name exists
4682
|
47-
LL | fn bar<const N: u8>(a: X) {}
83+
LL | fn bar<const N: u8>(_: X) {}
4884
| ^
4985
help: you might be missing a type parameter
5086
|
51-
LL | fn bar<const N, A: u8>(a: A) {}
52-
| ^^^
87+
LL | fn bar<const N: u8, A>(_: A) {}
88+
| ^^^
5389

5490
error[E0747]: unresolved item provided when a constant was expected
5591
--> $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
79115
LL | impl<T, const A: u8 = 2> X<{ N }> {}
80116
| ^ ^
81117

82-
error: aborting due to 6 previous errors
118+
error: aborting due to 8 previous errors
83119

84120
Some errors have detailed explanations: E0412, E0747.
85121
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)