Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ICE: index out of bounds with const generics #62456

Closed
alex opened this issue Jul 7, 2019 · 3 comments · Fixed by #69859 or #69914
Closed

ICE: index out of bounds with const generics #62456

alex opened this issue Jul 7, 2019 · 3 comments · Fixed by #69859 or #69914
Labels
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

@alex
Copy link
Member

alex commented Jul 7, 2019

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

#![feature(const_generics)]

use std::fmt;

struct Builder<const N: usize> {
    items: [&'static str; N],
}

fn new_builder() -> Builder<{0}> {
    return Builder{items: []};
}

impl<const N: usize> Builder<{ N }> {
    fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
        let mut new_items = [""; N + 1];
        new_items[..N].copy_from_slice(self.items);
        new_items[N] = value;
        return Builder { items: new_items };
    }

    fn build(self) -> Final<{ N }> {
        return Final { items: self.items };
    }
}

struct Final<const N: usize> {
    items: [&'static str; N],
}

impl<const N: usize> fmt::Debug for Final<{ N }> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Final")
            .field("items", &&self.items[..])
            .finish()
    }
}

fn main() {
    let f = new_builder()
        .append("abc")
        .append("def")
        .append("ghi")
        .build();
    println!("f={:?}", f);
}
   Compiling playground v0.0.1 (/playground)
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> src/main.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^

thread 'rustc' panicked at 'index out of bounds: the len is 1 but the index is 1', /rustc/481068a707679257e2a738b40987246e0420e787/src/libcore/slice/mod.rs:2681:10
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.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:200
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:214
   6: rustc::util::common::panic_hook
   7: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:481
   8: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:384
   9: rust_begin_unwind
             at src/libstd/panicking.rs:311
  10: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  11: core::panicking::panic_bounds_check
             at src/libcore/panicking.rs:61
  12: rustc_mir::borrow_check::nll::type_check::type_check
  13: rustc_mir::borrow_check::nll::compute_regions
  14: rustc_mir::borrow_check::do_mir_borrowck
  15: rustc::ty::context::GlobalCtxt::enter_local
  16: rustc_mir::borrow_check::mir_borrowck
  17: rustc::ty::query::__query_compute::mir_borrowck
  18: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::mir_borrowck>::compute
  19: rustc::dep_graph::graph::DepGraph::with_task_impl
  20: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  21: rustc::ty::<impl rustc::ty::context::TyCtxt>::par_body_owners
  22: rustc::util::common::time
  23: rustc_interface::passes::analysis
  24: rustc::ty::query::__query_compute::analysis
  25: rustc::dep_graph::graph::DepGraph::with_task_impl
  26: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  27: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  28: rustc_interface::passes::create_global_ctxt::{{closure}}
  29: rustc_interface::interface::run_compiler_in_existing_thread_pool
  30: std::thread::local::LocalKey<T>::with
  31: scoped_tls::ScopedKey<T>::set
  32: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
query stack during panic:
#0 [mir_borrowck] processing `Builder::<N>::append`
#1 [analysis] running analysis passes on this crate
end of query stack
error: internal compiler error: cat_expr Errd
  --> src/main.rs:14:64
   |
14 |       fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
   |  ________________________________________________________________^
15 | |         let mut new_items = [""; N + 1];
16 | |         new_items[..N].copy_from_slice(self.items);
17 | |         new_items[N] = value;
18 | |         return Builder { items: new_items };
19 | |     }
   | |_____^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:15:29
   |
15 |         let mut new_items = [""; N + 1];
   |                             ^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:16:9
   |
16 |         new_items[..N].copy_from_slice(self.items);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:16:9
   |
16 |         new_items[..N].copy_from_slice(self.items);
   |         ^^^^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:16:9
   |
16 |         new_items[..N].copy_from_slice(self.items);
   |         ^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:17:9
   |
17 |         new_items[N] = value;
   |         ^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:17:9
   |
17 |         new_items[N] = value;
   |         ^^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:17:9
   |
17 |         new_items[N] = value;
   |         ^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:18:33
   |
18 |         return Builder { items: new_items };
   |                                 ^^^^^^^^^

error: internal compiler error: no type-dependent def for method call
  --> src/main.rs:16:9
   |
16 |         new_items[..N].copy_from_slice(self.items);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: QualifyAndPromoteConstants: MIR had errors
  --> src/main.rs:14:5
   |
14 | /     fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
15 | |         let mut new_items = [""; N + 1];
16 | |         new_items[..N].copy_from_slice(self.items);
17 | |         new_items[N] = value;
18 | |         return Builder { items: new_items };
19 | |     }
   | |_____^

error: internal compiler error: broken MIR in DefId(0:22 ~ playground[a9b4]::{{impl}}[0]::append[0]) ("return type"): bad type [type error]
  --> src/main.rs:14:5
   |
14 | /     fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
15 | |         let mut new_items = [""; N + 1];
16 | |         new_items[..N].copy_from_slice(self.items);
17 | |         new_items[N] = value;
18 | |         return Builder { items: new_items };
19 | |     }
   | |_____^

error: internal compiler error: broken MIR in DefId(0:22 ~ playground[a9b4]::{{impl}}[0]::append[0]) (LocalDecl { mutability: Mut, is_user_variable: None, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, name: None, source_info: SourceInfo { span: src/main.rs:14:5: 19:6, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
  --> src/main.rs:14:5
   |
14 | /     fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
15 | |         let mut new_items = [""; N + 1];
16 | |         new_items[..N].copy_from_slice(self.items);
17 | |         new_items[N] = value;
18 | |         return Builder { items: new_items };
19 | |     }
   | |_____^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:366:17
stack backtrace:
   0:     0x7f2a9f73e24b - backtrace::backtrace::libunwind::trace::hda4e121c515bf458
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1:     0x7f2a9f73e24b - backtrace::backtrace::trace_unsynchronized::h0a71c953a4742195
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2:     0x7f2a9f73e24b - std::sys_common::backtrace::_print::hf4a62558314f5307
                               at src/libstd/sys_common/backtrace.rs:47
   3:     0x7f2a9f73e24b - std::sys_common::backtrace::print::hcb7a471013853997
                               at src/libstd/sys_common/backtrace.rs:36
   4:     0x7f2a9f73e24b - std::panicking::default_hook::{{closure}}::ha0ea642565fdae2f
                               at src/libstd/panicking.rs:200
   5:     0x7f2a9f73df27 - std::panicking::default_hook::h0f057f6c7c516eb6
                               at src/libstd/panicking.rs:214
   6:     0x7f2a9cde32c1 - rustc::util::common::panic_hook::hc3d6a9f610b367e7
   7:     0x7f2a9f73eaa9 - std::panicking::rust_panic_with_hook::hc77fb9874fa29555
                               at src/libstd/panicking.rs:481
   8:     0x7f2a9ba562a5 - std::panicking::begin_panic::h6e376c592b6253f0
   9:     0x7f2a9ba79dc5 - <rustc_errors::Handler as core::ops::drop::Drop>::drop::h34bba684e8175cc7
  10:     0x7f2a9fa54b82 - core::ptr::real_drop_in_place::hb2f5bc0c04a6a718
  11:     0x7f2a9fa59e94 - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::hbcc6465daa24608a
  12:     0x7f2a9fa270ac - core::ptr::real_drop_in_place::h5e74c5df24af64f3
  13:     0x7f2a9fa24058 - rustc_interface::interface::run_compiler_in_existing_thread_pool::hfa8966a3f2d428a3
  14:     0x7f2a9fa380c2 - std::thread::local::LocalKey<T>::with::ha697a453d1de3d8a
  15:     0x7f2a9fa4b071 - scoped_tls::ScopedKey<T>::set::h50b61de3f9f457ff
  16:     0x7f2a9fa62954 - syntax::with_globals::h2642fa717df1d186
  17:     0x7f2a9fa8ca0d - std::sys_common::backtrace::__rust_begin_short_backtrace::ha67256aa038100bc
  18:     0x7f2a9f74f50a - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:82
  19:     0x7f2a9fa0a9f9 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h187e5c932baee631
  20:     0x7f2a9f721ecf - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h4b1f48c87819f1d7
                               at /rustc/481068a707679257e2a738b40987246e0420e787/src/liballoc/boxed.rs:766
  21:     0x7f2a9f74e1f0 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h982c5ef9aacde87b
                               at /rustc/481068a707679257e2a738b40987246e0420e787/src/liballoc/boxed.rs:766
  22:     0x7f2a9f74e1f0 - std::sys_common::thread::start_thread::h961b6807ee2d2f95
                               at src/libstd/sys_common/thread.rs:13
  23:     0x7f2a9f74e1f0 - std::sys::unix::thread::Thread::new::thread_start::h166a70c782ebeb55
                               at src/libstd/sys/unix/thread.rs:79
  24:     0x7f2a9f4be6db - start_thread
  25:     0x7f2a9eddb88f - __clone
  26:                0x0 - <unknown>
query stack during panic:
end of query stack
thread panicked while panicking. aborting.
error: Could not compile `playground`.
@Centril Centril added 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. A-const-generics Area: const generics (parameters and arguments) labels Jul 8, 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
@jonas-schievink jonas-schievink added the C-bug Category: This is a bug. label Aug 6, 2019
@BenLewis-Seequent
Copy link

As of rustc 1.39.0-nightly (96d07e0 2019-09-15) it gives an ICE with the following message:
error: internal compiler error: src/librustc/ty/subst.rs:597: const parameter N/#0 (Const { ty: usize, val: Param(N/#0) }/0) out of range when substituting substs=[]
Which is the same ICE #61747 currently gives.

@jonas-schievink
Copy link
Contributor

The ICE is now again the out of bound access on nightly.

Another instance: #66987

@varkor
Copy link
Member

varkor commented Jan 5, 2020

I believe this is essentially due to the following not working:

#![feature(const_generics)]

fn foo<const N: usize>() {
    let _ = [""; N + 1];
}

@contrun contrun mentioned this issue Mar 9, 2020
bors added a commit that referenced this issue Mar 10, 2020
Rollup of 6 pull requests

Successful merges:

 - #69122 (Backtrace Debug tweaks)
 - #69591 (Use TypeRelating for instantiating query responses)
 - #69760 (Improve expression & attribute parsing)
 - #69837 (Use smaller discriminants for generators)
 - #69838 (Expansion-driven outline module parsing)
 - #69859 (fix #62456)

Failed merges:

r? @ghost
bors added a commit that referenced this issue Mar 11, 2020
Rollup of 10 pull requests

Successful merges:

 - #66059 (mem::zeroed/uninit: panic on types that do not permit zero-initialization)
 - #69373 (Stabilize const for integer {to,from}_{be,le,ne}_bytes methods)
 - #69591 (Use TypeRelating for instantiating query responses)
 - #69625 (Implement nth, last, and count for iter::Copied)
 - #69645 (const forget tests)
 - #69766 (Make Point `Copy` in arithmetic documentation)
 - #69825 (make `mem::discriminant` const)
 - #69859 (fix #62456)
 - #69891 (Exhaustiveness checking, `Matrix::push`: recursively expand or-patterns)
 - #69896 (parse: Tweak the function parameter edition check)

Failed merges:

r? @ghost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
6 participants