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: the len is 0 but the index is 0 #67945

Closed
DutchGhost opened this issue Jan 6, 2020 · 15 comments · Fixed by #98050
Closed

ICE: index out of bounds: the len is 0 but the index is 0 #67945

DutchGhost opened this issue Jan 6, 2020 · 15 comments · Fixed by #98050
Labels
A-const-generics Area: const generics (parameters and arguments) A-lazy-normalization Area: lazy normalization (tracking issue: #60471) C-bug Category: This is a bug. const-generics-fixed-by-const_generics A bug that has been fixed once `const_generics` is enabled. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. 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) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DutchGhost
Copy link
Contributor

The following ICE's on nightly and beta:

use std::marker::PhantomData;

use std::mem::{self, MaybeUninit};

struct Bug<S> {
    A: [(); {
        let x: S = MaybeUninit::uninit();
        let b = &*(&x as *const _ as *const S);
        0
    }],
}
Backtrace:
thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', /rustc/da3629b05f8f1b425a738bfe9fe9aedd47c5417a/src/libcore/slice/mod.rs:2791:10
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1057
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:195
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:215
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:476
  12: rust_begin_unwind
             at src/libstd/panicking.rs:376
  13: core::panicking::panic_fmt
             at src/libcore/panicking.rs:84
  14: core::panicking::panic_bounds_check
             at src/libcore/panicking.rs:62
  15: rustc::ty::Generics::type_param
  16: rustc::ty::error::<impl rustc::ty::context::TyCtxt>::note_and_explain_type_err
  17: rustc::infer::error_reporting::<impl rustc::infer::InferCtxt>::note_type_err
  18: rustc::infer::error_reporting::<impl rustc::infer::InferCtxt>::report_and_explain_type_error
  19: rustc::infer::InferCtxt::report_mismatched_types
  20: rustc_typeck::check::demand::<impl rustc_typeck::check::FnCtxt>::demand_coerce_diag
  21: rustc_typeck::check::demand::<impl rustc_typeck::check::FnCtxt>::demand_coerce
  22: rustc_typeck::check::FnCtxt::check_decl_local
  23: rustc_typeck::check::FnCtxt::check_stmt
  24: rustc_typeck::check::FnCtxt::check_block_with_expected
  25: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  26: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation_and_needs
  27: rustc::ty::context::tls::with_context::{{closure}}
  28: rustc_typeck::check::typeck_tables_of
  29: rustc::ty::query::__query_compute::typeck_tables_of
  30: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::typeck_tables_of>::compute
  31: rustc::dep_graph::graph::DepGraph::with_task_impl
  32: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  33: rustc_mir::const_eval::eval_queries::const_eval_raw_provider
  34: rustc::ty::query::__query_compute::const_eval_raw
  35: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_raw>::compute
  36: rustc::dep_graph::graph::DepGraph::with_task_impl
  37: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  38: rustc_mir::const_eval::eval_queries::const_eval_validated_provider
  39: rustc::ty::query::__query_compute::const_eval_validated
  40: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_validated>::compute
  41: rustc::dep_graph::graph::DepGraph::with_task_impl
  42: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  43: rustc_mir::const_eval::eval_queries::const_eval_validated_provider
  44: rustc::ty::query::__query_compute::const_eval_validated
  45: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_validated>::compute
  46: rustc::dep_graph::graph::DepGraph::with_task_impl
  47: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  48: rustc::mir::interpret::queries::<impl rustc::ty::context::TyCtxt>::const_eval_instance
  49: rustc::ty::sty::Const::eval::{{closure}}
  50: rustc::ty::sty::Const::eval
  51: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable for &rustc::ty::TyS>::super_fold_with
  52: <rustc::traits::project::AssocTypeNormalizer as rustc::ty::fold::TypeFolder>::fold_ty
  53: rustc::traits::project::normalize
  54: rustc_typeck::check::FnCtxt::normalize_associated_types_in
  55: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  56: rustc::ty::context::GlobalCtxt::enter_local
  57: rustc_typeck::check::wfcheck::check_item_well_formed
  58: rustc::ty::query::__query_compute::check_item_well_formed
  59: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::check_item_well_formed>::compute
  60: rustc::dep_graph::graph::DepGraph::with_task_impl
  61: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  62: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::ensure_query
  63: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:79
  64: rustc_data_structures::sync::par_for_each_in
  65: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:79
  66: rustc::hir::Crate::par_visit_all_item_likes
  67: rustc::util::common::time
  68: rustc_typeck::check_crate
  69: rustc_interface::passes::analysis
  70: rustc::ty::query::__query_compute::analysis
  71: rustc::dep_graph::graph::DepGraph::with_task_impl
  72: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  73: rustc::ty::context::tls::enter_global
  74: rustc_interface::interface::run_compiler_in_existing_thread_pool
  75: scoped_tls::ScopedKey<T>::set
  76: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.42.0-nightly (da3629b05 2019-12-29) running on x86_64-unknown-linux-gnu

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

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [typeck_tables_of] processing `Bug::A::{{constant}}#0`
#1 [const_eval_raw] const-evaluating `Bug::A::{{constant}}#0`
#2 [const_eval_validated] const-evaluating + checking `Bug::A::{{constant}}#0`
#3 [const_eval_validated] const-evaluating + checking `Bug::A::{{constant}}#0`
#4 [check_item_well_formed] processing `Bug`
#5 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 6, 2020
@Centril Centril added regression-from-stable-to-beta Performance or correctness regression from stable to beta. P-high High priority labels Jan 6, 2020
@Centril

This comment has been minimized.

@Centril

This comment has been minimized.

@davidtwco
Copy link
Member

I looked into this briefly and found that on this line:

let param = &self.params[index as usize];

...that self has the value:

Generics { parent: None, parent_count: 0, params: [], param_def_id_to_index: {}, has_self: false, has_late_bound_regions: None }

Digging a bit deeper, the generics appear to be empty because of the following lines:

// FIXME(#43408) enable this always when we get lazy normalization.
Node::AnonConst(_) => {
// HACK(eddyb) this provides the correct generics when
// `feature(const_generics)` is enabled, so that const expressions
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
if tcx.features().const_generics {
let parent_id = tcx.hir().get_parent_item(hir_id);
Some(tcx.hir().local_def_id(parent_id))
} else {
None
}
}

I can confirm that adding #![feature(const_generics)] makes this code compile, so I suspect that this is blocked on #43408.

@Centril Centril added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` A-lazy-normalization Area: lazy normalization (tracking issue: #60471) labels Jan 6, 2020
@estebank
Copy link
Contributor

estebank commented Jan 6, 2020

What would be the consequences of always returning the DefId?

@varkor
Copy link
Member

varkor commented Jan 6, 2020

What would be the consequences of always returning the DefId?

It causes query cycle errors at the moment.

@pnkfelix
Copy link
Member

pnkfelix commented Jan 9, 2020

Discussed at T-compiler meeting. Due to all the known examples being compile-time errors, the ICE here does not seem so strong a hazard as to warrant weekly visits during triage.

(Certainly failing more gracefully would be great. But its not something we're going to e.g. block a release on.)

Downgrading priority to P-medium.

@pnkfelix pnkfelix added P-medium Medium priority and removed P-high High priority labels Jan 9, 2020
@nikomatsakis
Copy link
Contributor

Some relevant examples. This gives an (incorrect) error on stable that S: Sized does not hold:

use std::marker::PhantomData;

use std::mem::{self, MaybeUninit};

struct Bug<S> {
    A: [(); {
        let x: Option<S> = None;
        0
    }],
}

This ICEs on stable:

use std::marker::PhantomData;

use std::mem::{self, MaybeUninit};

struct Bug<S> {
    A: [(); {
        let x: Option<Box<S>> = None;
        0
    }],
}

@BenLewis-Seequent
Copy link

Is there a reason why we can't just disallow type parameters from appearing within constants during resolve, like we do for type parameters from outer functions? Obviously const generics/lazy normization willl loosen this rule.

@nikomatsakis
Copy link
Contributor

@Skinny121 good point, I imagine that might work.

@BenLewis-Seequent

This comment was marked as resolved.

@pietroalbini pietroalbini added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Jan 27, 2020
@JohnTitor
Copy link
Member

Triage: The two examples that Centril posted (#67945 (comment) and #67945 (comment)) are no longer ICEs in the latest nightly. Possibly related to #70825?

@nikomatsakis
Copy link
Contributor

Maybe we should just add tests then?

@JohnTitor
Copy link
Member

Maybe we should just add tests then?

Sounds good! I'll do later.

JohnTitor added a commit to JohnTitor/rust that referenced this issue May 6, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
Add some regression tests

Closes rust-lang#29988
Closes rust-lang#34979
Pick up two snippets that have been fixed from rust-lang#67945 (shouldn't be closed yet!)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
Add some regression tests

Closes rust-lang#29988
Closes rust-lang#34979
Pick up two snippets that have been fixed from rust-lang#67945 (shouldn't be closed yet!)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
Add some regression tests

Closes rust-lang#29988
Closes rust-lang#34979
Pick up two snippets that have been fixed from rust-lang#67945 (shouldn't be closed yet!)
@varkor varkor added the const-generics-fixed-by-const_generics A bug that has been fixed once `const_generics` is enabled. label Sep 13, 2020
@varkor

This comment was marked as resolved.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 5, 2020
…lcnr,estebank

Fix missing diagnostic span for `impl Trait` with const generics, and add various tests for `min_const_generics` and `const_generics`

Closes rust-lang#61410.

Adds `min_const_generics` tests for:
- rust-lang#73727
- rust-lang#72293
- rust-lang#67375
- rust-lang#75153
- rust-lang#71922
- rust-lang#69913
- rust-lang#67945
- rust-lang#69239

Adds `const_generics` tests for:
- rust-lang#67375
- rust-lang#75153
- rust-lang#71922
- rust-lang#69913
- rust-lang#67945
- rust-lang#69239

(I only added separate `min_const_generics` and `const_generics` tests if they were handled differently by the two features.)

We need to figure out how to deduplicate when `const_generics` is stabilised, but we can discuss that later. For now, we should be checking neither feature breaks, so require regression tests for both. I've given them identical names when I've added both, which should make it easier to spot them later.

r? @lcnr
@JohnTitor
Copy link
Member

Triage: All the snippets including the original are no longer ICE with the latest nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=fc90d68f3fd2b8958e7365870eee6202

@varkor varkor added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 30, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 13, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 13, 2022
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#97709 (Normalize consts' tys when relating with `adt_const_params`)
 - rust-lang#97875 (Remove the `infer_static_outlives_requirements` feature)
 - rust-lang#97960 (interpret: unify offset_from check with offset check)
 - rust-lang#97999 (Make `type_changing_struct_update` no longer an incomplete feature)
 - rust-lang#98043 (Remove unnecessary `to_string` and `String::new`)
 - rust-lang#98044 ([issues:97981] del unrelated comment)
 - rust-lang#98049 (Document an edge case of `str::split_once`)
 - rust-lang#98050 (Add some more regression tests for rust-lang#67945)
 - rust-lang#98054 (Fix error message for `download-ci-llvm`)
 - rust-lang#98057 (Update miri)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 3f17cd7 Jun 13, 2022
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) A-lazy-normalization Area: lazy normalization (tracking issue: #60471) C-bug Category: This is a bug. const-generics-fixed-by-const_generics A bug that has been fixed once `const_generics` is enabled. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. 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) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.