Skip to content

Commit 807b981

Browse files
authored
Rollup merge of rust-lang#77825 - ethanboxx:min_const_generics_diagnostic, r=lcnr
`min_const_generics` diagnostics improvements As disscussed in [zulip/project-const-generics/non-trivial anonymous constant](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/non-trivial.20anonymous.20constants). This is my first PR on the compiler. @lcnr is mentoring me on this PR. Related to rust-lang#60551.
2 parents a732d3a + 79351b1 commit 807b981

File tree

56 files changed

+185
-192
lines changed

Some content is hidden

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

56 files changed

+185
-192
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -469,24 +469,17 @@ impl<'a> Resolver<'a> {
469469
ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
470470
let mut err = self.session.struct_span_err(
471471
span,
472-
"generic parameters must not be used inside of non-trivial constant values",
473-
);
474-
err.span_label(
475-
span,
476-
&format!(
477-
"non-trivial anonymous constants must not depend on the parameter `{}`",
478-
name
479-
),
472+
"generic parameters may not be used in const operations",
480473
);
474+
err.span_label(span, &format!("cannot perform const operation using `{}`", name));
481475

482476
if is_type {
483-
err.note("type parameters are currently not permitted in anonymous constants");
477+
err.note("type parameters may not be used in const expressions");
484478
} else {
485-
err.help(
486-
&format!("it is currently only allowed to use either `{0}` or `{{ {0} }}` as generic constants",
487-
name
488-
)
489-
);
479+
err.help(&format!(
480+
"const parameters may only be used as standalone arguments, i.e. `{}`",
481+
name
482+
));
490483
}
491484

492485
err

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ enum ResolutionError<'a> {
218218
ParamInTyOfConstParam(Symbol),
219219
/// constant values inside of type parameter defaults must not depend on generic parameters.
220220
ParamInAnonConstInTyDefault(Symbol),
221-
/// generic parameters must not be used inside of non-trivial constant values.
221+
/// generic parameters must not be used inside const evaluations.
222222
///
223223
/// This error is only emitted when using `min_const_generics`.
224224
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },

src/test/ui/const-generics/array-size-in-generic-struct-param.min.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/array-size-in-generic-struct-param.rs:9:48
33
|
44
LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]);
5-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
5+
| ^ cannot perform const operation using `N`
66
|
7-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/array-size-in-generic-struct-param.rs:20:15
1111
|
1212
LL | arr: [u8; CFG.arr_size],
13-
| ^^^ non-trivial anonymous constants must not depend on the parameter `CFG`
13+
| ^^^ cannot perform const operation using `CFG`
1414
|
15-
= help: it is currently only allowed to use either `CFG` or `{ CFG }` as generic constants
15+
= help: const parameters may only be used as standalone arguments, i.e. `CFG`
1616

1717
error: `Config` is forbidden as the type of a const generic parameter
1818
--> $DIR/array-size-in-generic-struct-param.rs:18:21

src/test/ui/const-generics/array-size-in-generic-struct-param.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#[allow(dead_code)]
99
struct ArithArrayLen<const N: usize>([u32; 0 + N]);
1010
//[full]~^ ERROR constant expression depends on a generic parameter
11-
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial constant values
11+
//[min]~^^ ERROR generic parameters may not be used in const operations
1212

1313
#[derive(PartialEq, Eq)]
1414
struct Config {
@@ -19,7 +19,7 @@ struct B<const CFG: Config> {
1919
//[min]~^ ERROR `Config` is forbidden
2020
arr: [u8; CFG.arr_size],
2121
//[full]~^ ERROR constant expression depends on a generic parameter
22-
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial
22+
//[min]~^^ ERROR generic parameters may not be used in const operations
2323
}
2424

2525
const C: Config = Config { arr_size: 5 };

src/test/ui/const-generics/const-argument-if-length.min.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/const-argument-if-length.rs:19:24
33
|
44
LL | pad: [u8; is_zst::<T>()],
5-
| ^ non-trivial anonymous constants must not depend on the parameter `T`
5+
| ^ cannot perform const operation using `T`
66
|
7-
= note: type parameters are currently not permitted in anonymous constants
7+
= note: type parameters may not be used in const expressions
88

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

src/test/ui/const-generics/const-argument-if-length.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct AtLeastByte<T: ?Sized> {
1717
value: T,
1818
//~^ ERROR the size for values of type `T` cannot be known at compilation time
1919
pad: [u8; is_zst::<T>()],
20-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
20+
//[min]~^ ERROR generic parameters may not be used in const operations
2121
//[full]~^^ ERROR evaluation of constant value failed
2222
}
2323

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/feature-gate-const_evaluatable_checked.rs:6:33
33
|
44
LL | type Arr<const N: usize> = [u8; N - 1];
5-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
5+
| ^ cannot perform const operation using `N`
66
|
7-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/const_evaluatable_checked/feature-gate-const_evaluatable_checked.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(min, feature(min_const_generics))]
55

66
type Arr<const N: usize> = [u8; N - 1];
7-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
7+
//[min]~^ ERROR generic parameters may not be used in const operations
88

99
fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
1010
//[full]~^ ERROR constant expression depends
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/simple.rs:8:53
33
|
44
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {
5-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
5+
| ^ cannot perform const operation using `N`
66
|
7-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/simple.rs:8:35
1111
|
1212
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {
13-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
13+
| ^ cannot perform const operation using `N`
1414
|
15-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
15+
= help: const parameters may only be used as standalone arguments, i.e. `N`
1616

1717
error: aborting due to 2 previous errors
1818

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/simple_fail.rs:7:33
33
|
44
LL | type Arr<const N: usize> = [u8; N - 1];
5-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
5+
| ^ cannot perform const operation using `N`
66
|
7-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(incomplete_features)]
66

77
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
8-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
8+
//[min]~^ ERROR generic parameters may not be used in const operations
99

1010
fn test<const N: usize>() -> Arr<N> where Arr<N>: Sized {
1111
todo!()
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/generic-function-call-in-array-length.rs:9:39
33
|
44
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
5-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
5+
| ^ cannot perform const operation using `N`
66
|
7-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `N`
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/generic-function-call-in-array-length.rs:12:13
1111
|
1212
LL | [0; foo(N)]
13-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
13+
| ^ cannot perform const operation using `N`
1414
|
15-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
15+
= help: const parameters may only be used as standalone arguments, i.e. `N`
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/const-generics/generic-function-call-in-array-length.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
const fn foo(n: usize) -> usize { n * 2 }
88

99
fn bar<const N: usize>() -> [u32; foo(N)] {
10-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
10+
//[min]~^ ERROR generic parameters may not be used in const operations
1111
//[full]~^^ ERROR constant expression depends on a generic parameter
1212
[0; foo(N)]
13-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
13+
//[min]~^ ERROR generic parameters may not be used in const operations
1414
}
1515

1616
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/generic-sum-in-array-length.rs:7:53
33
|
44
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
5-
| ^ non-trivial anonymous constants must not depend on the parameter `A`
5+
| ^ cannot perform const operation using `A`
66
|
7-
= help: it is currently only allowed to use either `A` or `{ A }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `A`
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/generic-sum-in-array-length.rs:7:57
1111
|
1212
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
13-
| ^ non-trivial anonymous constants must not depend on the parameter `B`
13+
| ^ cannot perform const operation using `B`
1414
|
15-
= help: it is currently only allowed to use either `B` or `{ B }` as generic constants
15+
= help: const parameters may only be used as standalone arguments, i.e. `B`
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/const-generics/generic-sum-in-array-length.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![cfg_attr(min, feature(min_const_generics))]
66

77
fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
8-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
9-
//[min]~| ERROR generic parameters must not be used inside of non-trivial constant values
8+
//[min]~^ ERROR generic parameters may not be used in const operations
9+
//[min]~| ERROR generic parameters may not be used in const operations
1010
//[full]~^^^ ERROR constant expression depends on a generic parameter
1111

1212
fn main() {}

src/test/ui/const-generics/intrinsics-type_name-as-const-argument.min.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:44
33
|
44
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
5-
| ^ non-trivial anonymous constants must not depend on the parameter `T`
5+
| ^ cannot perform const operation using `T`
66
|
7-
= note: type parameters are currently not permitted in anonymous constants
7+
= note: type parameters may not be used in const expressions
88

99
error: `&'static str` is forbidden as the type of a const generic parameter
1010
--> $DIR/intrinsics-type_name-as-const-argument.rs:10:22

src/test/ui/const-generics/intrinsics-type_name-as-const-argument.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait Trait<const S: &'static str> {}
1313
struct Bug<T>
1414
where
1515
T: Trait<{std::intrinsics::type_name::<T>()}>
16-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
16+
//[min]~^ ERROR generic parameters may not be used in const operations
1717
//[full]~^^ ERROR constant expression depends on a generic parameter
1818
{
1919
t: T
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/issue-61522-array-len-succ.rs:7:45
33
|
44
LL | pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
5-
| ^^^^^ non-trivial anonymous constants must not depend on the parameter `COUNT`
5+
| ^^^^^ cannot perform const operation using `COUNT`
66
|
7-
= help: it is currently only allowed to use either `COUNT` or `{ COUNT }` as generic constants
7+
= help: const parameters may only be used as standalone arguments, i.e. `COUNT`
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/issue-61522-array-len-succ.rs:12:30
1111
|
1212
LL | fn inner(&self) -> &[u8; COUNT + 1] {
13-
| ^^^^^ non-trivial anonymous constants must not depend on the parameter `COUNT`
13+
| ^^^^^ cannot perform const operation using `COUNT`
1414
|
15-
= help: it is currently only allowed to use either `COUNT` or `{ COUNT }` as generic constants
15+
= help: const parameters may only be used as standalone arguments, i.e. `COUNT`
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/const-generics/issue-61522-array-len-succ.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
88
//[full]~^ ERROR constant expression depends on a generic parameter
9-
//[min]~^^ ERROR generic parameters must not be used
9+
//[min]~^^ ERROR generic parameters may not be used
1010

1111
impl<const COUNT: usize> MyArray<COUNT> {
1212
fn inner(&self) -> &[u8; COUNT + 1] {
1313
//[full]~^ ERROR constant expression depends on a generic parameter
14-
//[min]~^^ ERROR generic parameters must not be used
14+
//[min]~^^ ERROR generic parameters may not be used
1515
&self.0
1616
}
1717
}

src/test/ui/const-generics/issue-67375.min.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/issue-67375.rs:9:25
33
|
44
LL | inner: [(); { [|_: &T| {}; 0].len() }],
5-
| ^ non-trivial anonymous constants must not depend on the parameter `T`
5+
| ^ cannot perform const operation using `T`
66
|
7-
= note: type parameters are currently not permitted in anonymous constants
7+
= note: type parameters may not be used in const expressions
88

99
error[E0392]: parameter `T` is never used
1010
--> $DIR/issue-67375.rs:7:12

src/test/ui/const-generics/issue-67375.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
struct Bug<T> {
88
//~^ ERROR parameter `T` is never used
99
inner: [(); { [|_: &T| {}; 0].len() }],
10-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
10+
//[min]~^ ERROR generic parameters may not be used in const operations
1111
//[full]~^^ WARN cannot use constants which depend on generic parameters in types
1212
//[full]~^^^ WARN this was previously accepted by the compiler
1313
}

src/test/ui/const-generics/issue-67945-1.min.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: generic parameters must not be used inside of non-trivial constant values
1+
error: generic parameters may not be used in const operations
22
--> $DIR/issue-67945-1.rs:14:16
33
|
44
LL | let x: S = MaybeUninit::uninit();
5-
| ^ non-trivial anonymous constants must not depend on the parameter `S`
5+
| ^ cannot perform const operation using `S`
66
|
7-
= note: type parameters are currently not permitted in anonymous constants
7+
= note: type parameters may not be used in const expressions
88

9-
error: generic parameters must not be used inside of non-trivial constant values
9+
error: generic parameters may not be used in const operations
1010
--> $DIR/issue-67945-1.rs:17:45
1111
|
1212
LL | let b = &*(&x as *const _ as *const S);
13-
| ^ non-trivial anonymous constants must not depend on the parameter `S`
13+
| ^ cannot perform const operation using `S`
1414
|
15-
= note: type parameters are currently not permitted in anonymous constants
15+
= note: type parameters may not be used in const expressions
1616

1717
error[E0392]: parameter `S` is never used
1818
--> $DIR/issue-67945-1.rs:11:12

src/test/ui/const-generics/issue-67945-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ struct Bug<S> {
1212
//~^ ERROR parameter `S` is never used
1313
A: [(); {
1414
let x: S = MaybeUninit::uninit();
15-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
15+
//[min]~^ ERROR generic parameters may not be used in const operations
1616
//[full]~^^ ERROR mismatched types
1717
let b = &*(&x as *const _ as *const S);
18-
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
18+
//[min]~^ ERROR generic parameters may not be used in const operations
1919
0
2020
}],
2121
}

0 commit comments

Comments
 (0)