Skip to content

Commit 3631d29

Browse files
committed
Add tests for const_generics
1 parent 96eb68b commit 3631d29

24 files changed

+392
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0277]: the size for values of type `T` cannot be known at compilation time
2+
--> $DIR/const-argument-if-length.rs:8:28
3+
|
4+
LL | pub const fn is_zst<T: ?Sized>() -> usize {
5+
| - this type parameter needs to be `Sized`
6+
LL | if std::mem::size_of::<T>() == 0 {
7+
| ^ doesn't have a size known at compile-time
8+
|
9+
::: $SRC_DIR/core/src/mem/mod.rs:LL:COL
10+
|
11+
LL | pub const fn size_of<T>() -> usize {
12+
| - required by this bound in `std::mem::size_of`
13+
14+
error[E0080]: evaluation of constant value failed
15+
--> $DIR/const-argument-if-length.rs:19:15
16+
|
17+
LL | pad: [u8; is_zst::<T>()],
18+
| ^^^^^^^^^^^^^ referenced constant has errors
19+
20+
error[E0277]: the size for values of type `T` cannot be known at compilation time
21+
--> $DIR/const-argument-if-length.rs:17:12
22+
|
23+
LL | pub struct AtLeastByte<T: ?Sized> {
24+
| - this type parameter needs to be `Sized`
25+
LL | value: T,
26+
| ^ doesn't have a size known at compile-time
27+
|
28+
= note: only the last field of a struct may have a dynamically sized type
29+
= help: change the field's type to have a statically known size
30+
help: borrowed types always have a statically known size
31+
|
32+
LL | value: &T,
33+
| ^
34+
help: the `Box` type always has a statically known size and allocates its contents in the heap
35+
|
36+
LL | value: Box<T>,
37+
| ^^^^ ^
38+
39+
error: aborting due to 3 previous errors
40+
41+
Some errors have detailed explanations: E0080, E0277.
42+
For more information about an error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/const-argument-if-length.rs:19:24
3+
|
4+
LL | pad: [u8; is_zst::<T>()],
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error[E0277]: the size for values of type `T` cannot be known at compilation time
10+
--> $DIR/const-argument-if-length.rs:17:12
11+
|
12+
LL | pub struct AtLeastByte<T: ?Sized> {
13+
| - this type parameter needs to be `Sized`
14+
LL | value: T,
15+
| ^ doesn't have a size known at compile-time
16+
|
17+
= note: only the last field of a struct may have a dynamically sized type
18+
= help: change the field's type to have a statically known size
19+
help: borrowed types always have a statically known size
20+
|
21+
LL | value: &T,
22+
| ^
23+
help: the `Box` type always has a statically known size and allocates its contents in the heap
24+
|
25+
LL | value: Box<T>,
26+
| ^^^^ ^
27+
28+
error: aborting due to 2 previous errors
29+
30+
For more information about this error, try `rustc --explain E0277`.

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
26

37
pub const fn is_zst<T: ?Sized>() -> usize {
48
if std::mem::size_of::<T>() == 0 {
9+
//[full]~^ ERROR the size for values of type `T` cannot be known at compilation time
510
1
611
} else {
712
0
@@ -12,7 +17,8 @@ pub struct AtLeastByte<T: ?Sized> {
1217
value: T,
1318
//~^ ERROR the size for values of type `T` cannot be known at compilation time
1419
pad: [u8; is_zst::<T>()],
15-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
20+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
21+
//[full]~^^ ERROR evaluation of constant value failed
1622
}
1723

1824
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/generic-function-call-in-array-length.rs:9:29
3+
|
4+
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/generic-function-call-in-array-length.rs:9:39
3+
|
4+
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
5+
| ^ non-trivial anonymous constants must not depend on the parameter `N`
6+
|
7+
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
8+
9+
error: generic parameters must not be used inside of non-trivial constant values
10+
--> $DIR/generic-function-call-in-array-length.rs:12:13
11+
|
12+
LL | [0; foo(N)]
13+
| ^ non-trivial anonymous constants must not depend on the parameter `N`
14+
|
15+
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
26

37
const fn foo(n: usize) -> usize { n * 2 }
48

59
fn bar<const N: usize>() -> [u32; foo(N)] {
6-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
10+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
11+
//[full]~^^ ERROR constant expression depends on a generic parameter
712
[0; foo(N)]
8-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
13+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
914
}
1015

1116
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/generic-sum-in-array-length.rs:7:45
3+
|
4+
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/generic-sum-in-array-length.rs:7:53
3+
|
4+
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`
6+
|
7+
= help: it is currently only allowed to use either `A` or `{ A }` as generic constants
8+
9+
error: generic parameters must not be used inside of non-trivial constant values
10+
--> $DIR/generic-sum-in-array-length.rs:7:57
11+
|
12+
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`
14+
|
15+
= help: it is currently only allowed to use either `B` or `{ B }` as generic constants
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
26

37
fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
4-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
5-
//~| ERROR generic parameters must not be used inside of non-trivial constant values
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
10+
//[full]~^^^ ERROR constant expression depends on a generic parameter
611

712
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:8
3+
|
4+
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:44
3+
|
4+
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error: `&'static str` is forbidden as the type of a const generic parameter
10+
--> $DIR/intrinsics-type_name-as-const-argument.rs:10:22
11+
|
12+
LL | trait Trait<const S: &'static str> {}
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: the only supported types are integers, `bool` and `char`
16+
= note: more complex types are supported with `#[feature(const_generics)]`
17+
18+
error: aborting due to 2 previous errors
19+

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
27
#![feature(core_intrinsics)]
8+
#![feature(const_type_name)]
39

410
trait Trait<const S: &'static str> {}
5-
//~^ ERROR `&'static str` is forbidden as the type of a const generic parameter
11+
//[min]~^ ERROR `&'static str` is forbidden as the type of a const generic parameter
612

713
struct Bug<T>
814
where
915
T: Trait<{std::intrinsics::type_name::<T>()}>
10-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
16+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
17+
//[full]~^^ ERROR constant expression depends on a generic parameter
1118
{
1219
t: T
1320
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: cannot use constants which depend on generic parameters in types
2+
--> $DIR/issue-67375.rs:9:12
3+
|
4+
LL | inner: [(); { [|_: &T| {}; 0].len() }],
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(const_evaluatable_unchecked)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
10+
11+
error[E0392]: parameter `T` is never used
12+
--> $DIR/issue-67375.rs:7:12
13+
|
14+
LL | struct Bug<T> {
15+
| ^ unused parameter
16+
|
17+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
18+
19+
error: aborting due to previous error; 1 warning emitted
20+
21+
For more information about this error, try `rustc --explain E0392`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/issue-67375.rs:9:25
3+
|
4+
LL | inner: [(); { [|_: &T| {}; 0].len() }],
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error[E0392]: parameter `T` is never used
10+
--> $DIR/issue-67375.rs:7:12
11+
|
12+
LL | struct Bug<T> {
13+
| ^ unused parameter
14+
|
15+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0392`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
26

37
struct Bug<T> {
48
//~^ ERROR parameter `T` is never used
59
inner: [(); { [|_: &T| {}; 0].len() }],
6-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
10+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
11+
//[full]~^^ WARN cannot use constants which depend on generic parameters in types
12+
//[full]~^^^ WARN this was previously accepted by the compiler
713
}
814

915
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-67945-1.rs:14:20
3+
|
4+
LL | struct Bug<S> {
5+
| - this type parameter
6+
...
7+
LL | let x: S = MaybeUninit::uninit();
8+
| - ^^^^^^^^^^^^^^^^^^^^^ expected type parameter `S`, found union `MaybeUninit`
9+
| |
10+
| expected due to this
11+
|
12+
= note: expected type parameter `S`
13+
found union `MaybeUninit<_>`
14+
15+
error[E0392]: parameter `S` is never used
16+
--> $DIR/issue-67945-1.rs:11:12
17+
|
18+
LL | struct Bug<S> {
19+
| ^ unused parameter
20+
|
21+
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
22+
23+
error: aborting due to 2 previous errors
24+
25+
Some errors have detailed explanations: E0308, E0392.
26+
For more information about an error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: generic parameters must not be used inside of non-trivial constant values
2+
--> $DIR/issue-67945-1.rs:14:16
3+
|
4+
LL | let x: S = MaybeUninit::uninit();
5+
| ^ non-trivial anonymous constants must not depend on the parameter `S`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error: generic parameters must not be used inside of non-trivial constant values
10+
--> $DIR/issue-67945-1.rs:17:45
11+
|
12+
LL | let b = &*(&x as *const _ as *const S);
13+
| ^ non-trivial anonymous constants must not depend on the parameter `S`
14+
|
15+
= note: type parameters are currently not permitted in anonymous constants
16+
17+
error[E0392]: parameter `S` is never used
18+
--> $DIR/issue-67945-1.rs:11:12
19+
|
20+
LL | struct Bug<S> {
21+
| ^ unused parameter
22+
|
23+
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0392`.

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#![feature(min_const_generics)]
1+
// revisions: full min
2+
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(full, feature(const_generics))]
5+
#![cfg_attr(min, feature(min_const_generics))]
26

37
use std::marker::PhantomData;
48

@@ -8,9 +12,10 @@ struct Bug<S> {
812
//~^ ERROR parameter `S` is never used
913
A: [(); {
1014
let x: S = MaybeUninit::uninit();
11-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
15+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
16+
//[full]~^^ ERROR mismatched types
1217
let b = &*(&x as *const _ as *const S);
13-
//~^ ERROR generic parameters must not be used inside of non-trivial constant values
18+
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
1419
0
1520
}],
1621
}

0 commit comments

Comments
 (0)