Skip to content

Commit cc7a6d1

Browse files
committed
add tests for type dependent paths
1 parent 2c6e561 commit cc7a6d1

File tree

10 files changed

+241
-0
lines changed

10 files changed

+241
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
#![feature(const_fn)]
5+
6+
struct Foo;
7+
8+
impl Foo {
9+
fn foo<const N: usize>(&self) -> usize {
10+
let f = self;
11+
f.bar::<{
12+
let f = Foo;
13+
f.bar::<7>()
14+
}>() + N
15+
}
16+
17+
const fn bar<const M: usize>(&self) -> usize {
18+
M
19+
}
20+
}
21+
22+
fn main() {
23+
let f = Foo;
24+
25+
assert_eq!(f.foo::<13>(), 20)
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
trait SliceExt<T: Clone> {
6+
fn array_windows<'a, const N: usize>(&'a self) -> ArrayWindows<'a, T, N>;
7+
}
8+
9+
impl <T: Clone> SliceExt<T> for [T] {
10+
fn array_windows<'a, const N: usize>(&'a self) -> ArrayWindows<'a, T, N> {
11+
ArrayWindows{ idx: 0, slice: &self }
12+
}
13+
}
14+
15+
struct ArrayWindows<'a, T, const N: usize> {
16+
slice: &'a [T],
17+
idx: usize,
18+
}
19+
20+
impl <'a, T: Clone, const N: usize> Iterator for ArrayWindows<'a, T, N> {
21+
type Item = [T; N];
22+
fn next(&mut self) -> Option<Self::Item> {
23+
let mut res = unsafe{ std::mem::zeroed() };
24+
let mut ptr = &mut res as *mut [T; N] as *mut T;
25+
26+
for i in 0..N {
27+
match self.slice[i..].get(i) {
28+
None => return None,
29+
Some(elem) => unsafe { std::ptr::write_volatile(ptr, elem.clone())},
30+
};
31+
ptr = ptr.wrapping_add(1);
32+
self.idx += 1;
33+
}
34+
35+
Some(res)
36+
}
37+
}
38+
39+
const FOUR: usize = 4;
40+
41+
fn main() {
42+
let v: Vec<usize> = vec![100; 0usize];
43+
44+
for array in v.as_slice().array_windows::<FOUR>() {
45+
assert_eq!(array, [0, 0, 0, 0])
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
trait T {
6+
fn test<const A: i32>(&self) -> i32 { A }
7+
}
8+
9+
struct S();
10+
11+
impl T for S {}
12+
13+
fn main() {
14+
let foo = S();
15+
assert_eq!(foo.test::<8i32>(), 8);
16+
assert_eq!(foo.test::<16i32>(), 16);
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
trait IterExt: Sized + Iterator {
6+
fn default_for_size<const N: usize>(self) -> [Self::Item; N]
7+
where
8+
[Self::Item; N]: Default,
9+
{
10+
Default::default()
11+
}
12+
}
13+
14+
impl<T: Iterator> IterExt for T {}
15+
16+
fn main(){
17+
const N: usize = 10;
18+
let arr = (0u32..10).default_for_size::<N>();
19+
assert_eq!(arr, [0; 10]);
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
trait ConstChunksExactTrait<T> {
6+
fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}>;
7+
}
8+
9+
impl <T> ConstChunksExactTrait<T> for [T] {
10+
fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}> {
11+
assert!(N != 0);
12+
let rem = self.len() % N;
13+
let len = self.len() - rem;
14+
let (fst, _) = self.split_at(len);
15+
ConstChunksExact { v: fst, }
16+
}
17+
}
18+
19+
struct ConstChunksExact<'a, T: 'a, const N: usize> {
20+
v: &'a [T],
21+
}
22+
23+
impl <'a, T: std::fmt::Debug, const N: usize> Iterator for ConstChunksExact<'a, T, {N}> {
24+
type Item = &'a [T; N];
25+
26+
fn next(&mut self) -> Option<Self::Item> {
27+
if self.v.len() < N {
28+
None
29+
} else {
30+
let (fst, snd) = self.v.split_at(N);
31+
32+
self.v = snd;
33+
let ptr = fst.as_ptr() as *const _;
34+
Some(unsafe { &*ptr})
35+
}
36+
}
37+
}
38+
39+
fn main() {
40+
let slice = &[1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10];
41+
42+
let mut iter = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].iter();
43+
44+
for a in slice.const_chunks_exact::<3>() {
45+
assert_eq!(a, iter.next().unwrap());
46+
}
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
#![feature(const_compare_raw_pointers)]
5+
6+
struct Test;
7+
8+
fn pass() -> u8 {
9+
42
10+
}
11+
12+
impl Test {
13+
pub fn call_me(&self) -> u8 {
14+
self.test::<pass>()
15+
}
16+
17+
fn test<const FN: fn() -> u8>(&self) -> u8 {
18+
FN()
19+
}
20+
}
21+
22+
fn main() {
23+
let x = Test;
24+
assert_eq!(x.call_me(), 42);
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
struct A;
6+
impl A {
7+
fn foo<const N: usize>() -> usize { N + 1 }
8+
}
9+
10+
fn main() {
11+
assert_eq!(A::foo::<7>(), 8);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
#![allow(incomplete_features)]
4+
5+
struct R;
6+
7+
impl R {
8+
fn method<const N: u8>(&self) -> u8 { N }
9+
}
10+
fn main() {
11+
assert_eq!(R.method::<1u8>(), 1);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete
3+
4+
struct R;
5+
6+
impl R {
7+
fn method<const N: u8>(&self) -> u8 { N }
8+
}
9+
fn main() {
10+
assert_eq!(R.method::<1u16>(), 1);
11+
//~^ ERROR mismatched types
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/type-mismatch.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/type-mismatch.rs:10:27
12+
|
13+
LL | assert_eq!(R.method::<1u16>(), 1);
14+
| ^^^^ expected `u8`, found `u16`
15+
|
16+
help: change the type of the numeric literal from `u16` to `u8`
17+
|
18+
LL | assert_eq!(R.method::<1u8>(), 1);
19+
| ^^^
20+
21+
error: aborting due to previous error; 1 warning emitted
22+
23+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)