Skip to content

Commit 49f2384

Browse files
authored
Rollup merge of #90114 - BoxyUwU:cg_defaults_tests, r=lcnr
Add some tests for const_generics_defaults I think this covers some of the stuff required for stabilisation report, some of these tests are probably covering stuff we already have but it can't hurt to have more :) r? ````@lcnr````
2 parents de306d7 + e7a9e82 commit 49f2384

13 files changed

+294
-2
lines changed

src/test/ui/const-generics/defaults/default-annotation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(staged_api)]
33
#![feature(const_generics_defaults)]
44
#![allow(incomplete_features)]
5-
// FIXME(const_generics): It seems like we aren't testing the right thing here,
5+
// FIXME(const_generics_defaults): It seems like we aren't testing the right thing here,
66
// I would assume that we want the attributes to apply to the const parameter defaults
77
// themselves.
88
#![stable(feature = "const_default_test", since="none")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_generics_defaults)]
2+
3+
// test that defaulted const params are not used to help type inference
4+
5+
struct Foo<const N: u32 = 2>;
6+
7+
impl<const N: u32> Foo<N> {
8+
fn foo() -> Self { loop {} }
9+
}
10+
11+
fn main() {
12+
let foo = Foo::<1>::foo();
13+
let foo = Foo::foo();
14+
//~^ error: type annotations needed for `Foo<{_: u32}>`
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed for `Foo<{_: u32}>`
2+
--> $DIR/doesnt_infer.rs:13:15
3+
|
4+
LL | let foo = Foo::foo();
5+
| --- ^^^^^^^^ cannot infer the value of const parameter `N`
6+
| |
7+
| consider giving `foo` the explicit type `Foo<{_: u32}>`, where the type parameter `N` is specified
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-pass
2+
#![feature(const_generics_defaults)]
3+
4+
struct Uwu<const N: u32 = 1, const M: u32 = N>;
5+
6+
trait Trait {}
7+
impl<const N: u32> Trait for Uwu<N> {}
8+
9+
fn rawr<const N: u32>() -> impl Trait {
10+
Uwu::<N>
11+
}
12+
13+
trait Traitor<const N: u8 = 1, const M: u8 = N> { }
14+
15+
impl<const N: u8> Traitor<N> for u32 {}
16+
impl Traitor<1, 1> for u64 {}
17+
18+
fn uwu<const N: u8>() -> impl Traitor<N> {
19+
1_u32
20+
}
21+
22+
fn owo() -> impl Traitor {
23+
1_u64
24+
}
25+
26+
fn main() {
27+
rawr::<3>();
28+
rawr::<7>();
29+
uwu::<{ u8::MAX }>();
30+
owo();
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(const_generics_defaults)]
2+
3+
struct Uwu<const N: u32 = 1, const M: u32 = N>;
4+
5+
trait Trait {}
6+
impl<const N: u32> Trait for Uwu<N> {}
7+
8+
fn rawr() -> impl Trait {
9+
//~^ error: the trait bound `Uwu<10_u32, 12_u32>: Trait` is not satisfied
10+
Uwu::<10, 12>
11+
}
12+
13+
trait Traitor<const N: u8 = 1, const M: u8 = N> { }
14+
15+
impl<const N: u8> Traitor<N, 2> for u32 {}
16+
impl Traitor<1, 2> for u64 {}
17+
18+
19+
fn uwu<const N: u8>() -> impl Traitor<N> {
20+
//~^ error: the trait bound `u32: Traitor<N, N>` is not satisfied
21+
1_u32
22+
}
23+
24+
fn owo() -> impl Traitor {
25+
//~^ error: the trait bound `u64: Traitor<1_u8, 1_u8>` is not satisfied
26+
1_u64
27+
}
28+
29+
fn main() {
30+
rawr();
31+
uwu();
32+
owo();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0277]: the trait bound `Uwu<10_u32, 12_u32>: Trait` is not satisfied
2+
--> $DIR/rp_impl_trait_fail.rs:8:14
3+
|
4+
LL | fn rawr() -> impl Trait {
5+
| ^^^^^^^^^^ the trait `Trait` is not implemented for `Uwu<10_u32, 12_u32>`
6+
|
7+
= help: the following implementations were found:
8+
<Uwu<N> as Trait>
9+
10+
error[E0277]: the trait bound `u32: Traitor<N, N>` is not satisfied
11+
--> $DIR/rp_impl_trait_fail.rs:19:26
12+
|
13+
LL | fn uwu<const N: u8>() -> impl Traitor<N> {
14+
| ^^^^^^^^^^^^^^^ the trait `Traitor<N, N>` is not implemented for `u32`
15+
|
16+
= help: the following implementations were found:
17+
<u32 as Traitor<N, 2_u8>>
18+
19+
error[E0277]: the trait bound `u64: Traitor<1_u8, 1_u8>` is not satisfied
20+
--> $DIR/rp_impl_trait_fail.rs:24:13
21+
|
22+
LL | fn owo() -> impl Traitor {
23+
| ^^^^^^^^^^^^ the trait `Traitor<1_u8, 1_u8>` is not implemented for `u64`
24+
|
25+
= help: the following implementations were found:
26+
<u64 as Traitor<1_u8, 2_u8>>
27+
28+
error: aborting due to 3 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,45 @@
1+
// run-pass
2+
#![feature(const_generics_defaults)]
3+
4+
trait Trait<const N: u8 = 12> {
5+
fn uwu(&self) -> u8 {
6+
N
7+
}
8+
}
9+
10+
impl Trait for u32 {}
11+
12+
impl Trait<12> for u64 {
13+
fn uwu(&self) -> u8 {
14+
*self as u8
15+
}
16+
}
17+
18+
fn foo(arg: &dyn Trait) -> u8 {
19+
arg.uwu()
20+
}
21+
22+
trait Traitor<const N: u8 = 1, const M: u8 = N> {
23+
fn owo(&self) -> u8 {
24+
M
25+
}
26+
}
27+
28+
impl Traitor<2> for bool { }
29+
impl Traitor for u8 {
30+
fn owo(&self) -> u8 {
31+
*self
32+
}
33+
}
34+
35+
fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {
36+
arg.owo()
37+
}
38+
39+
fn main() {
40+
assert_eq!(foo(&10_u32), 12);
41+
assert_eq!(foo(&3_u64), 3);
42+
43+
assert_eq!(bar(&true), 2);
44+
assert_eq!(bar(&1_u8), 1);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(const_generics_defaults)]
2+
3+
trait Trait<const N: u8 = 12> {
4+
fn uwu(&self) -> u8 {
5+
N
6+
}
7+
}
8+
9+
impl Trait<2> for u32 {}
10+
11+
fn foo(arg: &dyn Trait) -> u8 {
12+
arg.uwu()
13+
}
14+
15+
trait Traitor<const N: u8 = 1, const M: u8 = N> {
16+
fn owo(&self) -> u8 {
17+
M
18+
}
19+
}
20+
21+
impl Traitor<2, 3> for bool { }
22+
23+
fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {
24+
arg.owo()
25+
}
26+
27+
fn main() {
28+
foo(&10_u32);
29+
//~^ error: the trait bound `u32: Trait` is not satisfied
30+
bar(&true);
31+
//~^ error: the trait bound `bool: Traitor<{_: u8}, {_: u8}>` is not satisfied
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0277]: the trait bound `u32: Trait` is not satisfied
2+
--> $DIR/trait_objects_fail.rs:28:9
3+
|
4+
LL | foo(&10_u32);
5+
| --- ^^^^^^^ the trait `Trait` is not implemented for `u32`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the following implementations were found:
10+
<u32 as Trait<2_u8>>
11+
= note: required for the cast to the object type `dyn Trait`
12+
13+
error[E0277]: the trait bound `bool: Traitor<{_: u8}, {_: u8}>` is not satisfied
14+
--> $DIR/trait_objects_fail.rs:30:9
15+
|
16+
LL | bar(&true);
17+
| --- ^^^^^ the trait `Traitor<{_: u8}, {_: u8}>` is not implemented for `bool`
18+
| |
19+
| required by a bound introduced by this call
20+
|
21+
= help: the following implementations were found:
22+
<bool as Traitor<2_u8, 3_u8>>
23+
= note: required for the cast to the object type `dyn Traitor<{_: u8}, {_: u8}>`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(const_generics_defaults)]
2+
3+
struct Ooopsies<const N: u8 = { u8::MAX + 1 }>;
4+
//~^ error: evaluation of constant value failed
5+
6+
trait Trait<const N: u8> {}
7+
impl Trait<3> for () {}
8+
struct WhereClause<const N: u8 = 2> where (): Trait<N>;
9+
//~^ error: the trait bound `(): Trait<2_u8>` is not satisfied
10+
11+
trait Traitor<T, const N: u8> {}
12+
struct WhereClauseTooGeneric<T = u32, const N: u8 = 2>(T) where (): Traitor<T, N>;
13+
14+
// no error on struct def
15+
struct DependentDefaultWfness<const N: u8 = 1, T = WhereClause<N>>(T);
16+
fn foo() -> DependentDefaultWfness {
17+
//~^ error: the trait bound `(): Trait<1_u8>` is not satisfied
18+
loop {}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/wfness.rs:3:33
3+
|
4+
LL | struct Ooopsies<const N: u8 = { u8::MAX + 1 }>;
5+
| ^^^^^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
6+
7+
error[E0277]: the trait bound `(): Trait<2_u8>` is not satisfied
8+
--> $DIR/wfness.rs:8:47
9+
|
10+
LL | struct WhereClause<const N: u8 = 2> where (): Trait<N>;
11+
| ^^^^^^^^ the trait `Trait<2_u8>` is not implemented for `()`
12+
|
13+
= help: the following implementations were found:
14+
<() as Trait<3_u8>>
15+
note: required by `WhereClause`
16+
--> $DIR/wfness.rs:8:1
17+
|
18+
LL | struct WhereClause<const N: u8 = 2> where (): Trait<N>;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error[E0277]: the trait bound `(): Trait<1_u8>` is not satisfied
22+
--> $DIR/wfness.rs:16:13
23+
|
24+
LL | fn foo() -> DependentDefaultWfness {
25+
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<1_u8>` is not implemented for `()`
26+
|
27+
= help: the following implementations were found:
28+
<() as Trait<3_u8>>
29+
note: required by a bound in `WhereClause`
30+
--> $DIR/wfness.rs:8:47
31+
|
32+
LL | struct WhereClause<const N: u8 = 2> where (): Trait<N>;
33+
| ^^^^^^^^ required by this bound in `WhereClause`
34+
35+
error: aborting due to 3 previous errors
36+
37+
Some errors have detailed explanations: E0080, E0277.
38+
For more information about an error, try `rustc --explain E0080`.

src/test/ui/const-generics/defaults/wrong-order.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ struct A<T = u32, const N: usize> {
55
arg: T,
66
}
77

8+
struct Foo<const N: u8 = 3, T>(T);
9+
//~^ error: generic parameters with a default must be trailing
10+
811
fn main() {}

src/test/ui/const-generics/defaults/wrong-order.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: generic parameters with a default must be trailing
44
LL | struct A<T = u32, const N: usize> {
55
| ^
66

7-
error: aborting due to previous error
7+
error: generic parameters with a default must be trailing
8+
--> $DIR/wrong-order.rs:8:18
9+
|
10+
LL | struct Foo<const N: u8 = 3, T>(T);
11+
| ^
12+
13+
error: aborting due to 2 previous errors
814

0 commit comments

Comments
 (0)