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: assertion failed: elem.index() < self.domain_size #103748

Closed
jruderman opened this issue Oct 30, 2022 · 14 comments · Fixed by #104202
Closed

ICE: assertion failed: elem.index() < self.domain_size #103748

jruderman opened this issue Oct 30, 2022 · 14 comments · Fixed by #104202
Labels
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.

Comments

@jruderman
Copy link
Contributor

jruderman commented Oct 30, 2022

Code

struct Apple((Apple, Option(Banana ? Citron)));

fn main(){}

Error output

thread 'rustc' panicked at 'assertion failed: elem.index() < self.domain_size', /rustc/0da281b6068a7d889ae89a9bd8991284cc9b7535/compiler/rustc_index/src/bit_set.rs:161:9
Full output including backtrace

error: invalid `?` in type
 --> u.rs:1:36
  |
1 | struct Apple((Apple, Option(Banana ? Citron)));
  |                                    ^ `?` is only allowed on expressions, not types
  |
help: if you meant to express that the type might not contain a value, use the `Option` wrapper type
  |
1 | struct Apple((Apple, Option(Option<Banana > Citron)));
  |                             +++++++       ~

error: expected one of `)` or `,`, found `Citron`
 --> u.rs:1:38
  |
1 | struct Apple((Apple, Option(Banana ? Citron)));
  |                                     -^^^^^^ expected one of `)` or `,`
  |                                     |
  |                                     help: missing `,`

error[E0412]: cannot find type `Citron` in this scope
 --> u.rs:1:38
  |
1 | struct Apple((Apple, Option(Banana ? Citron)));
  |                                      ^^^^^^ not found in this scope

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> u.rs:1:22
  |
1 | struct Apple((Apple, Option(Banana ? Citron)));
  |                      ^^^^^^^^^^^^^^^^^^^^^^^ only `Fn` traits may use parentheses
  |
help: use angle brackets instead
  |
1 | struct Apple((Apple, Option<Banana ? Citron>));
  |                            ~               ~

thread 'rustc' panicked at 'assertion failed: elem.index() < self.domain_size', /rustc/0da281b6068a7d889ae89a9bd8991284cc9b7535/compiler/rustc_index/src/bit_set.rs:161:9
stack backtrace:
   0:        0x106f8a8a2 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h98e7719cdb057d8f
   1:        0x106fe274a - core::fmt::write::he324e6f3a7ef7ed0
   2:        0x106f7c97c - std::io::Write::write_fmt::h295c914c323e508a
   3:        0x106f8a66a - std::sys_common::backtrace::print::hb2f2bd76d0a1d9b2
   4:        0x106f8da76 - std::panicking::default_hook::{{closure}}::h7f2a23a911934152
   5:        0x106f8d7c7 - std::panicking::default_hook::h576cf5867894044c
   6:        0x112f8262d - rustc_driver[ba5c792b9438c2b]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x106f8e271 - std::panicking::rust_panic_with_hook::hf0781b554a936fbf
   8:        0x106f8dfc2 - std::panicking::begin_panic_handler::{{closure}}::h8d2525e276d552d8
   9:        0x106f8ad38 - std::sys_common::backtrace::__rust_end_short_backtrace::hf3badbbd5492e2d6
  10:        0x106f8dccd - _rust_begin_unwind
  11:        0x10700f1b3 - core::panicking::panic_fmt::h15d4409e62dc090a
  12:        0x10700f287 - core::panicking::panic::h503de31a4470d5f7
  13:        0x117747a5b - rustc_middle[298407787cc28eff]::values::find_item_ty_spans
  14:        0x1177477e5 - rustc_middle[298407787cc28eff]::values::find_item_ty_spans
  15:        0x11774601c - rustc_middle[298407787cc28eff]::values::recursive_type_error
  16:        0x1177eb5a9 - <rustc_middle[298407787cc28eff]::ty::adt::Representability as rustc_query_system[fd3d689380a50de4]::values::Value<rustc_middle[298407787cc28eff]::ty::context::TyCtxt>>::from_cycle_error
  17:        0x117c5c66d - rustc_query_system[fd3d689380a50de4]::query::plumbing::mk_cycle::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_middle[298407787cc28eff]::ty::adt::Representability, rustc_middle[298407787cc28eff]::ty::adt::Representability>
  18:        0x116767a32 - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_span[73e074df7cba0abf]::def_id::LocalDefId, rustc_middle[298407787cc28eff]::ty::adt::Representability>>
  19:        0x11685f309 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::representability, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  20:        0x115748f97 - rustc_ty_utils[c85f993db90c93e8]::representability::representability_adt_ty
  21:        0x11675ab4c - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_middle[298407787cc28eff]::ty::Ty, rustc_middle[298407787cc28eff]::ty::adt::Representability>>
  22:        0x116867cd8 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::representability_adt_ty, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  23:        0x115748bc9 - rustc_ty_utils[c85f993db90c93e8]::representability::representability_ty
  24:        0x115748a65 - rustc_ty_utils[c85f993db90c93e8]::representability::representability_ty
  25:        0x1157486f0 - rustc_ty_utils[c85f993db90c93e8]::representability::representability
  26:        0x116767502 - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_span[73e074df7cba0abf]::def_id::LocalDefId, rustc_middle[298407787cc28eff]::ty::adt::Representability>>
  27:        0x11685f309 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::representability, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  28:        0x1157484b8 - rustc_ty_utils[c85f993db90c93e8]::representability::representability
  29:        0x116767502 - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_span[73e074df7cba0abf]::def_id::LocalDefId, rustc_middle[298407787cc28eff]::ty::adt::Representability>>
  30:        0x11685f309 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::representability, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  31:        0x115a83b18 - rustc_hir_analysis[3f5a3f8b91adb05]::check::wfcheck::check_item
  32:        0x115a7511f - rustc_hir_analysis[3f5a3f8b91adb05]::check::wfcheck::check_well_formed
  33:        0x1167ca855 - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_hir[7dceeafd8b0a3ae3]::hir_id::OwnerId, ()>>
  34:        0x11685fba5 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::check_well_formed, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  35:        0x11597bf8b - rustc_data_structures[445d045225b5bae3]::sync::par_for_each_in::<&[rustc_hir[7dceeafd8b0a3ae3]::hir::ItemId], <rustc_middle[298407787cc28eff]::hir::ModuleItems>::par_items<rustc_hir_analysis[3f5a3f8b91adb05]::check::wfcheck::check_mod_type_wf::{closure#0}>::{closure#0}>
  36:        0x115a7e411 - rustc_hir_analysis[3f5a3f8b91adb05]::check::wfcheck::check_mod_type_wf
  37:        0x116770855 - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<rustc_span[73e074df7cba0abf]::def_id::LocalDefId, ()>>
  38:        0x11685fa85 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::check_mod_type_wf, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  39:        0x11597c08b - rustc_data_structures[445d045225b5bae3]::sync::par_for_each_in::<&[rustc_hir[7dceeafd8b0a3ae3]::hir_id::OwnerId], <rustc_middle[298407787cc28eff]::hir::map::Map>::par_for_each_module<rustc_hir_analysis[3f5a3f8b91adb05]::check_crate::{closure#5}::{closure#0}::{closure#0}>::{closure#0}>
  40:        0x1159e6f20 - <rustc_session[43d2d941ace548e5]::session::Session>::track_errors::<rustc_hir_analysis[3f5a3f8b91adb05]::check_crate::{closure#5}, ()>
  41:        0x115995352 - rustc_hir_analysis[3f5a3f8b91adb05]::check_crate
  42:        0x11305afda - rustc_interface[a666f6d755ed9279]::passes::analysis
  43:        0x1167f326c - rustc_query_system[fd3d689380a50de4]::query::plumbing::try_execute_query::<rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt, rustc_query_system[fd3d689380a50de4]::query::caches::DefaultCache<(), core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>>
  44:        0x11686d3f9 - rustc_query_system[fd3d689380a50de4]::query::plumbing::get_query::<rustc_query_impl[f77634870212a75e]::queries::analysis, rustc_query_impl[f77634870212a75e]::plumbing::QueryCtxt>
  45:        0x112f11296 - <rustc_interface[a666f6d755ed9279]::passes::QueryContext>::enter::<rustc_driver[ba5c792b9438c2b]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>
  46:        0x112f55a4d - rustc_span[73e074df7cba0abf]::with_source_map::<core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>, rustc_interface[a666f6d755ed9279]::interface::run_compiler<core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>, rustc_driver[ba5c792b9438c2b]::run_compiler::{closure#1}>::{closure#0}::{closure#1}>
  47:        0x112f4534c - <scoped_tls[c17fceff1b2b43d8]::ScopedKey<rustc_span[73e074df7cba0abf]::SessionGlobals>>::set::<rustc_interface[a666f6d755ed9279]::interface::run_compiler<core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>, rustc_driver[ba5c792b9438c2b]::run_compiler::{closure#1}>::{closure#0}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>
  48:        0x112f14e5a - std[8878bd7dd366d71b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[a666f6d755ed9279]::util::run_in_thread_pool_with_globals<rustc_interface[a666f6d755ed9279]::interface::run_compiler<core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>, rustc_driver[ba5c792b9438c2b]::run_compiler::{closure#1}>::{closure#0}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>
  49:        0x112efaf7b - <<std[8878bd7dd366d71b]::thread::Builder>::spawn_unchecked_<rustc_interface[a666f6d755ed9279]::util::run_in_thread_pool_with_globals<rustc_interface[a666f6d755ed9279]::interface::run_compiler<core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>, rustc_driver[ba5c792b9438c2b]::run_compiler::{closure#1}>::{closure#0}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[13371b8ce97f01bb]::result::Result<(), rustc_errors[b0e16a209d58ca7d]::ErrorGuaranteed>>::{closure#1} as core[13371b8ce97f01bb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  50:        0x106f97387 - std::sys::unix::thread::Thread::new::thread_start::hfd54df40dd14130e
  51:     0x7ff814d514e1 - __pthread_start

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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.66.0-nightly (0da281b60 2022-10-27) running on x86_64-apple-darwin

query stack during panic:
#0 [representability_adt_ty] checking if `Apple` is representable
#1 [representability] checking if `Apple::0` is representable
#2 [representability] checking if `Apple` is representable
#3 [check_well_formed] checking that `Apple` is well-formed
#4 [check_mod_type_wf] checking that types are well-formed in top-level module
#5 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 4 previous errors

Version

rustc 1.66.0-nightly (0da281b60 2022-10-27)
binary: rustc
commit-hash: 0da281b6068a7d889ae89a9bd8991284cc9b7535
commit-date: 2022-10-27
host: x86_64-apple-darwin
release: 1.66.0-nightly
LLVM version: 15.0.2

Regression

Regression from bba9785 (#100720)

Bisection details

bisected with cargo-bisect-rustc v0.6.4

searched nightlies: from nightly-2022-10-06 to nightly-2022-10-28
regressed nightly: nightly-2022-10-09
searched commit range: 8b0c05d...8796e7a
regressed commit: bba9785

Host triple: x86_64-apple-darwin

@jruderman jruderman added 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 Oct 30, 2022
@chenyukang
Copy link
Member

@camsteffen

if let hir::GenericArg::Type(ty) = arg && params_in_repr.contains(i as u32) {

params_in_repr.contains(i as u32) this function call crash for the assertion fail,
from the log, params_in_repr is with count = 1, and i is also 1 when crashing.
Do you have any clue?

params_in_repr is implemented at here, I'm still not quite familar with the query part:

query params_in_repr(key: DefId) -> rustc_index::bit_set::BitSet<u32> {

@camsteffen
Copy link
Contributor

It looks like the parens are parsed as generic parameters for a type that doesn't have generic parameters? The simple fix is just check i < params_in_repr.domain_size().

@chenyukang
Copy link
Member

I think a reasonable fix is modifying the API for BitSet,

   /// Returns `true` if `self` contains `elem`.
    #[inline]
    pub fn contains(&self, elem: T) -> bool {
-        assert!(elem.index() < self.domain_size);
+        if elem.index() >= self.domain_size { return false; }        
        let (word_index, mask) = word_index_and_mask(elem);
        (self.words[word_index] & mask) != 0
    }

It reasonable to add assertion assert!(elem.index() < self.domain_size) for insert, but it's better to return false for idx outside of expected range?

@chenyukang
Copy link
Member

Have a test on C++'s bitset, which also has this assertion:

terminate called after throwing an instance of 'std::out_of_range'
  what():  bitset::test: __position (which is 20) >= _Nb (which is 10)

emm, it's better to keep it for POLA.

@chenyukang
Copy link
Member

@rustbot claim

@camsteffen
Copy link
Contributor

I'd be curious to know if we can maintain an invariant in the HIR that it does not contain generics that do not align with generics_of. That was what I assumed when writing that code, but that may be too optimistic.

@camsteffen
Copy link
Contributor

I considered removing the assert too, but I think it's better to have it since it exposes invalid assumptions as in this case.

@compiler-errors
Copy link
Member

compiler-errors commented Oct 31, 2022

We should delay a bug (replacing the assertion) -- that's probably fine as long as it's understood why the generics_of does not line up here, and it's understood why we can't fix it somewhere upstream (e.g. in parsing or lowering instead)

@chenyukang
Copy link
Member

chenyukang commented Oct 31, 2022

@compiler-errors
I have a general question for rustc, why we just exit when we find a fatal error in parsing (which can not recover by simply autofix).

I have a real Dev experience when working on rustc, I was editing an file with 3k+ lines of code, and I forgetted remove an extra '{', then the compiler give about 20+ errors, the first two are from parsing:

error: this file contains an unclosed delimiter
    --> compiler/rustc_hir_typeck/src/method/suggest.rs:2649:3
     |
38   | impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
     |                                 - unclosed delimiter
...
110  |                                  sugg_span: Span| {
     |                                                   - this delimiter might not be properly closed...
...
240  |         };
     |         - ...as it matches this but it has different indentation
...
2649 | }
     |   ^

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
    --> compiler/rustc_hir_typeck/src/method/suggest.rs:1448:49
     |
1448 |             let self_ty = self.check_expr(expr) {
     |                                                 ^ expected one of `.`, `;`, `?`, `else`, or an operator

error: enum is not supported in `trait`s or `impl`s
    --> compiler/rustc_hir_typeck/src/method/suggest.rs:2557:1
     |
2557 | pub enum SelfSource<'a> {
     | ^^^^^^^^^^^^^^^^^^^^^^^

But there are many many other errors from later phases...
I need to scroll up several pages to the beginning of error to find the rootcause.

@chenyukang
Copy link
Member

If we can not get valid AST in parsing, later phases such as analysising will report out extra (mostly unhelpful) errors?

@camsteffen
Copy link
Contributor

We should delay a bug (replacing the assertion)

The assertion is inside of the BitSet data structure. I generally expect data structures to simply assert on pre-conditions. We could put a delay-bug before the contains call, but that also seems like overkill since that is already on the error-reporting path.

I have a general question for rustc, why we just exit when we find a fatal error in parsing

I agree there are cases where rustc should quit sooner. But in general I think we have to err on the side of recover-and-continue if there is some possibility that more helpful errors may be emitted.

@estebank
Copy link
Contributor

If we can not get valid AST in parsing, later phases such as analysising will report out extra (mostly unhelpful) errors?

This can happen. The parser should be more realistic on what it recovers and avoid errors from later passes that do not make sense. A few years back we would stop as soon as any error occurred in a given pass and avoid trying any further evaluation, and that was also a usability issue, because people would get a false sense of "how much work is left to fix". We shouldn't have a blanket evaluation stop, but maybe be more aggressive about pruning the AST when multiple parse errors have occurred.

@chenyukang
Copy link
Member

I'd be curious to know if we can maintain an invariant in the HIR that it does not contain generics that do not align with generics_of. That was what I assumed when writing that code, but that may be too optimistic.

@camsteffen do you want to take over this issue?
I currently don't have any clue about the rootcause.
Or we give it an simple fix now.

@camsteffen
Copy link
Contributor

I can take a look.

bors added a commit to rust-lang-ci/rust that referenced this issue Nov 11, 2022
Rollup of 9 pull requests

Successful merges:

 - rust-lang#100633 (Consider `#[must_use]` annotation on `async fn` as also affecting the `Future::Output`)
 - rust-lang#103445 (`#[test]`: Point at return type if `Termination` bound is unsatisfied)
 - rust-lang#103924 (Fix broken link in description of error code E0706)
 - rust-lang#104146 (Retry binding TCP Socket in remote-test-server)
 - rust-lang#104169 (Migrate `:target` rules to use CSS variables)
 - rust-lang#104202 (Fix ICE rust-lang#103748)
 - rust-lang#104216 (Don't ICE on operator trait methods with generic methods)
 - rust-lang#104217 (Display help message when fluent arg was referenced incorrectly)
 - rust-lang#104245 (Reduce default configuration's dependency upon static libstdcpp library (rust-lang#103606))

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in f3931c8 Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants