rust-lang / rust

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

ICE: const eval: `ImmTy { .. } input to a fat-to-thin cast´ #130425

Open matthiaskrgr opened 1 week ago

matthiaskrgr commented 1 week ago

auto-reduced (treereduce-rust):

struct S<T>(T)
where
    [T; (
        |_: u8| {
            static FOO: Sync = AtomicUsize::new(0);
            unsafe { &*(&FOO as *const _ as *const usize) }
        },
        1,
    )
        .1]: Copy;

original:

struct S<T>(T) where [T; (|_: u8|{
    static FOO: Sync = AtomicUsize::new(0);
    unsafe { &*(&FOO as *const _ as *const usize) }
}, 1).1]: Copy;
fn main() {}

Version information

rustc 1.83.0-nightly (39b766934 2024-09-16)
binary: rustc
commit-hash: 39b7669347b02f25a36da610822fb3c1e03bac6c
commit-date: 2024-09-16
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zpolymorphize=on

Program output

``` error[E0433]: failed to resolve: use of undeclared type `AtomicUsize` --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:32 | 5 | static FOO: Sync = AtomicUsize::new(0); | ^^^^^^^^^^^ use of undeclared type `AtomicUsize` | help: consider importing this struct | 1 + use std::sync::atomic::AtomicUsize; | error[E0601]: `main` function not found in crate `mvce` --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:10:19 | 10 | .1]: Copy; | ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs` warning: trait objects without an explicit `dyn` are deprecated --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:25 | 5 | static FOO: Sync = AtomicUsize::new(0); | ^^^^ | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default help: if this is an object-safe trait, use `dyn` | 5 | static FOO: dyn Sync = AtomicUsize::new(0); | +++ error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:25 | 5 | static FOO: Sync = AtomicUsize::new(0); | ^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `(dyn Sync + 'static)` error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:32 | 5 | static FOO: Sync = AtomicUsize::new(0); | ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `(dyn Sync + 'static)` = note: constant expressions must have a statically known size error: internal compiler error: /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize) thread 'rustc' panicked at /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42: Box stack backtrace: 0: 0x7cdf71066b5a - ::fmt::h688d92dd3e7bfae7 1: 0x7cdf71803297 - core::fmt::write::h8095b5fc1f2aba3f 2: 0x7cdf7270c073 - std::io::Write::write_fmt::hdc66ed1666cc8932 3: 0x7cdf710669b2 - std::sys::backtrace::BacktraceLock::print::h7b49d08d4dea3fff 4: 0x7cdf71069131 - std::panicking::default_hook::{{closure}}::hc0a5f9ff76fea135 5: 0x7cdf71068f64 - std::panicking::default_hook::hf28f41a3f75f65f5 6: 0x7cdf7016b81f - std[5fe423659e5b23cd]::panicking::update_hook::>::{closure#0} 7: 0x7cdf71069858 - std::panicking::rust_panic_with_hook::h868a1036df76a3e6 8: 0x7cdf701a5001 - std[5fe423659e5b23cd]::panicking::begin_panic::::{closure#0} 9: 0x7cdf70198676 - std[5fe423659e5b23cd]::sys::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 10: 0x7cdf70193bb9 - std[5fe423659e5b23cd]::panicking::begin_panic:: 11: 0x7cdf701ae181 - ::emit_producing_guarantee 12: 0x7cdf708bdb0d - ::span_bug:: 13: 0x7cdf708f15f8 - rustc_middle[552e10f1b06d458a]::util::bug::opt_span_bug_fmt::::{closure#0} 14: 0x7cdf708f1e7a - rustc_middle[552e10f1b06d458a]::ty::context::tls::with_opt::::{closure#0}, !>::{closure#0} 15: 0x7cdf708ddbeb - rustc_middle[552e10f1b06d458a]::ty::context::tls::with_context_opt::::{closure#0}, !>::{closure#0}, !> 16: 0x7cdf708d4a17 - rustc_middle[552e10f1b06d458a]::util::bug::span_bug_fmt:: 17: 0x7cdf72244b0d - ::insert 18: 0x7cdf72238dda - ::simplify_rvalue 19: 0x7cdf6ee4ae16 - ::run_pass 20: 0x7cdf7180d84d - rustc_mir_transform[e930eb8a847a145b]::pass_manager::run_passes_inner 21: 0x7cdf721c9222 - rustc_mir_transform[e930eb8a847a145b]::optimized_mir 22: 0x7cdf721c7ae1 - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::> 23: 0x7cdf7182f56e - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false> 24: 0x7cdf7182eb1f - rustc_query_impl[eb7750c59ad6b12a]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace 25: 0x7cdf721bebe7 - rustc_middle[552e10f1b06d458a]::query::plumbing::query_get_at::>> 26: 0x7cdf71cb78cf - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::> 27: 0x7cdf71cb70af - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false> 28: 0x7cdf71cb6d90 - rustc_query_impl[eb7750c59ad6b12a]::query_impl::unused_generic_params::get_query_non_incr::__rust_end_short_backtrace 29: 0x7cdf71cbb414 - rustc_interface[30a05c7cffabd25a]::passes::run_required_analyses 30: 0x7cdf7251c49e - rustc_interface[30a05c7cffabd25a]::passes::analysis 31: 0x7cdf7251c471 - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::> 32: 0x7cdf726b9e6e - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false> 33: 0x7cdf726b9bcf - rustc_query_impl[eb7750c59ad6b12a]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 34: 0x7cdf7250697c - rustc_interface[30a05c7cffabd25a]::interface::run_compiler::, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1} 35: 0x7cdf725c7f90 - std[5fe423659e5b23cd]::sys::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>> 36: 0x7cdf725c85fa - <::spawn_unchecked_, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#1} as core[62617e15cf8aa4b2]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 37: 0x7cdf725c89eb - std::sys::pal::unix::thread::Thread::new::thread_start::h043ff58bc28d939d 38: 0x7cdf73ce239d - 39: 0x7cdf73d6749c - 40: 0x0 - 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: rustc 1.83.0-nightly (39b766934 2024-09-16) running on x86_64-unknown-linux-gnu note: compiler flags: -Z mir-opt-level=5 -Z polymorphize=on -Z dump-mir-dir=dir query stack during panic: #0 [optimized_mir] optimizing MIR for `S::{constant#0}::{closure#0}` #1 [unused_generic_params] determining which generic parameters are unused by `S::{constant#0}::{closure#0}` end of query stack error: aborting due to 5 previous errors; 1 warning emitted Some errors have detailed explanations: E0277, E0433, E0601. For more information about an error, try `rustc --explain E0277`. ```

matthiaskrgr commented 4 days ago
fn foo() {
    let bar = || {
        static FOO: Sync = AtomicUsize::new(0);

        unsafe { &*(&FOO as *const _ as *const usize) }
    };
    bar();
}