rust-lang / rust

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

Compiler panic due to iter adapters #118590

Open jc65536 opened 11 months ago

jc65536 commented 11 months ago

I tried this code:

use std::io::stdin;

use nom::{
    bytes::complete::{tag, take_until},
    character::complete::{self, digit1},
    multi::{many0, many1},
    sequence::tuple,
    IResult,
};

fn parse_num(input: &str) -> IResult<&str, u32> {
    let (input, _) = many0(complete::char(' '))(input)?;
    let (input, num) = digit1(input)?;
    Ok((input, num.parse().unwrap()))
}

fn parse_card(input: &str) -> IResult<&str, u32> {
    let (input, _) = tuple((take_until(":"), tag(":")))(input)?;
    let (input, mut winning_nums) = many1(parse_num)(input)?;
    let (input, _) = tag(" | ")(input)?;
    let (input, mut my_nums) = many1(parse_num)(input)?;

    winning_nums.sort();
    my_nums.sort();

    Ok((input, calc_score(winning_nums, my_nums, 0)))
}

fn calc_score(
    winning_nums: impl IntoIterator<Item = u32>,
    my_nums: impl IntoIterator<Item = u32>,
    points: u32,
) -> u32 {
    let mut winning_nums = winning_nums.into_iter().peekable();
    let mut my_nums = my_nums.into_iter().peekable();
    if let (Some(&w), Some(&m)) = (winning_nums.peek(), my_nums.peek()) {
        if w > m {
            calc_score(winning_nums, my_nums.skip(1), points)
        } else if w < m {
            calc_score(winning_nums.skip(1), my_nums, points)
        } else {
            calc_score(
                winning_nums,
                my_nums.skip(1),
                if points == 0 { 1 } else { points * 2 },
            )
        }
    } else {
        points
    }
}

fn part1() -> u32 {
    stdin().lines().into_iter().fold(0, |acc, line| {
        acc + parse_card(line.unwrap().as_str()).unwrap().1
    })
}

fn main() {
    println!("{}", part1());
}

I expected to see this happen: cargo should compile successfully or with an error message

Instead, this happened: compiler panicked

Meta

rustc --version --verbose:

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4
Backtrace

``` Compiling advent-of-code-2023 v0.1.0 (/home/jason/projects/advent-of-code-2023) thread 'rustc' panicked at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/compiler/rustc_type_ir/src/lib.rs:759:17: type variables should not be hashed: ?0t stack backtrace: 0: rust_begin_unwind at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5 1: core::panicking::panic_fmt at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14 2: >::hash_stable 3: > as rustc_data_structures::stable_hasher::HashStable>::hash_stable 4: <&rustc_middle::ty::list::List as rustc_data_structures::stable_hasher::HashStable>::hash_stable 5: > as rustc_data_structures::stable_hasher::HashStable>::hash_stable 6: )>>::call_once 7: rustc_query_system::query::plumbing::try_execute_query::, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true> 8: >::try_fold_ty 9: rustc_ty_utils::needs_drop::needs_drop_raw [... omitted 1 frame ...] 10: rustc_ty_utils::instance::resolve_instance [... omitted 1 frame ...] 11: ::resolve_drop_in_place 12: rustc_monomorphize::collector::collect_used_items 13: rustc_monomorphize::collector::collect_items_rec 14: rustc_monomorphize::collector::collect_items_rec 15: rustc_monomorphize::collector::collect_items_rec 16: rustc_monomorphize::collector::collect_items_rec 17: rustc_monomorphize::collector::collect_items_rec 18: rustc_monomorphize::collector::collect_items_rec 19: rustc_monomorphize::collector::collect_items_rec 20: rustc_monomorphize::collector::collect_items_rec 21: rustc_monomorphize::collector::collect_items_rec 22: rustc_monomorphize::collector::collect_items_rec 23: rustc_monomorphize::collector::collect_items_rec 24: rustc_monomorphize::collector::collect_items_rec 25: rustc_monomorphize::collector::collect_items_rec 26: rustc_monomorphize::collector::collect_items_rec 27: rustc_monomorphize::collector::collect_items_rec 28: rustc_monomorphize::collector::collect_items_rec 29: rustc_monomorphize::collector::collect_items_rec 30: rustc_monomorphize::collector::collect_items_rec 31: rustc_monomorphize::collector::collect_items_rec 32: rustc_monomorphize::collector::collect_items_rec 33: rustc_monomorphize::collector::collect_items_rec 34: rustc_monomorphize::collector::collect_items_rec 35: rustc_monomorphize::collector::collect_items_rec 36: rustc_monomorphize::collector::collect_items_rec 37: rustc_monomorphize::collector::collect_items_rec 38: rustc_monomorphize::collector::collect_items_rec 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 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: rustc 1.74.0 (79e9716c9 2023-11-13) running on x86_64-unknown-linux-gnu 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 [try_normalize_generic_arg_after_erasing_regions] normalizing `core::option::Option<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> as core::iter::traits::iterator::Iterator>::Item>` #1 [needs_drop_raw] computing whether `core::iter::adapters::peekable::Peekable>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` needs drop #2 [resolve_instance] resolving instance `core::ptr::drop_in_place::>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` #3 [collect_and_partition_mono_items] collect_and_partition_mono_items end of query stack error: could not compile `advent-of-code-2023` (bin "day-04") ```

lqd commented 11 months ago

Can you try on the latest nightly? I believe this was recently fixed.

jc65536 commented 11 months ago

Nightly didn't work for me either. Is this the correct version?

[jason@tiramisu advent-of-code-2023]$ rustup run nightly cargo --version
cargo 1.76.0-nightly (623b78849 2023-12-02)
[jason@tiramisu advent-of-code-2023]$ rustup run nightly cargo run --bin test
   Compiling advent-of-code-2023 v0.1.0 (/home/jason/projects/advent-of-code-2023)
thread 'rustc' panicked at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/compiler/rustc_type_ir/src/ty_kind.rs:750:17:
lqd commented 11 months ago

The latest is 12/4 or 5 but this instance is still not fixed indeed. There's still the "type variables should not be hashed: ?0t" ICE.

This looks like an old regression from nightly-2022-04-08.

Marking it as t-compiler, but could be t-types since it seems to be in normalization and @spastorino's recent work was related to similar issues.

Luk-ESC commented 10 months ago

Minimized:

fn main() {
    recurse(std::iter::empty::<()>())
}

fn recurse(nums: impl Iterator) {
    if true { return }

    recurse(nums.skip(42).peekable())
}