Skip to content

Commit 17cd790

Browse files
committed
support as _ and add tests
1 parent c318364 commit 17cd790

File tree

6 files changed

+220
-3
lines changed

6 files changed

+220
-3
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
431431
}
432432
}
433433
// These are not actually relevant for us here, so we can ignore them.
434-
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => Ok(()),
434+
StatementKind::AscribeUserType(..)
435+
| StatementKind::StorageLive(_)
436+
| StatementKind::StorageDead(_) => Ok(()),
435437
_ => self.error(Some(stmt.source_info.span), "unsupported statement")?,
436438
}
437439
}

src/test/ui/const-generics/const_evaluatable_checked/abstract-const-as-cast.rs renamed to src/test/ui/const-generics/const_evaluatable_checked/abstract-const-as-cast-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ struct Foo<const N: u8>([u8; N as usize])
66
where
77
[(); N as usize]:;
88

9+
struct Bar<const N: u8>([u8; (N + 2) as usize]) where [(); (N + 2) as usize]:;
910

1011
// unifying with subtrees
1112
struct Evaluatable<const N: u16>;
1213
fn foo<const N: u8>() where Evaluatable<{N as usize as u16 }>: {
1314
let _ = Foo::<N>([1; N as usize]);
1415
}
1516

16-
struct Bar<const N: u8>([u8; (N + 2) as usize]) where [(); (N + 2) as usize]:;
1717

1818
fn main() {}

src/test/ui/const-generics/const_evaluatable_checked/abstract-const-as-cast-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Foo2<const N: u8>(Evaluatable::<{N as u128}>) where Evaluatable<{N as usi
1414
//~| help: try adding a `where` bound using this expression: `where [(); {N as u128}]:`
1515

1616
struct Bar<const N: u8>([u8; (N + 2) as usize]) where [(); (N + 1) as usize]:;
17-
//~^ unconstrained generic constant
17+
//~^ Error: unconstrained generic constant
1818
//~| help: try adding a `where` bound using this expression: `where [(); (N + 2) as usize]:`
1919

2020
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait {}
5+
pub struct EvaluatableU128<const N: u128>;
6+
7+
struct HasCastInTraitImpl<const N: usize, const M: u128>;
8+
impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
9+
10+
pub fn use_trait_impl<const N: usize>()
11+
where
12+
[(); { N + 1}]:,
13+
EvaluatableU128<{N as u128}>:, {
14+
fn assert_impl<T: Trait>() {}
15+
16+
// errors are bad but seems to be pre-existing issue #86198
17+
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
18+
//~^ Error: mismatched types
19+
//~^^ Error: unconstrained generic constant
20+
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
21+
//~^ Error: mismatched types
22+
//~^^ Error: unconstrained generic constant
23+
assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
24+
//~^ Error: mismatched types
25+
assert_impl::<HasCastInTraitImpl<14, 13>>();
26+
//~^ Error: mismatched types
27+
}
28+
pub fn use_trait_impl_2<const N: usize>()
29+
where
30+
[(); { N + 1}]:,
31+
EvaluatableU128<{N as _}>:, {
32+
fn assert_impl<T: Trait>() {}
33+
34+
// errors are bad but seems to be pre-existing issue #86198
35+
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
36+
//~^ Error: mismatched types
37+
//~^^ Error: unconstrained generic constant
38+
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
39+
//~^ Error: mismatched types
40+
//~^^ Error: unconstrained generic constant
41+
assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
42+
//~^ Error: mismatched types
43+
assert_impl::<HasCastInTraitImpl<14, 13>>();
44+
//~^ Error: mismatched types
45+
}
46+
47+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
error: unconstrained generic constant
2+
--> $DIR/abstract-const-as-cast-3.rs:17:5
3+
|
4+
LL | fn assert_impl<T: Trait>() {}
5+
| ----- required by this bound in `use_trait_impl::assert_impl`
6+
...
7+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
|
10+
= help: try adding a `where` bound using this expression: `where [(); { O as u128 }]:`
11+
note: required because of the requirements on the impl of `Trait` for `HasCastInTraitImpl<{ N + 1 }, { N as u128 }>`
12+
--> $DIR/abstract-const-as-cast-3.rs:8:22
13+
|
14+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
15+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/abstract-const-as-cast-3.rs:17:5
19+
|
20+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
22+
|
23+
= note: expected type `{ N as u128 }`
24+
found type `{ O as u128 }`
25+
26+
error: unconstrained generic constant
27+
--> $DIR/abstract-const-as-cast-3.rs:20:5
28+
|
29+
LL | fn assert_impl<T: Trait>() {}
30+
| ----- required by this bound in `use_trait_impl::assert_impl`
31+
...
32+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
= help: try adding a `where` bound using this expression: `where [(); { O as u128 }]:`
36+
note: required because of the requirements on the impl of `Trait` for `HasCastInTraitImpl<{ N + 1 }, { N as _ }>`
37+
--> $DIR/abstract-const-as-cast-3.rs:8:22
38+
|
39+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
40+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
42+
error[E0308]: mismatched types
43+
--> $DIR/abstract-const-as-cast-3.rs:20:5
44+
|
45+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
47+
|
48+
= note: expected type `{ N as _ }`
49+
found type `{ O as u128 }`
50+
51+
error[E0308]: mismatched types
52+
--> $DIR/abstract-const-as-cast-3.rs:23:5
53+
|
54+
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12_u128`, found `13_u128`
56+
|
57+
= note: expected type `12_u128`
58+
found type `13_u128`
59+
60+
error[E0308]: mismatched types
61+
--> $DIR/abstract-const-as-cast-3.rs:25:5
62+
|
63+
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13_u128`, found `14_u128`
65+
|
66+
= note: expected type `13_u128`
67+
found type `14_u128`
68+
69+
error: unconstrained generic constant
70+
--> $DIR/abstract-const-as-cast-3.rs:35:5
71+
|
72+
LL | fn assert_impl<T: Trait>() {}
73+
| ----- required by this bound in `use_trait_impl_2::assert_impl`
74+
...
75+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
76+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
|
78+
= help: try adding a `where` bound using this expression: `where [(); { O as u128 }]:`
79+
note: required because of the requirements on the impl of `Trait` for `HasCastInTraitImpl<{ N + 1 }, { N as u128 }>`
80+
--> $DIR/abstract-const-as-cast-3.rs:8:22
81+
|
82+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
83+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
85+
error[E0308]: mismatched types
86+
--> $DIR/abstract-const-as-cast-3.rs:35:5
87+
|
88+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
90+
|
91+
= note: expected type `{ N as u128 }`
92+
found type `{ O as u128 }`
93+
94+
error: unconstrained generic constant
95+
--> $DIR/abstract-const-as-cast-3.rs:38:5
96+
|
97+
LL | fn assert_impl<T: Trait>() {}
98+
| ----- required by this bound in `use_trait_impl_2::assert_impl`
99+
...
100+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
|
103+
= help: try adding a `where` bound using this expression: `where [(); { O as u128 }]:`
104+
note: required because of the requirements on the impl of `Trait` for `HasCastInTraitImpl<{ N + 1 }, { N as _ }>`
105+
--> $DIR/abstract-const-as-cast-3.rs:8:22
106+
|
107+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
108+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109+
110+
error[E0308]: mismatched types
111+
--> $DIR/abstract-const-as-cast-3.rs:38:5
112+
|
113+
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
114+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
115+
|
116+
= note: expected type `{ N as _ }`
117+
found type `{ O as u128 }`
118+
119+
error[E0308]: mismatched types
120+
--> $DIR/abstract-const-as-cast-3.rs:41:5
121+
|
122+
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
123+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12_u128`, found `13_u128`
124+
|
125+
= note: expected type `12_u128`
126+
found type `13_u128`
127+
128+
error[E0308]: mismatched types
129+
--> $DIR/abstract-const-as-cast-3.rs:43:5
130+
|
131+
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
132+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13_u128`, found `14_u128`
133+
|
134+
= note: expected type `13_u128`
135+
found type `14_u128`
136+
137+
error: aborting due to 12 previous errors
138+
139+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// check-pass
2+
#![feature(const_evaluatable_checked, const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
trait Trait {}
6+
pub struct EvaluatableU128<const N: u128>;
7+
8+
struct HasCastInTraitImpl<const N: usize, const M: u128>;
9+
impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
10+
11+
pub fn use_trait_impl<const N: usize>() where EvaluatableU128<{N as u128}>:, {
12+
fn assert_impl<T: Trait>() {}
13+
14+
assert_impl::<HasCastInTraitImpl<N, { N as u128 }>>();
15+
assert_impl::<HasCastInTraitImpl<N, { N as _ }>>();
16+
assert_impl::<HasCastInTraitImpl<12, { 12 as u128 }>>();
17+
assert_impl::<HasCastInTraitImpl<13, 13>>();
18+
}
19+
pub fn use_trait_impl_2<const N: usize>() where EvaluatableU128<{N as _}>:, {
20+
fn assert_impl<T: Trait>() {}
21+
22+
assert_impl::<HasCastInTraitImpl<N, { N as u128 }>>();
23+
assert_impl::<HasCastInTraitImpl<N, { N as _ }>>();
24+
assert_impl::<HasCastInTraitImpl<12, { 12 as u128 }>>();
25+
assert_impl::<HasCastInTraitImpl<13, 13>>();
26+
}
27+
28+
29+
fn main() {}

0 commit comments

Comments
 (0)