rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.1k stars 12.8k forks source link

ICE with cyclic type #133718

Open skibon02 opened 11 hours ago

skibon02 commented 11 hours ago

Code

/// what's funny is that this code was suggested by chatgpt

enum HierarchicalItem<T, I> {
    Element(T),
    Group(I),
}

struct HierarchicalIterator<I, T>
where
    I: Iterator<Item = HierarchicalItem<T, I>>,
{
    stack: Vec<I>,
}

impl<I, T> HierarchicalIterator<I, T>
where
    I: Iterator<Item = HierarchicalItem<T, I>>,
{
    fn new(root: I) -> Self {
        Self { stack: vec![root] }
    }
}

impl<I, T> Iterator for HierarchicalIterator<I, T>
where
    I: Iterator<Item = HierarchicalItem<T, I>>,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(current_iter) = self.stack.last_mut() {
            match current_iter.next() {
                Some(HierarchicalItem::Element(item)) => return Some(item),
                Some(HierarchicalItem::Group(sub_iter)) => self.stack.push(sub_iter),
                None => {
                    self.stack.pop();
                }
            }
        }
        None // No more elements to iterate
    }
}

fn main() {
    use HierarchicalItem::*;

    let sub_group = vec![
        Element(3),
        Element(4),
        Group(vec![Element(5), Element(6)].into_iter()),
    ];
    let root_group = vec![
        Element(1),
        Element(2),
        Group(sub_group.into_iter()),
    ];

    let iter = HierarchicalIterator::new(root_group.into_iter());

    let collected: Vec<_> = iter.collect();
    println!("{:?}", collected); // Output: [1, 2, 3, 4, 5, 6]
}

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.85.0-nightly (7442931d4 2024-11-30)
binary: rustc
commit-hash: 7442931d49b199ad0a1cc0f8ca54e327b5139b66
commit-date: 2024-11-30
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.4

Error output

error[E0271]: expected `IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>` to be an iterator that yields `HierarchicalItem<_, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>>`, but it yields `HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>`
  --> src/main.rs:60:42
   |
60 |     let iter = HierarchicalIterator::new(root_group.into_iter());
   |                ------------------------- ^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
   |                |
   |                required by a bound introduced by this call
   |
note: required by a bound in `HierarchicalIterator::<I, T>::new`
  --> src/main.rs:16:17
   |
16 |     I: Iterator<Item = HierarchicalItem<T, I>>,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `HierarchicalIterator::<I, T>::new`
17 | {
18 |     fn new(root: I) -> Self {
   |        --- required by a bound in this associated function

error[E0271]: expected `IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>` to be an iterator that yields `HierarchicalItem<_, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>>`, but it yields `HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>`
  --> src/main.rs:60:16
   |
60 |     let iter = HierarchicalIterator::new(root_group.into_iter());
   |                ^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
   |
note: required by a bound in `HierarchicalIterator`
  --> src/main.rs:9:17
   |
7  | struct HierarchicalIterator<I, T>
   |        -------------------- required by a bound in this struct
8  | where
9  |     I: Iterator<Item = HierarchicalItem<T, I>>,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `HierarchicalIterator`

error[E0271]: expected `IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>` to be an iterator that yields `HierarchicalItem<_, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>>>`, but it yields `HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, IntoIter<HierarchicalItem<{integer}, _>>>>>`
  --> src/main.rs:60:16
   |
60 |     let iter = HierarchicalIterator::new(root_group.into_iter());
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
   |
note: required by a bound in `HierarchicalIterator`
  --> src/main.rs:9:17
   |
7  | struct HierarchicalIterator<I, T>
   |        -------------------- required by a bound in this struct
8  | where
9  |     I: Iterator<Item = HierarchicalItem<T, I>>,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `HierarchicalIterator`

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/method/suggest.rs:1185:35:
internal error: entered unreachable code: encountered `Item(Item { ident: HierarchicalIterator#0, owner_id: DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator), kind: Struct(Struct { fields: [FieldDef { span: src/main.rs:11:5: 11:18 (#0), vis_span: src/main.rs:11:5: 11:5 (#0), ident: stack#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).19), def_id: DefId(0:15 ~ rust_ice[6b9d]::HierarchicalIterator::stack), ty: Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).15), kind: Path(Resolved(None, Path { span: src/main.rs:11:12: 11:18 (#0), res: Def(Struct, DefId(5:7570 ~ alloc[86f5]::vec::Vec)), segments: [PathSegment { ident: Vec#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).18), res: Def(Struct, DefId(5:7570 ~ alloc[86f5]::vec::Vec)), args: Some(GenericArgs { args: [Type(Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).16), kind: Path(Resolved(None, Path { span: src/main.rs:11:16: 11:17 (#0), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), segments: [PathSegment { ident: I#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).17), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), args: None, infer_args: false }] })), span: src/main.rs:11:16: 11:17 (#0) })], constraints: [], parenthesized: No, span_ext: src/main.rs:11:15: 11:18 (#0) }), infer_args: false }] })), span: src/main.rs:11:12: 11:18 (#0) }, safety: Safe }], recovered: No }, Generics { params: [GenericParam { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).13), def_id: DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I), name: Plain(I#0), span: src/main.rs:7:29: 7:30 (#0), pure_wrt_drop: false, kind: Type { default: None, synthetic: false }, colon_span: None, source: Generics }, GenericParam { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).14), def_id: DefId(0:14 ~ rust_ice[6b9d]::HierarchicalIterator::T), name: Plain(T#0), span: src/main.rs:7:32: 7:33 (#0), pure_wrt_drop: false, kind: Type { default: None, synthetic: false }, colon_span: None, source: Generics }], predicates: [WherePredicate { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).1), span: src/main.rs:9:5: 9:47 (#0), kind: BoundPredicate(WhereBoundPredicate { origin: WhereClause, bound_generic_params: [], bounded_ty: Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).2), kind: Path(Resolved(None, Path { span: src/main.rs:9:5: 9:6 (#0), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), segments: [PathSegment { ident: I#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).3), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), args: None, infer_args: false }] })), span: src/main.rs:9:5: 9:6 (#0) }, bounds: [Trait(PolyTraitRef { bound_generic_params: [], modifiers: TraitBoundModifiers { constness: Never, polarity: Positive }, trait_ref: TraitRef { path: Path { span: src/main.rs:9:8: 9:47 (#0), res: Def(Trait, DefId(2:8912 ~ core[c1d5]::iter::traits::iterator::Iterator)), segments: [PathSegment { ident: Iterator#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).11), res: Def(Trait, DefId(2:8912 ~ core[c1d5]::iter::traits::iterator::Iterator)), args: Some(GenericArgs { args: [], constraints: [AssocItemConstraint { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).10), ident: Item#0, gen_args: GenericArgs { args: [], constraints: [], parenthesized: No, span_ext: no-location (#0) }, kind: Equality { term: Ty(Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).4), kind: Path(Resolved(None, Path { span: src/main.rs:9:24: 9:46 (#0), res: Def(Enum, DefId(0:3 ~ rust_ice[6b9d]::HierarchicalItem)), segments: [PathSegment { ident: HierarchicalItem#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).9), res: Def(Enum, DefId(0:3 ~ rust_ice[6b9d]::HierarchicalItem)), args: Some(GenericArgs { args: [Type(Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).5), kind: Path(Resolved(None, Path { span: src/main.rs:9:41: 9:42 (#0), res: Def(TyParam, DefId(0:14 ~ rust_ice[6b9d]::HierarchicalIterator::T)), segments: [PathSegment { ident: T#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).6), res: Def(TyParam, DefId(0:14 ~ rust_ice[6b9d]::HierarchicalIterator::T)), args: None, infer_args: false }] })), span: src/main.rs:9:41: 9:42 (#0) }), Type(Ty { hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).7), kind: Path(Resolved(None, Path { span: src/main.rs:9:44: 9:45 (#0), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), segments: [PathSegment { ident: I#0, hir_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).8), res: Def(TyParam, DefId(0:13 ~ rust_ice[6b9d]::HierarchicalIterator::I)), args: None, infer_args: false }] })), span: src/main.rs:9:44: 9:45 (#0) })], constraints: [], parenthesized: No, span_ext: src/main.rs:9:40: 9:46 (#0) }), infer_args: false }] })), span: src/main.rs:9:24: 9:46 (#0) }) }, span: src/main.rs:9:17: 9:46 (#0) }], parenthesized: No, span_ext: src/main.rs:9:16: 9:47 (#0) }), infer_args: false }] }, hir_ref_id: HirId(DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator).12) }, span: src/main.rs:9:8: 9:47 (#0) })] }) }], has_where_clause_predicates: true, where_clause_span: src/main.rs:8:1: 9:48 (#0), span: src/main.rs:7:28: 7:34 (#0) }), span: src/main.rs:7:1: 12:2 (#0), vis_span: src/main.rs:7:1: 7:1 (#0) })` due to `Some(
    ObligationCause {
        span: src/main.rs:63:34: 63:41 (#0),
        body_id: DefId(0:25 ~ rust_ice[6b9d]::main),
        code: WhereClause(
            DefId(0:12 ~ rust_ice[6b9d]::HierarchicalIterator),
            src/main.rs:9:17: 9:46 (#0),
        ),
    },
)`
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_hir_typeck::fn_ctxt::FnCtxt>::report_no_match_method_error
   3: <rustc_hir_typeck::fn_ctxt::FnCtxt>::report_method_error
   4: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args::{closure#0}
   5: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
   6: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
   7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
   8: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args::{closure#0}
   9: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  10: rustc_hir_typeck::check::check_fn
  11: rustc_hir_typeck::typeck
      [... omitted 1 frame ...]
  12: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#4}>::{closure#0}
  13: rustc_hir_analysis::check_crate
  14: rustc_interface::passes::run_required_analyses
  15: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  16: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/skygrel19/projects/rust-ice/rustc-ice-2024-12-01T22_34_32-68884.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [typeck] type-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
For more information about this error, try `rustc --explain E0271`.
error: could not compile `rust-ice` (bin "rust-ice") due to 3 previous errors
Backtrace

``` stack backtrace: 0: 0x783e5a56a0da - ::fmt::h41650409cfcaa385 1: 0x783e5ac13c26 - core::fmt::write::h124e50a41a614f35 2: 0x783e5bc0f2d1 - std::io::Write::write_fmt::h152c770d77d8200b 3: 0x783e5a569f32 - std::sys::backtrace::BacktraceLock::print::hb77a071b09f7b2d4 4: 0x783e5a56c43a - std::panicking::default_hook::{{closure}}::h2289b310e8f70172 5: 0x783e5a56c283 - std::panicking::default_hook::h6b36901e2a542246 6: 0x783e596df618 - std[562fec4e669516d]::panicking::update_hook::>::{closure#0} 7: 0x783e5a56cbf8 - std::panicking::rust_panic_with_hook::hd7b2147b933d3e5b 8: 0x783e5a56c8ea - std::panicking::begin_panic_handler::{{closure}}::h529091dbfbeb0f88 9: 0x783e5a56a589 - std::sys::backtrace::__rust_end_short_backtrace::h560f3f6f1bef32f3 10: 0x783e5a56c5ad - rust_begin_unwind 11: 0x783e57963f70 - core::panicking::panic_fmt::h0831c2ae5fe21ab8 12: 0x783e59a1396c - ::report_no_match_method_error 13: 0x783e59a412a5 - ::report_method_error 14: 0x783e5bce83c8 - ::check_expr_with_expectation_and_args::{closure#0} 15: 0x783e5b6c0f1c - ::check_expr_with_expectation_and_args 16: 0x783e5b6bbcb9 - ::check_decl 17: 0x783e5b6be661 - ::check_expr_block 18: 0x783e5bcd7abf - ::check_expr_with_expectation_and_args::{closure#0} 19: 0x783e5b6c0f1c - ::check_expr_with_expectation_and_args 20: 0x783e5af3e1a6 - rustc_hir_typeck[56a70210104cd44b]::check::check_fn 21: 0x783e5af4616f - rustc_hir_typeck[56a70210104cd44b]::typeck 22: 0x783e5af44d5d - rustc_query_impl[1e7641ea5651ea82]::plumbing::__rust_begin_short_backtrace::> 23: 0x783e5aeac726 - rustc_query_system[76793d9e0f70d10d]::query::plumbing::try_execute_query::, rustc_query_system[76793d9e0f70d10d]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[1e7641ea5651ea82]::plumbing::QueryCtxt, true> 24: 0x783e5b18920e - rustc_query_impl[1e7641ea5651ea82]::query_impl::typeck::get_query_incr::__rust_end_short_backtrace 25: 0x783e5aea8d73 - ::par_body_owners::::{closure#0} 26: 0x783e5aea6d92 - rustc_hir_analysis[e6cbdf146491ad07]::check_crate 27: 0x783e5b535d7c - rustc_interface[c91af76d3bd1abc4]::passes::run_required_analyses 28: 0x783e5b530dde - rustc_interface[c91af76d3bd1abc4]::passes::analysis 29: 0x783e5b530daf - rustc_query_impl[1e7641ea5651ea82]::plumbing::__rust_begin_short_backtrace::> 30: 0x783e5bd0dfd2 - rustc_query_system[76793d9e0f70d10d]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[1e7641ea5651ea82]::plumbing::QueryCtxt, true> 31: 0x783e5bd0dab7 - rustc_query_impl[1e7641ea5651ea82]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace 32: 0x783e5bc94207 - rustc_interface[c91af76d3bd1abc4]::interface::run_compiler::, rustc_driver_impl[754960437973ebe]::run_compiler::{closure#0}>::{closure#1} 33: 0x783e5b9ee461 - std[562fec4e669516d]::sys::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[754960437973ebe]::run_compiler::{closure#0}>::{closure#1}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>>::{closure#0}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>> 34: 0x783e5b9ee108 - <::spawn_unchecked_, rustc_driver_impl[754960437973ebe]::run_compiler::{closure#0}>::{closure#1}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>>::{closure#0}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c1d5b3a4b0af00f7]::result::Result<(), rustc_span[218a417b0d7920e2]::ErrorGuaranteed>>::{closure#1} as core[c1d5b3a4b0af00f7]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 35: 0x783e5b9ed83b - std::sys::pal::unix::thread::Thread::new::thread_start::hf9fb86827535111f 36: 0x783e55c9ca94 - start_thread at ./nptl/pthread_create.c:447:8 37: 0x783e55d29c3c - clone3 at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78 38: 0x0 - ```

jieyouxu commented 8 hours ago

ICE in HIR typeck diagnostics report_no_match_method_error.

matthiaskrgr commented 2 hours ago

probably dupe of https://github.com/rust-lang/rust/issues/124350