Skip to content

Commit 2d9513b

Browse files
authored
Rollup merge of #142157 - Enselic:trivial-anon-const-use-cases, r=compiler-errors
rustc_resolve: Improve `resolve_const_param_in_non_trivial_anon_const` wording In some contexts, const expressions are OK. Add a `here` to the error message to clarify this. Closes #79429 which has 15 x 👍
2 parents b3ddf3c + 4882ea4 commit 2d9513b

22 files changed

+78
-35
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ resolve_const_param_in_enum_discriminant =
119119
const parameters may not be used in enum discriminant values
120120
121121
resolve_const_param_in_non_trivial_anon_const =
122-
const parameters may only be used as standalone arguments, i.e. `{$name}`
122+
const parameters may only be used as standalone arguments here, i.e. `{$name}`
123123
124124
resolve_constructor_private_if_any_field_private =
125125
a constructor is private if any of the fields is private

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | let _: [u8; bar::<N>()];
1414
| ^ cannot perform const operation using `N`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `N`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: generic parameters may not be used in const operations
@@ -58,7 +58,7 @@ error: generic parameters may not be used in const operations
5858
LL | let _ = [0; bar::<N>()];
5959
| ^ cannot perform const operation using `N`
6060
|
61-
= help: const parameters may only be used as standalone arguments, i.e. `N`
61+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
6262
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
6363

6464
error: generic parameters may not be used in const operations
@@ -112,7 +112,7 @@ error: generic parameters may not be used in const operations
112112
LL | let _: Foo<{ bar::<N>() }>;
113113
| ^ cannot perform const operation using `N`
114114
|
115-
= help: const parameters may only be used as standalone arguments, i.e. `N`
115+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
116116
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
117117

118118
error: generic parameters may not be used in const operations
@@ -166,7 +166,7 @@ error: generic parameters may not be used in const operations
166166
LL | let _ = Foo::<{ bar::<N>() }>;
167167
| ^ cannot perform const operation using `N`
168168
|
169-
= help: const parameters may only be used as standalone arguments, i.e. `N`
169+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
170170
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
171171

172172
error: generic parameters may not be used in const operations

tests/ui/const-generics/defaults/complex-generic-default-expr.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | struct Foo<const N: usize, const M: usize = { N + 1 }>;
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations

tests/ui/const-generics/early/const_arg_trivial_macro_expansion-4.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | N
77
LL | fn foo<const N: usize>() -> Foo<{ arg!{} arg!{} }> { loop {} }
88
| ------ in this macro invocation
99
|
10-
= help: const parameters may only be used as standalone arguments, i.e. `N`
10+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1111
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1212
= note: this error originates in the macro `arg` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

@@ -20,7 +20,7 @@ LL | N
2020
LL | fn foo<const N: usize>() -> Foo<{ arg!{} arg!{} }> { loop {} }
2121
| ------ in this macro invocation
2222
|
23-
= help: const parameters may only be used as standalone arguments, i.e. `N`
23+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
2424
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
2525
= note: this error originates in the macro `arg` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

@@ -30,7 +30,7 @@ error: generic parameters may not be used in const operations
3030
LL | fn bar<const N: usize>() -> [(); { empty!{}; N }] { loop {} }
3131
| ^ cannot perform const operation using `N`
3232
|
33-
= help: const parameters may only be used as standalone arguments, i.e. `N`
33+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
3434
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
3535

3636
error: aborting due to 3 previous errors

tests/ui/const-generics/early/macro_rules-braces.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ error: generic parameters may not be used in const operations
2626
LL | let _: foo!({{ N }});
2727
| ^ cannot perform const operation using `N`
2828
|
29-
= help: const parameters may only be used as standalone arguments, i.e. `N`
29+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
3030
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
3131

3232
error: generic parameters may not be used in const operations
@@ -35,7 +35,7 @@ error: generic parameters may not be used in const operations
3535
LL | let _: bar!({ N });
3636
| ^ cannot perform const operation using `N`
3737
|
38-
= help: const parameters may only be used as standalone arguments, i.e. `N`
38+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
3939
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
4040

4141
error: generic parameters may not be used in const operations
@@ -44,7 +44,7 @@ error: generic parameters may not be used in const operations
4444
LL | let _: baz!({{ N }});
4545
| ^ cannot perform const operation using `N`
4646
|
47-
= help: const parameters may only be used as standalone arguments, i.e. `N`
47+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
4848
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
4949

5050
error: generic parameters may not be used in const operations
@@ -53,7 +53,7 @@ error: generic parameters may not be used in const operations
5353
LL | let _: biz!({ N });
5454
| ^ cannot perform const operation using `N`
5555
|
56-
= help: const parameters may only be used as standalone arguments, i.e. `N`
56+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
5757
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
5858

5959
error: aborting due to 6 previous errors

tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | N
77
LL | fn foo<const N: usize>() -> A<{{ y!() }}> {
88
| ---- in this macro invocation
99
|
10-
= help: const parameters may only be used as standalone arguments, i.e. `N`
10+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1111
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1212
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | { N }
77
LL | fn foo<const N: usize>() -> A<{ y!() }> {
88
| ---- in this macro invocation
99
|
10-
= help: const parameters may only be used as standalone arguments, i.e. `N`
10+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1111
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1212
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | fn foo<const N: usize>() -> A<{ { N } }> {
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: aborting due to 1 previous error

tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]);
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | arr: [u8; CFG.arr_size],
1414
| ^^^ cannot perform const operation using `CFG`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `CFG`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `CFG`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: `Config` is forbidden as the type of a const generic parameter

tests/ui/const-generics/generic_const_exprs/bad-multiply.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | SmallVec<{ D * 2 }>:,
55
| ^ cannot perform const operation using `D`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `D`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `D`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error[E0747]: constant provided when a type was expected

tests/ui/const-generics/generic_const_exprs/feature-gate-generic_const_exprs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | type Arr<const N: usize> = [u8; N - 1];
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: aborting due to 1 previous error

tests/ui/const-generics/generic_const_exprs/issue-72787.min.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | Condition<{ LHS <= RHS }>: True
55
| ^^^ cannot perform const operation using `LHS`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `LHS`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `LHS`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | Condition<{ LHS <= RHS }>: True
1414
| ^^^ cannot perform const operation using `RHS`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `RHS`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `RHS`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: generic parameters may not be used in const operations
@@ -22,7 +22,7 @@ error: generic parameters may not be used in const operations
2222
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
2323
| ^ cannot perform const operation using `I`
2424
|
25-
= help: const parameters may only be used as standalone arguments, i.e. `I`
25+
= help: const parameters may only be used as standalone arguments here, i.e. `I`
2626
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
2727

2828
error: generic parameters may not be used in const operations
@@ -31,7 +31,7 @@ error: generic parameters may not be used in const operations
3131
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
3232
| ^ cannot perform const operation using `J`
3333
|
34-
= help: const parameters may only be used as standalone arguments, i.e. `J`
34+
= help: const parameters may only be used as standalone arguments here, i.e. `J`
3535
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
3636

3737
error: aborting due to 4 previous errors

tests/ui/const-generics/generic_const_exprs/issue-72819-generic-in-const-eval.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | where Assert::<{N < usize::MAX / 2}>: IsTrue,
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: aborting due to 1 previous error
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: unconstrained generic constant
2+
--> $DIR/trivial-anon-const-use-cases.rs:14:12
3+
|
4+
LL | stuff: [u8; { S + 1 }], // `S + 1` is NOT a valid const expression in this context.
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
help: try adding a `where` bound
8+
|
9+
LL | struct Y<const S: usize> where [(); { S + 1 }]: {
10+
| ++++++++++++++++++++++
11+
12+
error: aborting due to 1 previous error
13+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/trivial-anon-const-use-cases.rs:14:19
3+
|
4+
LL | stuff: [u8; { S + 1 }], // `S + 1` is NOT a valid const expression in this context.
5+
| ^ cannot perform const operation using `S`
6+
|
7+
= help: const parameters may only be used as standalone arguments here, i.e. `S`
8+
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
10+
error: aborting due to 1 previous error
11+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/79429>.
2+
3+
//@ revisions: full min
4+
#![cfg_attr(full, feature(generic_const_exprs))]
5+
#![cfg_attr(full, allow(incomplete_features))]
6+
7+
struct X<const S: usize>;
8+
9+
impl<const S: usize> X<S> {
10+
const LEN: usize = S + 1; // `S + 1` is a valid const expression in this context.
11+
}
12+
13+
struct Y<const S: usize> {
14+
stuff: [u8; { S + 1 }], // `S + 1` is NOT a valid const expression in this context.
15+
//[min]~^ ERROR generic parameters may not be used in const operations
16+
//[full]~^^ ERROR unconstrained generic constant
17+
}
18+
19+
fn main() {}

tests/ui/const-generics/issues/issue-68366.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | impl <const N: usize> Collatz<{Some(N)}> {}
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: `Option<usize>` is forbidden as the type of a const generic parameter

tests/ui/const-generics/issues/issue-76701-ty-param-in-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | fn const_param<const N: usize>() -> [u8; N + 1] {
1414
| ^ cannot perform const operation using `N`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `N`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: aborting due to 2 previous errors

tests/ui/const-generics/issues/issue-80375.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
55
| ^^^^^ cannot perform const operation using `COUNT`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `COUNT`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `COUNT`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: aborting due to 1 previous error

tests/ui/const-generics/legacy-const-generics-bad.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error: generic parameters may not be used in const operations
1616
LL | legacy_const_generics::foo(0, N + 1, 2);
1717
| ^ cannot perform const operation using `N`
1818
|
19-
= help: const parameters may only be used as standalone arguments, i.e. `N`
19+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
2020
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
2121

2222
error: aborting due to 2 previous errors

tests/ui/const-generics/min_const_generics/complex-expression.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | struct Break0<const N: usize>([u8; { N + 1 }]);
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | struct Break1<const N: usize>([u8; { { N } }]);
1414
| ^ cannot perform const operation using `N`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `N`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: generic parameters may not be used in const operations
@@ -22,7 +22,7 @@ error: generic parameters may not be used in const operations
2222
LL | let _: [u8; N + 1];
2323
| ^ cannot perform const operation using `N`
2424
|
25-
= help: const parameters may only be used as standalone arguments, i.e. `N`
25+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
2626
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
2727

2828
error: generic parameters may not be used in const operations
@@ -31,7 +31,7 @@ error: generic parameters may not be used in const operations
3131
LL | let _ = [0; N + 1];
3232
| ^ cannot perform const operation using `N`
3333
|
34-
= help: const parameters may only be used as standalone arguments, i.e. `N`
34+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
3535
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
3636

3737
error: generic parameters may not be used in const operations

tests/ui/const-generics/repeat_expr_hack_gives_right_generics.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
44
LL | bar::<{ [1; N] }>();
55
| ^ cannot perform const operation using `N`
66
|
7-
= help: const parameters may only be used as standalone arguments, i.e. `N`
7+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
@@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
1313
LL | bar::<{ [1; { N + 1 }] }>();
1414
| ^ cannot perform const operation using `N`
1515
|
16-
= help: const parameters may only be used as standalone arguments, i.e. `N`
16+
= help: const parameters may only be used as standalone arguments here, i.e. `N`
1717
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)