rust-lang / rust

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

rustc panicked with a simple code #107991

Open NMAGZ opened 1 year ago

NMAGZ commented 1 year ago

Code

mod bytes_num {
    use std::{error::Error, time::Duration};

    const NANOSECOND_CHARS: &[char] = &['n', 's'];
    const MICROSECOND_CHARS: &[char] = &['u', 's'];
    const MICROSECOND_GREEK_CHARS: &[char] = &['μ', 's'];
    const MILLISECOND_CHARS: &[char] = &['m', 's'];
    const SECOND_CHARS: &[char] = &['s'];
    const MINUTE_CHARS: &[char] = &['m'];
    const HOUR_CHARS: &[char] = &['h'];

    const NANOSECOND: u64 = 1;
    const MICROSECOND: u64 = NANOSECOND * 1000;
    const MILLISECOND: u64 = MICROSECOND * 1000;
    const SECOND: u64 = MILLISECOND * 1000;
    const MINUTE: u64 = SECOND * 60;
    const HOUR: u64 = MINUTE * 60;

    pub(crate) fn foo(bar: &str) -> Result<Duration, Box<dyn Error>> {
        let u = ['s'].as_slice();
        let unit = match u {
            NANOSECOND_CHARS => NANOSECOND,
            MICROSECOND_CHARS => MICROSECOND,
            MICROSECOND_GREEK_CHARS => MICROSECOND,
            MILLISECOND_CHARS => MILLISECOND,
            SECOND_CHARS => SECOND,
            MINUTE_CHARS => MINUTE,
            HOUR_CHARS => HOUR,
            _ => return Err(From::from(format!("Unknown Unit '{:?}' in '{}'", u, bar))),
        };

        Ok(Duration::from_secs(1))
    }
}

mod duration {
    use std::error::Error;

    const BYTE_CHARS: &[char] = &['b'];

    const KILOBYTES_CHARS: &[char] = &['k', 'b'];
    const KIBIBYTES_CHARS: &[char] = &['k', 'i', 'b'];
    const KIBIBYTES_SHORT: &[char] = &['k'];
    const MEGABYTES_CHARS: &[char] = &['m', 'b'];
    const MEBIBYTES_CHARS: &[char] = &['m', 'i', 'b'];
    const MEBIBYTES_SHORT: &[char] = &['m'];
    const GIGABYTES_CHARS: &[char] = &['g', 'b'];
    const GIBIBYTES_CHARS: &[char] = &['g', 'i', 'b'];
    const GIBIBYTES_SHORT: &[char] = &['g'];

    const BYTES: u64 = 1;
    const KILOBYTES: u64 = BYTES * 1000;
    const KIBIBYTES: u64 = BYTES * 1024;
    const MEGABYTES: u64 = KILOBYTES * 1000;
    const MEBIBYTES: u64 = KIBIBYTES * 1024;
    const GIGABYTES: u64 = MEGABYTES * 1000;
    const GIBIBYTES: u64 = MEBIBYTES * 1024;

    pub(crate) fn baz(qux: &str) -> Result<u64, Box<dyn Error>> {
        let u = ['b'].as_slice();
        let unit = match u {
            BYTE_CHARS => BYTES,
            KILOBYTES_CHARS => KILOBYTES,
            KIBIBYTES_CHARS | KIBIBYTES_SHORT => KIBIBYTES,
            MEGABYTES_CHARS => MEGABYTES,
            MEBIBYTES_CHARS | MEBIBYTES_SHORT => MEBIBYTES,
            GIGABYTES_CHARS => GIGABYTES,
            GIBIBYTES_CHARS | GIBIBYTES_SHORT => GIBIBYTES,
            _ => return Err(From::from(format!("Unknown unit '{:?}' in '{}'", u, qux))),
        };

        Ok(1)
    }
}

fn main() {
    println!("Hello world");
}

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: aarch64-apple-darwin
release: 1.67.1
LLVM version: 15.0.6

Error output

thread 'rustc' panicked at 'forcing query with already existing `DepNode`
- query-key: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, constness: NotConst }, value: Val(ByRef { alloc: ConstAllocation { .. }, offset: Size(0 bytes) }, &[char]) }
- dep-node: deref_mir_constant(6c0d3808aaf7532e-5e094c413a0202f3)', /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/compiler/rustc_query_system/src/dep_graph/graph.rs:316:9
Backtrace

``` > RUST_BACKTRACE=1 cargo build Compiling demo v0.1.0 (/Users/zipper/Rust/magz/demo) thread 'rustc' panicked at 'forcing query with already existing `DepNode` - query-key: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, constness: NotConst }, value: Val(ByRef { alloc: ConstAllocation { .. }, offset: Size(0 bytes) }, &[char]) } - dep-node: deref_mir_constant(6c0d3808aaf7532e-5e094c413a0202f3)', /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/compiler/rustc_query_system/src/dep_graph/graph.rs:316:9 stack backtrace: 0: _rust_begin_unwind 1: core::panicking::panic_fmt 2: >::with_task::, rustc_middle::mir::ConstantKind> 3: rustc_query_system::query::plumbing::get_query:: 4: ::deref_mir_constant 5: ::recur 6: ::to_pat 7: ::const_to_pat 8: ::lower_path 9: ::lower_pattern 10: ::lower_pattern 11: ::lower_pattern 12: as alloc::vec::spec_from_iter::SpecFromIter, ::check_match::{closure#0}>>>::from_iter 13: ::visit_expr 14: ::visit_local 15: rustc_hir::intravisit::walk_expr:: 16: ::visit_expr 17: rustc_mir_build::thir::pattern::check_match::check_match 18: >::with_task:: 19: rustc_query_system::query::plumbing::try_execute_query::> 20: rustc_query_system::query::plumbing::get_query:: 21: rustc_data_structures::sync::par_for_each_in::<&[rustc_span::def_id::LocalDefId], ::par_body_owners::{closure#0}> 22: ::time::<(), rustc_interface::passes::analysis::{closure#1}::{closure#0}::{closure#0}> 23: ::time::<(), rustc_interface::passes::analysis::{closure#1}> 24: rustc_interface::passes::analysis 25: >::with_task::> 26: rustc_query_system::query::plumbing::try_execute_query::>> 27: rustc_query_system::query::plumbing::get_query:: 28: ::enter::> 29: rustc_span::with_source_map::, rustc_interface::interface::run_compiler, rustc_driver::run_compiler::{closure#1}>::{closure#0}::{closure#0}> 30: >::set::, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>> 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.67.1 (d5a82bbd2 2023-02-07) running on aarch64-apple-darwin note: compiler flags: --crate-type bin -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental=[REDACTED] note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [deref_mir_constant] dereferencing MIR constant #1 [check_match] match-checking `duration::baz` #2 [analysis] running analysis passes on this crate end of query stack warning: unused variable: `unit` --> src/main.rs:21:13 | 21 | let unit = match u { | ^^^^ help: if this is intentional, prefix it with an underscore: `_unit` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `unit` --> src/main.rs:61:13 | 61 | let unit = match u { | ^^^^ help: if this is intentional, prefix it with an underscore: `_unit` warning: `demo` (bin "demo") generated 2 warnings error: could not compile `demo`; 2 warnings emitted ```

matthiaskrgr commented 1 year ago

Probably another case of https://github.com/rust-lang/rust/issues/83085

langston-barrett commented 1 year ago

Can't reproduce with rustc 1.68 nor nightly

rustc 1.70.0-nightly (511364e78 2023-03-16)
binary: rustc
commit-hash: 511364e7874dba9649a264100407e4bffe7b5425
commit-date: 2023-03-16
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

@rustbot label +S-bug-has-mcve

matthiaskrgr commented 1 year ago

This still reproduces with -Cincremental=. -Zincremental-verify-ich=yes

langston-barrett commented 1 year ago

Thanks @matthiaskrgr, I was able to reproduce with those flags. Minimized with icemelter ice.rs -- rustc +nightly -Cincremental=. -Zincremental-verify-ich=yes (see Icemelter):

mod bytes_num {
    use std::{error::Error, time::Duration};

    const MINUTE_CHARS: &[char] = &['m'];

    const NANOSECOND: u64 = 1;
    const MICROSECOND: u64 = NANOSECOND * 1000;
    const MILLISECOND: u64 = MICROSECOND * 1000;
    const SECOND: u64 = MILLISECOND * 1000;
    const MINUTE: u64 = SECOND * 60;

    fn foo(bar: &str) -> Result<Duration, Box<Error>> {
        let u = [].as_slice();
        let unit = match u {
            MINUTE_CHARS => MINUTE,

            _ => return Err(From::from(format!("Unknown Unit '{:?}' in '{}'", u, bar))),
        };

        Ok(Duration::from_secs(1))
    }
}

mod duration {
    use std::error::Error;

    const MEBIBYTES_SHORT: &[char] = &['m'];

    const BYTES: u64 = 1;

    const KIBIBYTES: u64 = BYTES * 1024;

    const MEBIBYTES: u64 = KIBIBYTES * 1024;

    fn baz() -> Result<u64, Box<Error>> {
        let u = [].as_slice();
        let unit = match u {
            MEBIBYTES_SHORT => MEBIBYTES,
        };

        Ok(1)
    }
}

fn main() {}
Zoxc commented 7 months ago

I'm unable to reproduce it with rustc 1.77.0-nightly (d5fd09972 2024-01-22).