Closed
Description
The compiler can't infer a const parameter based on the expected array type in an infallible pattern.
I tried this code:
#![feature(const_generics)]
struct ArrayChunks<'a,T,const N:usize>{
rem: &'a [T],
}
impl<'a,T,const N:usize> Iterator for ArrayChunks<'a,T,N>{
type Item=&'a [T;N];
fn next(&mut self)->Option<&'a [T;N]>{
if self.rem.len() < N {
return None;
}
let (ret,rem)=self.rem.split_at(N);
self.rem=rem;
let ret=unsafe{ std::mem::transmute::<*const T,&[T;N]>(ret.as_ptr()) };
Some(ret)
}
}
fn as_chunks<T,const N:usize>(slice:&[T])-> ArrayChunks<'_,T,N> {
ArrayChunks{rem:slice}
}
fn main(){
for &[x,y] in as_chunks(&[0,1,2,3,4]) {
println!("x:{:?} y:{:?}",x,y)
}
}
I expected the code to compile and run without errors.
Instead, I got this compiler error:
error[E0730]: cannot pattern-match on an array without a fixed length
--> src/main.rs:27:10
|
27 | for &[x,y] in as_chunks(&[0,1,2,3,4]) {
| ^^^^^
Meta
The compiler was 1.44.0-nightly (2020-03-28 7762131)
This issue has been assigned to @lcnr via this comment.
Metadata
Metadata
Assignees
Labels
Area: const generics (parameters and arguments)Area: Type systemCategory: A feature request, i.e: not implemented / a PR.`#![feature(const_generics)]`Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.