Skip to content

Commit 56e0626

Browse files
committed
Auto merge of #110041 - fmease:diag-sugg-adding-const-param, r=compiler-errors
Suggest defining const parameter when appropriate Helps a bit with #91119. Following #105523's lead, I use placeholder `/* Type */` instead of `_` in the suggestion. It should be easier for newcomers to parse. `@rustbot` label A-diagnostics r? diagnostics
2 parents 709a97f + f2acafe commit 56e0626

File tree

4 files changed

+100
-7
lines changed

4 files changed

+100
-7
lines changed

compiler/rustc_resolve/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3467,8 +3467,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
34673467
sugg.to_string(),
34683468
Applicability::MaybeIncorrect,
34693469
))
3470-
} else if res.is_none() && matches!(source, PathSource::Type) {
3471-
this.report_missing_type_error(path)
3470+
} else if res.is_none() && let PathSource::Type | PathSource::Expr(_) = source {
3471+
this.suggest_adding_generic_parameter(path, source)
34723472
} else {
34733473
None
34743474
};

compiler/rustc_resolve/src/late/diagnostics.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2110,9 +2110,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21102110
}
21112111
}
21122112

2113-
pub(crate) fn report_missing_type_error(
2113+
pub(crate) fn suggest_adding_generic_parameter(
21142114
&self,
21152115
path: &[Segment],
2116+
source: PathSource<'_>,
21162117
) -> Option<(Span, &'static str, String, Applicability)> {
21172118
let (ident, span) = match path {
21182119
[segment]
@@ -2148,7 +2149,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21482149
// Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
21492150
| (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
21502151
| (Some(Item { kind, .. }), false, _) => {
2151-
// Likely missing type parameter.
21522152
if let Some(generics) = kind.generics() {
21532153
if span.overlaps(generics.span) {
21542154
// Avoid the following:
@@ -2161,7 +2161,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21612161
// | not found in this scope
21622162
return None;
21632163
}
2164-
let msg = "you might be missing a type parameter";
2164+
2165+
let (msg, sugg) = match source {
2166+
PathSource::Type => ("you might be missing a type parameter", ident),
2167+
PathSource::Expr(_) => ("you might be missing a const parameter", format!("const {ident}: /* Type */")),
2168+
_ => return None,
2169+
};
21652170
let (span, sugg) = if let [.., param] = &generics.params[..] {
21662171
let span = if let [.., bound] = &param.bounds[..] {
21672172
bound.span()
@@ -2172,9 +2177,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21722177
} else {
21732178
param.ident.span
21742179
};
2175-
(span, format!(", {}", ident))
2180+
(span, format!(", {sugg}"))
21762181
} else {
2177-
(generics.span, format!("<{}>", ident))
2182+
(generics.span, format!("<{sugg}>"))
21782183
};
21792184
// Do not suggest if this is coming from macro expansion.
21802185
if span.can_be_used_for_suggestions() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct Struct<const N: usize>;
2+
3+
impl Struct<{ N }> {}
4+
//~^ ERROR cannot find value `N` in this scope
5+
//~| HELP you might be missing a const parameter
6+
7+
fn func0(_: Struct<{ N }>) {}
8+
//~^ ERROR cannot find value `N` in this scope
9+
//~| HELP you might be missing a const parameter
10+
11+
fn func1(_: [u8; N]) {}
12+
//~^ ERROR cannot find value `N` in this scope
13+
//~| HELP you might be missing a const parameter
14+
15+
fn func2<T>(_: [T; N]) {}
16+
//~^ ERROR cannot find value `N` in this scope
17+
//~| HELP you might be missing a const parameter
18+
19+
struct Image<const R: usize>([[u32; C]; R]);
20+
//~^ ERROR cannot find value `C` in this scope
21+
//~| HELP a const parameter with a similar name exists
22+
//~| HELP you might be missing a const parameter
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error[E0425]: cannot find value `N` in this scope
2+
--> $DIR/missing-const-parameter.rs:3:15
3+
|
4+
LL | impl Struct<{ N }> {}
5+
| ^ not found in this scope
6+
|
7+
help: you might be missing a const parameter
8+
|
9+
LL | impl<const N: /* Type */> Struct<{ N }> {}
10+
| +++++++++++++++++++++
11+
12+
error[E0425]: cannot find value `N` in this scope
13+
--> $DIR/missing-const-parameter.rs:7:22
14+
|
15+
LL | fn func0(_: Struct<{ N }>) {}
16+
| ^ not found in this scope
17+
|
18+
help: you might be missing a const parameter
19+
|
20+
LL | fn func0<const N: /* Type */>(_: Struct<{ N }>) {}
21+
| +++++++++++++++++++++
22+
23+
error[E0425]: cannot find value `N` in this scope
24+
--> $DIR/missing-const-parameter.rs:11:18
25+
|
26+
LL | fn func1(_: [u8; N]) {}
27+
| ^ not found in this scope
28+
|
29+
help: you might be missing a const parameter
30+
|
31+
LL | fn func1<const N: /* Type */>(_: [u8; N]) {}
32+
| +++++++++++++++++++++
33+
34+
error[E0425]: cannot find value `N` in this scope
35+
--> $DIR/missing-const-parameter.rs:15:20
36+
|
37+
LL | fn func2<T>(_: [T; N]) {}
38+
| ^ not found in this scope
39+
|
40+
help: you might be missing a const parameter
41+
|
42+
LL | fn func2<T, const N: /* Type */>(_: [T; N]) {}
43+
| +++++++++++++++++++++
44+
45+
error[E0425]: cannot find value `C` in this scope
46+
--> $DIR/missing-const-parameter.rs:19:37
47+
|
48+
LL | struct Image<const R: usize>([[u32; C]; R]);
49+
| - ^
50+
| |
51+
| similarly named const parameter `R` defined here
52+
|
53+
help: a const parameter with a similar name exists
54+
|
55+
LL | struct Image<const R: usize>([[u32; R]; R]);
56+
| ~
57+
help: you might be missing a const parameter
58+
|
59+
LL | struct Image<const R: usize, const C: /* Type */>([[u32; C]; R]);
60+
| +++++++++++++++++++++
61+
62+
error: aborting due to 5 previous errors
63+
64+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)