Skip to content

Compiler can't infer const parameter from infallible array pattern #70529

Closed
@rodrimati1992

Description

@rodrimati1992

The compiler can't infer a const parameter based on the expected array type in an infallible pattern.

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4673d663e2f78cdc15395c1cc3701d67

#![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

A-const-genericsArea: const generics (parameters and arguments)A-type-systemArea: Type systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.F-const_generics`#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions