Skip to content

Commit 1db05e0

Browse files
committed
Add various min_const_generics regression tests
1 parent 0801263 commit 1db05e0

19 files changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(min_const_generics)]
2+
3+
pub const fn is_zst<T: ?Sized>() -> usize {
4+
if std::mem::size_of::<T>() == 0 {
5+
1
6+
} else {
7+
0
8+
}
9+
}
10+
11+
pub struct AtLeastByte<T: ?Sized> {
12+
value: T,
13+
//~^ ERROR the size for values of type `T` cannot be known at compilation time
14+
pad: [u8; is_zst::<T>()],
15+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
16+
}
17+
18+
fn main() {}
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:14: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:12: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(min_const_generics)]
2+
3+
const fn foo(n: usize) -> usize { n * 2 }
4+
5+
fn bar<const N: usize>() -> [u32; foo(N)] {
6+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
7+
[0; foo(N)]
8+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(min_const_generics)]
2+
3+
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
6+
7+
fn main() {}
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:3: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:3: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
@@ -0,0 +1,15 @@
1+
#![feature(min_const_generics)]
2+
#![feature(core_intrinsics)]
3+
4+
trait Trait<const S: &'static str> {}
5+
//~^ ERROR `&'static str` is forbidden as the type of a const generic parameter
6+
7+
struct Bug<T>
8+
where
9+
T: Trait<{std::intrinsics::type_name::<T>()}>
10+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
11+
{
12+
t: T
13+
}
14+
15+
fn main() {}
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:9: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:4: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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(min_const_generics)]
2+
3+
struct Bug<T> {
4+
//~^ ERROR parameter `T` is never used
5+
inner: [(); { [|_: &T| {}; 0].len() }],
6+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
7+
}
8+
9+
fn main() {}
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:5: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:3: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
@@ -0,0 +1,18 @@
1+
#![feature(min_const_generics)]
2+
3+
use std::marker::PhantomData;
4+
5+
use std::mem::{self, MaybeUninit};
6+
7+
struct Bug<S> {
8+
//~^ ERROR parameter `S` is never used
9+
A: [(); {
10+
let x: S = MaybeUninit::uninit();
11+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
12+
let b = &*(&x as *const _ as *const S);
13+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
14+
0
15+
}],
16+
}
17+
18+
fn main() {}
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:10: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:12: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:7: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(min_const_generics)]
2+
3+
use std::mem::MaybeUninit;
4+
5+
struct Bug<S> {
6+
//~^ ERROR parameter `S` is never used
7+
A: [(); {
8+
let x: S = MaybeUninit::uninit();
9+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
10+
let b = &*(&x as *const _ as *const S);
11+
//~^ ERROR generic parameters must not be used inside of non trivial constant values
12+
0
13+
}],
14+
}
15+
16+
fn main() {}
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-2.rs:8: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-2.rs:10: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-2.rs:5: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(min_const_generics)]
2+
3+
struct Bug<S: ?Sized> {
4+
A: [(); {
5+
let x: Option<Box<Self>> = None;
6+
//~^ ERROR generic `Self` types are currently not permitted in anonymous constants
7+
0
8+
}],
9+
B: S
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: generic `Self` types are currently not permitted in anonymous constants
2+
--> $DIR/issue-67945-3.rs:5:27
3+
|
4+
LL | let x: Option<Box<Self>> = None;
5+
| ^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(min_const_generics)]
2+
3+
fn a<const X: &'static [u32]>() {}
4+
//~^ ERROR `&'static [u32]` is forbidden as the type of a const generic parameter
5+
6+
fn main() {
7+
a::<{&[]}>();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: `&'static [u32]` is forbidden as the type of a const generic parameter
2+
--> $DIR/static-reference-array-const-param.rs:3:15
3+
|
4+
LL | fn a<const X: &'static [u32]>() {}
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
= note: more complex types are supported with `#[feature(const_generics)]`
9+
10+
error: aborting due to previous error
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(min_const_generics)]
2+
3+
struct Const<const P: &'static ()>;
4+
//~^ ERROR `&'static ()` is forbidden as the type of a const generic parameter
5+
6+
fn main() {
7+
const A: &'static () = unsafe {
8+
std::mem::transmute(10 as *const ())
9+
};
10+
11+
let _ = Const::<{A}>;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: `&'static ()` is forbidden as the type of a const generic parameter
2+
--> $DIR/transmute-const-param-static-reference.rs:3:23
3+
|
4+
LL | struct Const<const P: &'static ()>;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
= note: more complex types are supported with `#[feature(const_generics)]`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)