Skip to content

ICE: index out of bounds with const generics #62456

Description

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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-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