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

Commit 6000b48

Browse files
committed
add tests
1 parent 7530c43 commit 6000b48

9 files changed

+225
-3
lines changed

src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// run-pass
21
#![feature(generic_const_exprs)]
32
#![allow(incomplete_features)]
43

@@ -22,8 +21,10 @@ where
2221
}
2322

2423
fn main() {
25-
// Test that we can correctly infer `T` which requires evaluating
26-
// `{ N + 1 }` which has substs containing an inference var
24+
// FIXME(generic_const_exprs): We can't correctly infer `T` which requires
25+
// evaluating `{ N + 1 }` which has substs containing an inference var
2726
let mut _q = Default::default();
27+
//~^ ERROR type annotations needed
28+
2829
_q = foo::<_, 2>(_q);
2930
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/const_eval_resolve_canonical.rs:26:9
3+
|
4+
LL | let mut _q = Default::default();
5+
| ^^^^^^ consider giving `_q` a type
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_const_exprs)]
3+
4+
trait Foo {
5+
const N: usize;
6+
}
7+
8+
impl Foo for u8 {
9+
const N: usize = 1;
10+
}
11+
12+
fn foo<T: Foo>(_: [u8; T::N]) -> T {
13+
todo!()
14+
}
15+
16+
pub fn bar() {
17+
let _: u8 = foo([0; 1]);
18+
19+
let _ = foo([0; 1]);
20+
//~^ ERROR type annotations needed
21+
}
22+
23+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/issue-83249.rs:19:13
3+
|
4+
LL | let _ = foo([0; 1]);
5+
| - ^^^ cannot infer type for type parameter `T` declared on the function `foo`
6+
| |
7+
| consider giving this pattern a type
8+
|
9+
= note: cannot satisfy `_: Foo`
10+
note: required by a bound in `foo`
11+
--> $DIR/issue-83249.rs:12:11
12+
|
13+
LL | fn foo<T: Foo>(_: [u8; T::N]) -> T {
14+
| ^^^ required by this bound in `foo`
15+
help: consider specifying the type argument in the function call
16+
|
17+
LL | let _ = foo::<T>([0; 1]);
18+
| +++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0283`.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// build-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(generic_const_exprs)]
5+
6+
use std::{marker::PhantomData, ops::Mul};
7+
8+
pub enum Nil {}
9+
pub struct Cons<T, L> {
10+
_phantom: PhantomData<(T, L)>,
11+
}
12+
13+
pub trait Indices<const N: usize> {
14+
const RANK: usize;
15+
const NUM_ELEMS: usize;
16+
}
17+
18+
impl<const N: usize> Indices<N> for Nil {
19+
const RANK: usize = 0;
20+
const NUM_ELEMS: usize = 1;
21+
}
22+
23+
impl<T, I: Indices<N>, const N: usize> Indices<N> for Cons<T, I> {
24+
const RANK: usize = I::RANK + 1;
25+
const NUM_ELEMS: usize = I::NUM_ELEMS * N;
26+
}
27+
28+
pub trait Concat<J> {
29+
type Output;
30+
}
31+
32+
impl<J> Concat<J> for Nil {
33+
type Output = J;
34+
}
35+
36+
impl<T, I, J> Concat<J> for Cons<T, I>
37+
where
38+
I: Concat<J>,
39+
{
40+
type Output = Cons<T, <I as Concat<J>>::Output>;
41+
}
42+
43+
pub struct Tensor<I: Indices<N>, const N: usize>
44+
where
45+
[u8; I::NUM_ELEMS]: Sized,
46+
{
47+
pub data: [u8; I::NUM_ELEMS],
48+
_phantom: PhantomData<I>,
49+
}
50+
51+
impl<I: Indices<N>, J: Indices<N>, const N: usize> Mul<Tensor<J, N>> for Tensor<I, N>
52+
where
53+
I: Concat<J>,
54+
<I as Concat<J>>::Output: Indices<N>,
55+
[u8; I::NUM_ELEMS]: Sized,
56+
[u8; J::NUM_ELEMS]: Sized,
57+
[u8; <I as Concat<J>>::Output::NUM_ELEMS]: Sized,
58+
{
59+
type Output = Tensor<<I as Concat<J>>::Output, N>;
60+
61+
fn mul(self, _rhs: Tensor<J, N>) -> Self::Output {
62+
Tensor {
63+
data: [0u8; <I as Concat<J>>::Output::NUM_ELEMS],
64+
_phantom: PhantomData,
65+
}
66+
}
67+
}
68+
69+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// build-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait TraitWithConst {
7+
const SOME_CONST: usize;
8+
}
9+
10+
pub trait OtherTrait: TraitWithConst {
11+
fn some_fn(self) -> [u8 ; <Self as TraitWithConst>::SOME_CONST];
12+
}
13+
14+
impl TraitWithConst for f32 {
15+
const SOME_CONST: usize = 32;
16+
}
17+
18+
impl OtherTrait for f32 {
19+
fn some_fn(self) -> [u8 ; <Self as TraitWithConst>::SOME_CONST] {
20+
[0; 32]
21+
}
22+
}
23+
24+
fn main() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// build-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait Target {
7+
const LENGTH: usize;
8+
}
9+
10+
11+
pub struct Container<T: Target>
12+
where
13+
[(); T::LENGTH]: Sized,
14+
{
15+
_target: T,
16+
}
17+
18+
impl<T: Target> Container<T>
19+
where
20+
[(); T::LENGTH]: Sized,
21+
{
22+
pub fn start(
23+
_target: T,
24+
) -> Container<T> {
25+
Container { _target }
26+
}
27+
}
28+
29+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// build-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(generic_const_exprs)]
5+
6+
pub trait Foo {
7+
const SIZE: usize;
8+
9+
fn to_bytes(&self) -> [u8; Self::SIZE];
10+
}
11+
12+
pub fn bar<G: Foo>(a: &G) -> u8
13+
where
14+
[(); G::SIZE]: Sized,
15+
{
16+
deeper_bar(a)
17+
}
18+
19+
fn deeper_bar<G: Foo>(a: &G) -> u8
20+
where
21+
[(); G::SIZE]: Sized,
22+
{
23+
a.to_bytes()[0]
24+
}
25+
26+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// build-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait Enumerable {
7+
const N: usize;
8+
}
9+
10+
#[derive(Clone)]
11+
pub struct SymmetricGroup<S>
12+
where
13+
S: Enumerable,
14+
[(); S::N]: Sized,
15+
{
16+
_phantom: std::marker::PhantomData<S>,
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)