Skip to content

ICE when working with infinitely nesting types and ?-operator #92004

Description

@dastbe

Code

I've hacked it down from an advent of code solution. When

    let version = extend_number(0, itr, 3)?;
    let type_id = extend_number(0, itr, 3)?;

is replaced with u32 literals the compiler emits a (correct) compilation error due to infinitely nesting types.

use std::vec::Vec;
use std::iter::Peekable;

pub fn main() {
    let packet = decode(vec![1,0,1,0]);
}

pub fn decode(bitstream: Vec<u64>) -> Packet {
    let mut bitstream_itr = bitstream.into_iter().peekable();
    return match decode_packet(&mut bitstream_itr) {
        Some(p) => p,
        None    => panic!("expected outer packet"),
    }
}

pub fn decode_packets<I: Iterator<Item = u64>>(itr: &mut Peekable<I>) -> Vec<Packet> {
    let mut res = Vec::new();
    loop {
        match decode_packet(itr) {
            Some(p) => { res.push(p); },
            None    => break
        }
    }

    return res;
}

pub fn decode_packet<I: Iterator<Item = u64>>(itr: &mut Peekable<I>) -> Option<Packet> {
    // get version digits
    let version = extend_number(0, itr, 3)?;
    let type_id = extend_number(0, itr, 3)?;
    return operator_packet(version, type_id, itr);
}

pub fn operator_packet<I: Iterator<Item = u64>>(version: u64, type_id: u64, itr: &mut Peekable<I>) -> Option<Packet> {
    let p = OperatorPacket {
        version: version,
        type_id: type_id,
        packets: decode_packets(&mut itr.take(0).peekable()),
    };

    return Some(Packet::Operator(p));
}

pub fn extend_number<I: Iterator<Item = u64>>(num: u64, itr: &mut Peekable<I>, take: u64) -> Option<u64> {
    let mut value = num;
    for _ in 0..take {
        value *= 2;
        value += itr.next()?;
    }

    return Some(value);
}

#[derive(Debug)]
pub enum Packet {
    Operator(OperatorPacket),
}

#[derive(Debug)]
pub struct OperatorPacket {
    version: u64,
    type_id: u64,
    packets: Vec<Packet>
}

Meta

rustc --version --verbose:

rustc 1.59.0-nightly (db9d361a4 2021-11-28)
binary: rustc
commit-hash: db9d361a4731ca0bb48533fab6297a8fea75696f
commit-date: 2021-11-28
host: aarch64-apple-darwin
release: 1.59.0-nightly
LLVM version: 13.0.0

Error output

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', /Users/runner/.cargo/registry/src/github.life-white.uk-1ecc6299db9ec823/ena-0.14.0/src/snapshot_vec.rs:199:10
note: run with `RUST_BACKTRACE=1` environment variable to display a 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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.59.0-nightly (db9d361a4 2021-11-28) running on aarch64-apple-darwin

note: compiler flags: -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental --crate-type bin

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

query stack during panic:
#0 [is_copy_raw] computing whether `core::option::Option<core::option::Option<_>>` is `Copy`
#1 [needs_drop_raw] computing whether `core::option::Option<core::option::Option<_>>` needs drop
end of query stack
warning: `aoc-uh-oh` (bin "aoc-uh-oh") generated 4 warnings
error: could not compile `aoc-uh-oh`; 4 warnings emitted
Backtrace

stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: <rustc_infer::infer::type_variable::TypeVariableTable>::probe
   4: <rustc_infer::infer::InferCtxt>::probe_ty_var
   5: <rustc_infer::infer::canonical::canonicalizer::Canonicalizer as rustc_middle::ty::fold::TypeFolder>::fold_ty
   6: <&rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg> as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
   7: <&rustc_middle::ty::TyS as rustc_middle::ty::fold::TypeFoldable>::super_fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
   8: <&rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg> as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
   9: <&rustc_middle::ty::TyS as rustc_middle::ty::fold::TypeFoldable>::super_fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
  10: <&rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg> as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
  11: <rustc_middle::ty::PredicateKind as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::canonical::canonicalizer::Canonicalizer>
  12: <rustc_infer::infer::canonical::canonicalizer::Canonicalizer>::canonicalize::<rustc_middle::ty::ParamEnvAnd<rustc_middle::ty::Predicate>>
  13: <rustc_infer::infer::InferCtxt as rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt>::evaluate_obligation
  14: <rustc_infer::infer::InferCtxt as rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt>::evaluate_obligation_no_overflow
  15: rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions
  16: <rustc_infer::infer::InferCtxtBuilder>::enter::<bool, rustc_ty_utils::common_traits::is_item_raw::{closure#0}>
  17: rustc_ty_utils::common_traits::is_copy_raw
  18: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>
  19: rustc_data_structures::stack::ensure_sufficient_stack::<(bool, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>::{closure#3}>
  20: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>>
  21: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::is_copy_raw, rustc_query_impl::plumbing::QueryCtxt>
  22: <rustc_middle::ty::TyS>::is_copy_modulo_regions
  23: rustc_ty_utils::needs_drop::needs_drop_raw
  24: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>
  25: rustc_data_structures::stack::ensure_sufficient_stack::<(bool, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>::{closure#3}>
  26: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<&rustc_middle::ty::TyS>, bool>>
  27: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::needs_drop_raw, rustc_query_impl::plumbing::QueryCtxt>
  28: rustc_ty_utils::instance::inner_resolve_instance
  29: rustc_ty_utils::instance::resolve_instance
  30: <rustc_middle::dep_graph::dep_node::DepKind as rustc_query_system::dep_graph::DepKind>::with_deps::<<rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task_impl<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<(rustc_span::def_id::DefId, &rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg>)>, core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>>::{closure#0}, core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>>
  31: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<(rustc_span::def_id::DefId, &rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg>)>, core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>>
  32: rustc_data_structures::stack::ensure_sufficient_stack::<(core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::ParamEnvAnd<(rustc_span::def_id::DefId, &rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg>)>, core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>>::{closure#3}>
  33: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::resolve_instance, rustc_query_impl::plumbing::QueryCtxt>
  34: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::resolve_instance
  35: <rustc_middle::ty::instance::Instance>::resolve_opt_const_arg
  36: <rustc_middle::ty::instance::Instance>::resolve_drop_in_place
  37: <rustc_monomorphize::collector::MirNeighborCollector as rustc_middle::mir::visit::Visitor>::visit_terminator
  38: rustc_monomorphize::collector::collect_neighbours
  39: rustc_monomorphize::collector::collect_items_rec
  40: rustc_monomorphize::collector::collect_items_rec
  41: rustc_monomorphize::collector::collect_items_rec
  42: rustc_monomorphize::collector::collect_items_rec
  43: rustc_monomorphize::collector::collect_items_rec
  44: rustc_monomorphize::collector::collect_items_rec
  45: rustc_monomorphize::collector::collect_items_rec
  46: rustc_monomorphize::collector::collect_items_rec
  47: rustc_monomorphize::collector::collect_items_rec
  48: rustc_monomorphize::collector::collect_items_rec
  49: rustc_monomorphize::collector::collect_items_rec
  50: rustc_monomorphize::collector::collect_items_rec
  51: rustc_monomorphize::collector::collect_items_rec
  52: rustc_monomorphize::collector::collect_items_rec
  53: rustc_monomorphize::collector::collect_items_rec
  54: rustc_monomorphize::collector::collect_items_rec
  55: rustc_monomorphize::collector::collect_items_rec
  56: rustc_monomorphize::collector::collect_items_rec
  57: rustc_monomorphize::collector::collect_items_rec
  58: rustc_monomorphize::collector::collect_items_rec
  59: rustc_monomorphize::collector::collect_items_rec
  60: rustc_monomorphize::collector::collect_items_rec
  61: rustc_monomorphize::collector::collect_items_rec
  62: rustc_monomorphize::collector::collect_items_rec
  63: rustc_monomorphize::collector::collect_items_rec
  64: rustc_monomorphize::collector::collect_items_rec
  65: rustc_monomorphize::collector::collect_items_rec
  66: rustc_monomorphize::collector::collect_items_rec
  67: rustc_monomorphize::collector::collect_items_rec
  68: rustc_monomorphize::collector::collect_items_rec
  69: rustc_monomorphize::collector::collect_items_rec
  70: rustc_monomorphize::collector::collect_items_rec
  71: rustc_monomorphize::collector::collect_items_rec
  72: rustc_monomorphize::collector::collect_items_rec
  73: rustc_monomorphize::collector::collect_items_rec
  74: rustc_monomorphize::collector::collect_items_rec
  75: rustc_monomorphize::collector::collect_items_rec
  76: rustc_monomorphize::collector::collect_items_rec
  77: rustc_monomorphize::collector::collect_items_rec
  78: rustc_monomorphize::collector::collect_items_rec
  79: rustc_monomorphize::collector::collect_items_rec
  80: rustc_monomorphize::collector::collect_items_rec
  81: rustc_monomorphize::collector::collect_items_rec
  82: rustc_monomorphize::collector::collect_items_rec
  83: rustc_monomorphize::collector::collect_items_rec
  84: rustc_monomorphize::collector::collect_items_rec
  85: rustc_monomorphize::collector::collect_items_rec
  86: rustc_monomorphize::collector::collect_items_rec
  87: rustc_monomorphize::collector::collect_items_rec
  88: rustc_monomorphize::collector::collect_items_rec
  89: rustc_monomorphize::collector::collect_items_rec
  90: rustc_monomorphize::collector::collect_items_rec
  91: rustc_monomorphize::collector::collect_items_rec
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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.59.0-nightly (db9d361a4 2021-11-28) running on aarch64-apple-darwin

note: compiler flags: -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental --crate-type bin

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

query stack during panic:
#0 [is_copy_raw] computing whether `core::option::Option<core::option::Option<_>>` is `Copy`
#1 [needs_drop_raw] computing whether `core::option::Option<core::option::Option<_>>` needs drop
#2 [resolve_instance] resolving instance `core::ptr::drop_in_place::<core::option::Option<core::option::Option<_>>>`
#3 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

Activity

  1. added
    C-bugCategory: This is a bug.
    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.
    on Dec 16, 2021
  2. Aaron1011 commented on Dec 16, 2021

    @Aaron1011
    Contributor

    We're incorrectly invoking resolve_instance with an inference car - this should never happen, but we seem to still have inference cars present during monomorphization.

    I suspect that this is related to the overflow, and could be solved by skipping Instance::resolve and calling delay_span_bug when we encounter an unresolved inference variable.

  3. JohnTitor commented on Aug 23, 2022

    @JohnTitor
    Member

    Triage: Fixed by #100757, I think it has a regression test that should cover this case, closing.

  4. matthiaskrgr commented on Nov 24, 2022

    @matthiaskrgr
    Member

    Reopened by #104610

  5. compiler-errors commented on Sep 22, 2023

    @compiler-errors
    Contributor

    This now overflows with an error.

  6. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Sep 22, 2023
  7. matthiaskrgr commented on Mar 25, 2024

    @matthiaskrgr
    Member

    This crashes again 🤣

  8. removed
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Mar 25, 2024
  9. matthiaskrgr commented on Mar 25, 2024

    @matthiaskrgr
    Member

    another regression of #122568

  10. matthiaskrgr commented on Mar 25, 2024

    @matthiaskrgr
    Member

    Ah this is where I got the code of #122823 from lol

  11. 1 remaining item

  12. added a commit that references this issue on Oct 13, 2025
    36e4f5d
  13. added a commit that references this issue on Oct 13, 2025
    ebb8df6
  14. added a commit that references this issue on Oct 14, 2025
    71cd25a
  15. added a commit that references this issue on Oct 18, 2025
    e0d9479
  16. added a commit that references this issue on Oct 20, 2025
    6c47a42
  17. added a commit that references this issue on Nov 2, 2025
    508472a
  18. added 4 commits that reference this issue on Nov 4, 2025
    1a0720f
    17d58b5
    352093b
    02f84b8
  19. added a commit that references this issue on Nov 27, 2025
    6c42eed
  20. added a commit that references this issue on Dec 21, 2025
    f6f2c21
  21. added a commit that references this issue on Dec 29, 2025
    26644cf
  22. added a commit that references this issue on Mar 27, 2026
    52b22ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions