Skip to content

ICE with const generics: llvm failed to get layout for [i32; _] #61936

Closed
@gregkatz

Description

@gregkatz

Hi, I'm trying to learn how to use the new const generics stuff and I ICEd the compiler. Here's the code:

#![feature(const_generics)]

trait SliceExt<T: Clone> {
    fn array_windows<'a, const N: usize>(&'a self) -> ArrayWindows<'a, T, {N}>;
}

impl <T: Clone> SliceExt<T> for [T] {
   fn array_windows<'a, const N: usize>(&'a self) -> ArrayWindows<'a, T, {N}> {
       ArrayWindows{ idx: 0, slice: &self }
   } 
}

struct ArrayWindows<'a, T, const N: usize> {
    slice: &'a [T],
    idx: usize,
}

impl <'a, T: Clone, const N: usize> Iterator for ArrayWindows<'a, T, {N}> {
    type Item = [T; N];
    fn next(&mut self) -> Option<Self::Item> {
        let mut res = unsafe{ std::mem::zeroed() };
        let mut ptr = &mut res as *mut [T; N] as *mut T;
        
        for i in 0..N {
            match self.slice[i..].get(i) {
                None => return None,
                Some(elem) => unsafe { std::ptr::write_volatile(ptr, elem.clone())}, 
            };
            ptr = ptr.wrapping_add(1);
            self.idx += 1;
        }
        
        Some(res)
    }
}

const FOUR: usize = 4;

fn main() {
    let v = vec![100; 0usize];
    
    for array in v.as_slice().array_windows::<{FOUR}>() {
       // println!("{:?}", array);
    }
}

I can't give you the verbose compiler info because this was done on play.rust-lang.org but here's the version:

note: rustc 1.37.0-nightly (b25ee6449 2019-06-17) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type bin

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions