Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
gregkatz opened this issue Jun 18, 2019 · 2 comments · Fixed by #74392
Closed

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

gregkatz opened this issue Jun 18, 2019 · 2 comments · Fixed by #74392
Labels
A-codegen Area: Code generation A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gregkatz
Copy link

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
@jonas-schievink
Copy link
Contributor

You can enable backtraces on the playground inside a menu.

error: internal compiler error: src/librustc_codegen_llvm/context.rs:856: failed to get layout for `[i32; _]`: the type `[i32; _]` has an unknown layout

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:643:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.heygears.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.heygears.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:212
   6: rustc::util::common::panic_hook
   7: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:479
   8: std::panicking::begin_panic
   9: rustc_errors::Handler::bug
  10: rustc::util::bug::opt_span_bug_fmt::{{closure}}
  11: rustc::ty::context::tls::with_opt::{{closure}}
  12: rustc::ty::context::tls::with_context_opt
  13: rustc::ty::context::tls::with_opt
  14: rustc::util::bug::opt_span_bug_fmt
  15: rustc::util::bug::bug_fmt
  16: <rustc_codegen_llvm::context::CodegenCx as rustc_target::abi::LayoutOf>::layout_of::{{closure}}
  17: <rustc_codegen_llvm::context::CodegenCx as rustc_target::abi::LayoutOf>::layout_of
  18: <rustc_target::abi::call::FnType<&rustc::ty::TyS> as rustc::ty::layout::FnTypeExt<C>>::new
  19: rustc_codegen_llvm::declare::<impl rustc_codegen_ssa::traits::declare::DeclareMethods for rustc_codegen_llvm::context::CodegenCx>::declare_fn
  20: rustc_codegen_llvm::mono_item::<impl rustc_codegen_ssa::traits::declare::PreDefineMethods for rustc_codegen_llvm::context::CodegenCx>::predefine_fn
  21: <rustc::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::predefine
  22: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  23: rustc::dep_graph::graph::DepGraph::with_task
  24: rustc_codegen_llvm::base::compile_codegen_unit
  25: rustc_codegen_ssa::base::codegen_crate
  26: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  27: rustc::util::common::time
  28: rustc_interface::passes::start_codegen
  29: rustc::ty::context::tls::enter_global
  30: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  31: rustc_interface::passes::create_global_ctxt::{{closure}}
  32: rustc_interface::passes::BoxedGlobalCtxt::enter
  33: rustc_interface::queries::Query<T>::compute
  34: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::ongoing_codegen
  35: rustc_interface::interface::run_compiler_in_existing_thread_pool
  36: std::thread::local::LocalKey<T>::with
  37: scoped_tls::ScopedKey<T>::set
  38: syntax::with_globals

@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 18, 2019
@Centril Centril added the A-codegen Area: Code generation label Jun 18, 2019
@Centril Centril added requires-nightly This issue requires a nightly compiler in some way. F-const_generics `#![feature(const_generics)]` labels Aug 6, 2019
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 15, 2019
@varkor varkor changed the title ICE with const generics ICE with const generics: llvm failed to get layout for [i32; _] Jan 5, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 7, 2020
…dtwco

Fix ICE in const pretty printing and resolve FIXME

Consts now have a `fmt::Display` impl, so we can just use that to pretty-print.

This resolves an ICE in rust-lang#61936, though it hits more ICEs afterwards. I couldn't find a test case that was resolved by this that didn't hit errors later on.
@Alexendoo
Copy link
Member

Alexendoo commented Mar 25, 2020

since 342c5f3 this still ICEs, but needs an explicit type added on line 40 (let v = vec![...];).

#![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<usize> = vec![100; 0usize];
    
    for array in v.as_slice().array_windows::<{FOUR}>() {
       // println!("{:?}", array);
    }
}

Manishearth added a commit to Manishearth/rust that referenced this issue Jul 16, 2020
const generics triage

I went through all const generics issues and closed all issues which are already fixed.

Some issues already have a regression test but were not closed. Also doing this as part of this PR.

uff r? @eddyb @varkor

closes rust-lang#61936
closes rust-lang#62878
closes rust-lang#63695
closes rust-lang#67144
closes rust-lang#68596
closes rust-lang#69816
closes rust-lang#70217
closes rust-lang#70507
closes rust-lang#70586
closes rust-lang#71348
closes rust-lang#71805
closes rust-lang#73120
closes rust-lang#73508
closes rust-lang#73730
closes rust-lang#74255
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-codegen Area: Code generation A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants