rust icon indicating copy to clipboard operation
rust copied to clipboard

ICE: hir: `index out of bounds: the len is 5 but the index is 5`

Open matthiaskrgr opened this issue 1 year ago • 4 comments

auto-reduced (treereduce-rust):

static mut TEST2: &'static mut [i32] = &mut [0, 1, 2];

fn main() {}


original: (tests/ui/consts/promotion-mutable-ref.rs)

//@ run-pass
#![feature(const_mut_refs)]

static mut TEST: i32 = {
    // We must not promote this, as CTFE needs to be able to mutate it later.
    let x = &mut [1,2,3];
    x[0] += 1;
    x[0]
};

// This still works -- it's not done via promotion.
#[allow(unused)]
static mut TEST2: &'static mut [i32] = &mut [0,1,2];

fn main() {
    assert_eq!(unsafe { TEST }, 2);
}

Version information

rustc 1.78.0-nightly (7de1a1f6d 2024-03-12)
binary: rustc
commit-hash: 7de1a1f6db26cf7af43cca74819118428e6317ee
commit-date: 2024-03-12
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zcodegen-backend=cranelift

Program output

warning: static `TEST2` is never used
 --> /tmp/icemaker_global_tempdir.VX9jN0SNTvXS/rustc_testrunner_tmpdir_reporting.oNybMUzdaDH5/mvce.rs:1:12
  |
1 | static mut TEST2: &'static mut [i32] = &mut [0, 1, 2];
  |            ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

thread 'rustc' panicked at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/compiler/rustc_middle/src/hir/mod.rs:131:44:
index out of bounds: the len is 5 but the index is 5
stack backtrace:
   0:     0x7f4e5964f6f2 - std::backtrace_rs::backtrace::libunwind::trace::h88547ccd0750bcc5
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f4e5964f6f2 - std::backtrace_rs::backtrace::trace_unsynchronized::hc14f662a9b0401aa
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f4e5964f6f2 - std::sys_common::backtrace::_print_fmt::h07146244765e2a09
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f4e5964f6f2 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5c8264a9859d052b
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f4e596a07fc - core::fmt::rt::Argument::fmt::h9ca9e08086b797e7
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/core/src/fmt/rt.rs:142:9
   5:     0x7f4e596a07fc - core::fmt::write::h8b40f2214bb14880
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/core/src/fmt/mod.rs:1153:17
   6:     0x7f4e5964414f - std::io::Write::write_fmt::h02aeae87b3e9139f
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/io/mod.rs:1843:15
   7:     0x7f4e5964f4c4 - std::sys_common::backtrace::_print::h23cf181c7ee884c0
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f4e5964f4c4 - std::sys_common::backtrace::print::ha8c43be89c69bb35
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f4e596521cb - std::panicking::default_hook::{{closure}}::h5850daabba95add3
  10:     0x7f4e59651f23 - std::panicking::default_hook::h927208de7c8aa04a
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/panicking.rs:292:9
  11:     0x7f4e562a393f - std[16fa80a9100f94e9]::panicking::update_hook::<alloc[7bbb032d5977675a]::boxed::Box<rustc_driver_impl[93973095071799d4]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f4e59652930 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hef4f571603a76142
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/alloc/src/boxed.rs:2034:9
  13:     0x7f4e59652930 - std::panicking::rust_panic_with_hook::hf4470ff416e0f85c
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/panicking.rs:783:13
  14:     0x7f4e59652672 - std::panicking::begin_panic_handler::{{closure}}::hc33ab4a8553711f1
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/panicking.rs:657:13
  15:     0x7f4e5964fbc6 - std::sys_common::backtrace::__rust_end_short_backtrace::heea4e6144f841498
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f4e596523a4 - rust_begin_unwind
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/panicking.rs:645:5
  17:     0x7f4e5969cd15 - core::panicking::panic_fmt::h6b47fc784c42a44f
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/core/src/panicking.rs:72:14
  18:     0x7f4e5969cf62 - core::panicking::panic_bounds_check::h40920a00b2e8645f
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/core/src/panicking.rs:209:5
  19:     0x7f4e5773cba5 - rustc_query_impl[388e884d96e7c885]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[388e884d96e7c885]::query_impl::opt_local_def_id_to_hir_id::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1566db9aa553dc1e]::query::erase::Erased<[u8; 8usize]>>
  20:     0x7f4e5773c137 - rustc_query_system[be268ab4dd4fe2c4]::query::plumbing::try_execute_query::<rustc_query_impl[388e884d96e7c885]::DynamicConfig<rustc_query_system[be268ab4dd4fe2c4]::query::caches::VecCache<rustc_span[b32d8a6dd76386fe]::def_id::LocalDefId, rustc_middle[1566db9aa553dc1e]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[388e884d96e7c885]::plumbing::QueryCtxt, false>
  21:     0x7f4e5773bc89 - rustc_query_impl[388e884d96e7c885]::query_impl::opt_local_def_id_to_hir_id::get_query_non_incr::__rust_end_short_backtrace
  22:     0x7f4e5566df6b - rustc_hir_analysis[a25e4a65a289fc30]::collect::type_of::type_of
  23:     0x7f4e5765eaa2 - rustc_query_impl[388e884d96e7c885]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[388e884d96e7c885]::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1566db9aa553dc1e]::query::erase::Erased<[u8; 8usize]>>
  24:     0x7f4e5765d559 - rustc_query_system[be268ab4dd4fe2c4]::query::plumbing::try_execute_query::<rustc_query_impl[388e884d96e7c885]::DynamicConfig<rustc_query_system[be268ab4dd4fe2c4]::query::caches::DefIdCache<rustc_middle[1566db9aa553dc1e]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[388e884d96e7c885]::plumbing::QueryCtxt, false>
  25:     0x7f4e5765d063 - rustc_query_impl[388e884d96e7c885]::query_impl::type_of::get_query_non_incr::__rust_end_short_backtrace
  26:     0x7f4e5813c19c - <rustc_middle[1566db9aa553dc1e]::ty::instance::Instance>::ty
  27:     0x7f4e49c1a91c - rustc_codegen_cranelift[6d40d3b611a797fa]::constant::data_id_for_static
  28:     0x7f4e49c18942 - <rustc_codegen_cranelift[6d40d3b611a797fa]::constant::ConstantCx>::finalize
  29:     0x7f4e49c21be3 - rustc_codegen_cranelift[6d40d3b611a797fa]::driver::aot::module_codegen::{closure#0}
  30:     0x7f4e49c20297 - rustc_codegen_cranelift[6d40d3b611a797fa]::driver::aot::module_codegen
  31:     0x7f4e49c2c68c - rustc_codegen_cranelift[6d40d3b611a797fa]::driver::aot::run_aot
  32:     0x7f4e49c5ba71 - <rustc_codegen_cranelift[6d40d3b611a797fa]::CraneliftCodegenBackend as rustc_codegen_ssa[ea620ae6e0f9d31]::traits::backend::CodegenBackend>::codegen_crate
  33:     0x7f4e57f84045 - rustc_interface[de72e7e0dc20aa2a]::passes::start_codegen
  34:     0x7f4e57f82e33 - <rustc_interface[de72e7e0dc20aa2a]::queries::Queries>::codegen_and_build_linker
  35:     0x7f4e585f0131 - rustc_interface[de72e7e0dc20aa2a]::interface::run_compiler::<core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>, rustc_driver_impl[93973095071799d4]::run_compiler::{closure#0}>::{closure#0}
  36:     0x7f4e585e84d7 - std[16fa80a9100f94e9]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[de72e7e0dc20aa2a]::util::run_in_thread_with_globals<rustc_interface[de72e7e0dc20aa2a]::util::run_in_thread_pool_with_globals<rustc_interface[de72e7e0dc20aa2a]::interface::run_compiler<core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>, rustc_driver_impl[93973095071799d4]::run_compiler::{closure#0}>::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>
  37:     0x7f4e585e8304 - <<std[16fa80a9100f94e9]::thread::Builder>::spawn_unchecked_<rustc_interface[de72e7e0dc20aa2a]::util::run_in_thread_with_globals<rustc_interface[de72e7e0dc20aa2a]::util::run_in_thread_pool_with_globals<rustc_interface[de72e7e0dc20aa2a]::interface::run_compiler<core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>, rustc_driver_impl[93973095071799d4]::run_compiler::{closure#0}>::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[57c9baa80fbe8163]::result::Result<(), rustc_span[b32d8a6dd76386fe]::ErrorGuaranteed>>::{closure#1} as core[57c9baa80fbe8163]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  38:     0x7f4e5965c135 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hc7bf8226d462e248
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/alloc/src/boxed.rs:2020:9
  39:     0x7f4e5965c135 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h0c8e4067a29a93ad
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/alloc/src/boxed.rs:2020:9
  40:     0x7f4e5965c135 - std::sys::pal::unix::thread::Thread::new::thread_start::hcc7d14588e2fbcf6
                               at /rustc/7de1a1f6db26cf7af43cca74819118428e6317ee/library/std/src/sys/pal/unix/thread.rs:108:17
  41:     0x7f4e532a955a - <unknown>
  42:     0x7f4e53326a3c - <unknown>
  43:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.78.0-nightly (7de1a1f6d 2024-03-12) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z codegen-backend=cranelift -Z dump-mir-dir=dir

query stack during panic:
#0 [opt_local_def_id_to_hir_id] getting HIR ID of `TEST2::{constant#0}`
#1 [type_of] computing type of `TEST2::{constant#0}`
end of query stack
warning: 1 warning emitted


matthiaskrgr avatar Mar 12 '24 18:03 matthiaskrgr

regression in #121644 cc @oli-obk

also cc @bjorn3 for awareness

matthiaskrgr avatar Mar 12 '24 18:03 matthiaskrgr

Oh yea, I only really implemented and tested llvm

oli-obk avatar Mar 12 '24 19:03 oli-obk

gcc is now also very broken for statics with multiple references in the trailing expression

cc @GuillaumeGomez

I'll happily provide guidance for the backends. The most important point is to stop using types of statics, and obtain all information (alignment, size, mutability) from the allocation returned by evaluating the static's initializer

Or implement stuff myself if x test ui is easily able to run with the other backend

oli-obk avatar Mar 12 '24 19:03 oli-obk

I'll let @antoyo take a look for the statics issue.

For being able to easily test the gcc backend, it's still in-progress.

GuillaumeGomez avatar Mar 12 '24 20:03 GuillaumeGomez

I get an ICE with miri that looks similar (and started showing up around the same time) that I can reproduce with git clone -b v0.12.1 https://github.com/rustls/rcgen.git && cd rcgen && cargo +nightly miri test.

rustc-ice-2024-03-15T19_11_39-24486.txt

Edit: filed https://github.com/rust-lang/miri/issues/3389.

andyleiserson avatar Mar 15 '24 19:03 andyleiserson

I somehow missed this. In any case this is fixed by https://github.com/rust-lang/rustc_codegen_cranelift/commit/e775fdc9e92939111255c7e228a211cff6e03485. Will do a subtree sync soon.

bjorn3 avatar Mar 16 '24 17:03 bjorn3

I created https://github.com/rust-lang/rust/pull/122719 so we get sensible ICEs from now on (and fixing https://github.com/rust-lang/miri/issues/3389 in the process

oli-obk avatar Mar 19 '24 09:03 oli-obk