Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3631d29

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

24 files changed

+392
-21
lines changed
Lines changed: 42 additions & 0 deletions
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`.
Lines changed: 30 additions & 0 deletions
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

Lines changed: 8 additions & 2 deletions
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() {}
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 8 additions & 3 deletions
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() {}
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 8 additions & 3 deletions
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() {}
Lines changed: 10 additions & 0 deletions
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+

0 commit comments

Comments
 (0)