Skip to content

Commit d890018

Browse files
committed
Suggest generic_const_args + type const in const operation diagnostics
Point users at `#![feature(generic_const_args)]` and `type const` items when they hit the "generic parameters may not be used in const operations" error, not just `generic_const_exprs`.
1 parent 31a9463 commit d890018

69 files changed

Lines changed: 213 additions & 28 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,14 @@ impl<'tcx> ForbidParamUsesFolder<'tcx> {
432432
diag.span_note(impl_.self_ty.span, "not a concrete type");
433433
}
434434
}
435-
if matches!(self.context, ForbidParamContext::ConstArgument)
436-
&& self.tcx.features().min_generic_const_args()
437-
{
438-
if !self.tcx.features().generic_const_args() {
439-
diag.help("add `#![feature(generic_const_args)]` to allow generic expressions as the RHS of const items");
440-
} else {
435+
if matches!(self.context, ForbidParamContext::ConstArgument) {
436+
if self.tcx.features().generic_const_args() {
441437
diag.help("consider factoring the expression into a `type const` item and use it as the const argument instead");
438+
} else if self.tcx.features().min_generic_const_args() {
439+
diag.help("add `#![feature(generic_const_args)]` and extract the expression into a `type const` item");
440+
} else if self.tcx.sess.is_nightly_build() {
441+
diag.help("add `#![feature(generic_const_exprs)]` to allow generic const expressions");
442+
diag.help("alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item");
442443
}
443444
}
444445
diag.emit()

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10711071
span,
10721072
name,
10731073
param_kind: is_type,
1074-
help: self.tcx.sess.is_nightly_build(),
1074+
help: self.tcx.sess.is_nightly_build()
1075+
&& !self.tcx.features().min_generic_const_args(),
10751076
is_gca,
10761077
help_gca: is_gca,
1078+
help_suggest_gca: self.tcx.sess.is_nightly_build() && !is_gca,
10771079
})
10781080
}
10791081
ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self

compiler/rustc_resolve/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ pub(crate) struct ParamInNonTrivialAnonConst {
407407
"consider factoring the expression into a `type const` item and use it as the const argument instead"
408408
)]
409409
pub(crate) help_gca: bool,
410+
#[help(
411+
"alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item"
412+
)]
413+
pub(crate) help_suggest_gca: bool,
410414
}
411415

412416
#[derive(Debug)]

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3943,9 +3943,12 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
39433943
span: lifetime_ref.ident.span,
39443944
name: lifetime_ref.ident.name,
39453945
param_kind: errors::ParamKindInNonTrivialAnonConst::Lifetime,
3946-
help: self.r.tcx.sess.is_nightly_build(),
3946+
help: self.r.tcx.sess.is_nightly_build()
3947+
&& !self.r.tcx.features().min_generic_const_args(),
39473948
is_gca: self.r.tcx.features().generic_const_args(),
39483949
help_gca: self.r.tcx.features().generic_const_args(),
3950+
help_suggest_gca: self.r.tcx.sess.is_nightly_build()
3951+
&& !self.r.tcx.features().generic_const_args(),
39493952
})
39503953
.emit()
39513954
}

tests/ui/associated-consts/associated-const-type-parameter-arrays.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let _array: [u32; <A as Foo>::Y];
66
|
77
= note: type parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
910

1011
error: aborting due to 1 previous error
1112

tests/ui/associated-consts/issue-47814.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ note: not a concrete type
99
|
1010
LL | impl<'a> ArpIPv4<'a> {
1111
| ^^^^^^^^^^^
12+
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
13+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
1214

1315
error: aborting due to 1 previous error
1416

tests/ui/associated-item/associated-item-duplicate-bounds.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | links: [u32; A::LINKS], // Shouldn't suggest bounds already there.
66
|
77
= note: type parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
910

1011
error: aborting due to 1 previous error
1112

tests/ui/const-generics/adt_const_params/index-oob-ice-83993.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let x: &'b ();
66
|
77
= note: lifetime parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
910

1011
error: generic parameters may not be used in const operations
1112
--> $DIR/index-oob-ice-83993.rs:18:17
@@ -15,6 +16,7 @@ LL | let _: &'b ();
1516
|
1617
= note: lifetime parameters may not be used in const expressions
1718
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
19+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
1820

1921
error: aborting due to 2 previous errors
2022

tests/ui/const-generics/const-arg-in-const-arg.min.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let _: [u8; foo::<T>()];
66
|
77
= note: type parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
910

1011
error: generic parameters may not be used in const operations
1112
--> $DIR/const-arg-in-const-arg.rs:16:23
@@ -15,6 +16,7 @@ LL | let _: [u8; bar::<N>()];
1516
|
1617
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1718
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
19+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
1820

1921
error: generic parameters may not be used in const operations
2022
--> $DIR/const-arg-in-const-arg.rs:18:23
@@ -24,6 +26,7 @@ LL | let _: [u8; faz::<'a>(&())];
2426
|
2527
= note: lifetime parameters may not be used in const expressions
2628
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
29+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
2730

2831
error: generic parameters may not be used in const operations
2932
--> $DIR/const-arg-in-const-arg.rs:20:23
@@ -33,6 +36,7 @@ LL | let _: [u8; baz::<'a>(&())];
3336
|
3437
= note: lifetime parameters may not be used in const expressions
3538
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
39+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
3640

3741
error: generic parameters may not be used in const operations
3842
--> $DIR/const-arg-in-const-arg.rs:21:23
@@ -42,6 +46,7 @@ LL | let _: [u8; faz::<'b>(&())];
4246
|
4347
= note: lifetime parameters may not be used in const expressions
4448
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
49+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
4550

4651
error: generic parameters may not be used in const operations
4752
--> $DIR/const-arg-in-const-arg.rs:23:23
@@ -51,6 +56,7 @@ LL | let _: [u8; baz::<'b>(&())];
5156
|
5257
= note: lifetime parameters may not be used in const expressions
5358
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
59+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
5460

5561
error: generic parameters may not be used in const operations
5662
--> $DIR/const-arg-in-const-arg.rs:27:23
@@ -60,6 +66,7 @@ LL | let _ = [0; bar::<N>()];
6066
|
6167
= help: const parameters may only be used as standalone arguments here, i.e. `N`
6268
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
69+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
6370

6471
error: generic parameters may not be used in const operations
6572
--> $DIR/const-arg-in-const-arg.rs:29:23
@@ -69,6 +76,7 @@ LL | let _ = [0; faz::<'a>(&())];
6976
|
7077
= note: lifetime parameters may not be used in const expressions
7178
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
79+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
7280

7381
error: generic parameters may not be used in const operations
7482
--> $DIR/const-arg-in-const-arg.rs:31:23
@@ -78,6 +86,7 @@ LL | let _ = [0; baz::<'a>(&())];
7886
|
7987
= note: lifetime parameters may not be used in const expressions
8088
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
89+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
8190

8291
error: generic parameters may not be used in const operations
8392
--> $DIR/const-arg-in-const-arg.rs:32:23
@@ -87,6 +96,7 @@ LL | let _ = [0; faz::<'b>(&())];
8796
|
8897
= note: lifetime parameters may not be used in const expressions
8998
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
90100

91101
error: generic parameters may not be used in const operations
92102
--> $DIR/const-arg-in-const-arg.rs:34:23
@@ -96,6 +106,7 @@ LL | let _ = [0; baz::<'b>(&())];
96106
|
97107
= note: lifetime parameters may not be used in const expressions
98108
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
109+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
99110

100111
error: generic parameters may not be used in const operations
101112
--> $DIR/const-arg-in-const-arg.rs:35:24
@@ -105,6 +116,7 @@ LL | let _: Foo<{ foo::<T>() }>;
105116
|
106117
= note: type parameters may not be used in const expressions
107118
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
119+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
108120

109121
error: generic parameters may not be used in const operations
110122
--> $DIR/const-arg-in-const-arg.rs:36:24
@@ -114,6 +126,7 @@ LL | let _: Foo<{ bar::<N>() }>;
114126
|
115127
= help: const parameters may only be used as standalone arguments here, i.e. `N`
116128
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
129+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
117130

118131
error: generic parameters may not be used in const operations
119132
--> $DIR/const-arg-in-const-arg.rs:38:24
@@ -123,6 +136,7 @@ LL | let _: Foo<{ faz::<'a>(&()) }>;
123136
|
124137
= note: lifetime parameters may not be used in const expressions
125138
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
139+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
126140

127141
error: generic parameters may not be used in const operations
128142
--> $DIR/const-arg-in-const-arg.rs:40:24
@@ -132,6 +146,7 @@ LL | let _: Foo<{ baz::<'a>(&()) }>;
132146
|
133147
= note: lifetime parameters may not be used in const expressions
134148
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
149+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
135150

136151
error: generic parameters may not be used in const operations
137152
--> $DIR/const-arg-in-const-arg.rs:41:24
@@ -141,6 +156,7 @@ LL | let _: Foo<{ faz::<'b>(&()) }>;
141156
|
142157
= note: lifetime parameters may not be used in const expressions
143158
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
159+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
144160

145161
error: generic parameters may not be used in const operations
146162
--> $DIR/const-arg-in-const-arg.rs:43:24
@@ -150,6 +166,7 @@ LL | let _: Foo<{ baz::<'b>(&()) }>;
150166
|
151167
= note: lifetime parameters may not be used in const expressions
152168
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
169+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
153170

154171
error: generic parameters may not be used in const operations
155172
--> $DIR/const-arg-in-const-arg.rs:44:27
@@ -159,6 +176,7 @@ LL | let _ = Foo::<{ foo::<T>() }>;
159176
|
160177
= note: type parameters may not be used in const expressions
161178
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
179+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
162180

163181
error: generic parameters may not be used in const operations
164182
--> $DIR/const-arg-in-const-arg.rs:45:27
@@ -168,6 +186,7 @@ LL | let _ = Foo::<{ bar::<N>() }>;
168186
|
169187
= help: const parameters may only be used as standalone arguments here, i.e. `N`
170188
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
189+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
171190

172191
error: generic parameters may not be used in const operations
173192
--> $DIR/const-arg-in-const-arg.rs:47:27
@@ -177,6 +196,7 @@ LL | let _ = Foo::<{ faz::<'a>(&()) }>;
177196
|
178197
= note: lifetime parameters may not be used in const expressions
179198
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
199+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
180200

181201
error: generic parameters may not be used in const operations
182202
--> $DIR/const-arg-in-const-arg.rs:49:27
@@ -186,6 +206,7 @@ LL | let _ = Foo::<{ baz::<'a>(&()) }>;
186206
|
187207
= note: lifetime parameters may not be used in const expressions
188208
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
209+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
189210

190211
error: generic parameters may not be used in const operations
191212
--> $DIR/const-arg-in-const-arg.rs:50:27
@@ -195,6 +216,7 @@ LL | let _ = Foo::<{ faz::<'b>(&()) }>;
195216
|
196217
= note: lifetime parameters may not be used in const expressions
197218
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
219+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
198220

199221
error: generic parameters may not be used in const operations
200222
--> $DIR/const-arg-in-const-arg.rs:52:27
@@ -204,6 +226,7 @@ LL | let _ = Foo::<{ baz::<'b>(&()) }>;
204226
|
205227
= note: lifetime parameters may not be used in const expressions
206228
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
229+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
207230

208231
error[E0747]: unresolved item provided when a constant was expected
209232
--> $DIR/const-arg-in-const-arg.rs:16:23

tests/ui/const-generics/const-argument-if-length.min.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | pad: [u8; is_zst::<T>()],
66
|
77
= note: type parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
= help: alternatively, you can use `#![feature(generic_const_args)]` and extract the expression into a `type const` item
910

1011
error[E0277]: the size for values of type `T` cannot be known at compilation time
1112
--> $DIR/const-argument-if-length.rs:16:12

0 commit comments

Comments
 (0)