Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ice: mir_const_qualif: MIR had errors #121103

Closed
matthiaskrgr opened this issue Feb 14, 2024 · 4 comments · Fixed by #121116
Closed

ice: mir_const_qualif: MIR had errors #121103

matthiaskrgr opened this issue Feb 14, 2024 · 4 comments · Fixed by #121116
Assignees
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

//!

//!

//!

//! }
//! ```

use std::mem;

macro_rules! define_reify_functions {
    ($(
        fn $name:ident $(<$($param:ident),*>)?
            for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
    )+) => {
        $(pub const fn $name<
            $($($param,)*)?
            F: Fn($($arg_ty),*) -> $ret_ty + Copy
        >(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {


            assert!(mem::size_of::<F>() == 0, );

            $(extern $abi)? fn wrapper<
                $($($param,)*)?
                F: Fn($($arg_ty),*) -> $ret_ty + Copy
            >($($arg: $arg_ty),*) -> $ret_ty {
                let f = unsafe {


                    mem::MaybeUninit::<F>::uninit().assume_init()
                };
                f($($arg),*)
            }
            let _f_proof = f;
            wrapper::<
                $($($param,)*)?
                F
            >
        })+
    }
}

define_reify_functions! {
    fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;






    fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
}
original code

original:

//! Abstraction for creating `fn` pointers from any callable that *effectively*
//! has the equivalent of implementing `Default`, even if the compiler neither
//! provides `Default` nor allows reifying closures (i.e. creating `fn` pointers)
//! other than those with absolutely no captures.
//!
//! More specifically, for a closure-like type to be "effectively `Default`":
//! * it must be a ZST (zero-sized type): no information contained within, so
//!   that `Default`'s return value (if it were implemented) is unambiguous
//! * it must be `Copy`: no captured "unique ZST tokens" or any other similar
//!   types that would make duplicating values at will unsound
//!   * combined with the ZST requirement, this confers a kind of "telecopy"
//!     ability: similar to `Copy`, but without keeping the value around, and
//!     instead "reconstructing" it (a noop given it's a ZST) when needed
//! * it must be *provably* inhabited: no captured uninhabited types or any
//!   other types that cannot be constructed by the user of this abstraction
//!   * the proof is a value of the closure-like type itself, in a sense the
//!     "seed" for the "telecopy" process made possible by ZST + `Copy`
//!   * this requirement is the only reason an abstraction limited to a specific
//!     usecase is required: ZST + `Copy` can be checked with *at worst* a panic
//!     at the "attempted `::default()` call" time, but that doesn't guarantee
//!     that the value can be soundly created, and attempting to use the typical
//!     "proof ZST token" approach leads yet again to having a ZST + `Copy` type
//!     that is not proof of anything without a value (i.e. isomorphic to a
//!     newtype of the type it's trying to prove the inhabitation of)
//!
//! A more flexible (and safer) solution to the general problem could exist once
//! `const`-generic parameters can have type parameters in their types:
//!
//! ```rust,ignore (needs future const-generics)
//! extern "C" fn ffi_wrapper<
//!     A, R,
//!     F: Fn(A) -> R,
//!     const f: F, // <-- this `const`-generic is not yet allowed
//! >(arg: A) -> R {
//!     f(arg)
//! }
//! ```

use std::mem;

// FIXME(eddyb) this could be `trait` impls except for the `const fn` requirement.
macro_rules! define_reify_functions {
    ($(
        fn $name:ident $(<$($param:ident),*>)?
            for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
    )+) => {
        $(pub const fn $name<
            $($($param,)*)?
            F: Fn($($arg_ty),*) -> $ret_ty + Copy
        >(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
            // FIXME(eddyb) describe the `F` type (e.g. via `type_name::<F>`) once panic
            // formatting becomes possible in `const fn`.
            assert!(mem::size_of::<F>() == 0, "selfless_reify: closure must be zero-sized");

            $(extern $abi)? fn wrapper<
                $($($param,)*)?
                F: Fn($($arg_ty),*) -> $ret_ty + Copy
            >($($arg: $arg_ty),*) -> $ret_ty {
                let f = unsafe {
                    // SAFETY: `F` satisfies all criteria for "out of thin air"
                    // reconstructability (see module-level doc comment).
                    mem::MaybeUninit::<F>::uninit().assume_init()
                };
                f($($arg),*)
            }
            let _f_proof = f;
            wrapper::<
                $($($param,)*)?
                F
            >
        })+
    }
}

define_reify_functions! {
    fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;

    // HACK(eddyb) this abstraction is used with `for<'a> fn(BridgeConfig<'a>)
    // -> T` but that doesn't work with just `reify_to_extern_c_fn_unary`
    // because of the `fn` pointer type being "higher-ranked" (i.e. the
    // `for<'a>` binder).
    // FIXME(eddyb) try to remove the lifetime from `BridgeConfig`, that'd help.
    fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
}

Version information

rustc 1.78.0-nightly (340bb19fe 2024-02-14)
binary: rustc
commit-hash: 340bb19fea20fd5f9357bbfac542fad84fc7ea2b
commit-date: 2024-02-14
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc --crate-type=lib

Program output

thread 'rustc' panicked at compiler/rustc_mir_transform/src/lib.rs:268:9:
mir_const_qualif: MIR had errors
stack backtrace:
   0:     0x7f406118ce46 - std::backtrace_rs::backtrace::libunwind::trace::h741b35e6ebf0ea82
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f406118ce46 - std::backtrace_rs::backtrace::trace_unsynchronized::hf3b09a36cbdb818e
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f406118ce46 - std::sys_common::backtrace::_print_fmt::hd9ddcb56e855ae1f
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f406118ce46 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1d659f3f1b376713
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f40611dd46c - core::fmt::rt::Argument::fmt::h268aa39b6fb00372
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/core/src/fmt/rt.rs:142:9
   5:     0x7f40611dd46c - core::fmt::write::h3ca4c8a121ee4a21
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f406118087f - std::io::Write::write_fmt::hbae5643614f30ef5
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/io/mod.rs:1854:15
   7:     0x7f406118cbf4 - std::sys_common::backtrace::_print::h46251963e880a6db
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f406118cbf4 - std::sys_common::backtrace::print::hf7d8eafd5758e700
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f406118f93b - std::panicking::default_hook::{{closure}}::h762f7d01310422ff
  10:     0x7f406118f689 - std::panicking::default_hook::h6bf045830ee7f539
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/panicking.rs:292:9
  11:     0x7f4063ff2bac - std[3c399c9137e7035e]::panicking::update_hook::<alloc[b5118abce3d9b84a]::boxed::Box<rustc_driver_impl[aed52aad4ff697f9]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f40611900a0 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h183d4d6c4a9f8452
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/alloc/src/boxed.rs:2030:9
  13:     0x7f40611900a0 - std::panicking::rust_panic_with_hook::hf47919d70d1ff480
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/panicking.rs:785:13
  14:     0x7f406118fda9 - std::panicking::begin_panic_handler::{{closure}}::h5fbc7433175e9186
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/panicking.rs:651:13
  15:     0x7f406118d326 - std::sys_common::backtrace::__rust_end_short_backtrace::h2b0c01029bb6a245
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f406118fb14 - rust_begin_unwind
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/panicking.rs:647:5
  17:     0x7f40611d9975 - core::panicking::panic_fmt::h213e6f8381440dd9
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/core/src/panicking.rs:72:14
  18:     0x7f4065528ff1 - rustc_mir_transform[a16df670bd198a8c]::mir_const_qualif
  19:     0x7f4065528d21 - rustc_query_impl[2b06c656d5e1ef18]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_const_qualif::dynamic_query::{closure#2}::{closure#0}, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 4usize]>>
  20:     0x7f406552804b - rustc_query_system[27a374b315d29eb0]::query::plumbing::try_execute_query::<rustc_query_impl[2b06c656d5e1ef18]::DynamicConfig<rustc_query_system[27a374b315d29eb0]::query::caches::DefIdCache<rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 4usize]>>, false, false, false>, rustc_query_impl[2b06c656d5e1ef18]::plumbing::QueryCtxt, false>
  21:     0x7f4065527dc9 - rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_const_qualif::get_query_non_incr::__rust_end_short_backtrace
  22:     0x7f4065527bfe - rustc_middle[70484f9a8b697519]::query::plumbing::query_get_at::<rustc_query_system[27a374b315d29eb0]::query::caches::DefIdCache<rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 4usize]>>>
  23:     0x7f406358f097 - rustc_mir_transform[a16df670bd198a8c]::mir_promoted
  24:     0x7f40657fb812 - rustc_query_impl[2b06c656d5e1ef18]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_promoted::dynamic_query::{closure#2}::{closure#0}, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 16usize]>>
  25:     0x7f40657fbabb - rustc_query_system[27a374b315d29eb0]::query::plumbing::try_execute_query::<rustc_query_impl[2b06c656d5e1ef18]::DynamicConfig<rustc_query_system[27a374b315d29eb0]::query::caches::VecCache<rustc_span[193b94924c128396]::def_id::LocalDefId, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[2b06c656d5e1ef18]::plumbing::QueryCtxt, false>
  26:     0x7f40660a745d - rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_promoted::get_query_non_incr::__rust_end_short_backtrace
  27:     0x7f40660a756e - rustc_borrowck[90372d0f1f178afe]::mir_borrowck
  28:     0x7f40660a74a1 - rustc_query_impl[2b06c656d5e1ef18]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 8usize]>>
  29:     0x7f40653858f0 - rustc_query_system[27a374b315d29eb0]::query::plumbing::try_execute_query::<rustc_query_impl[2b06c656d5e1ef18]::DynamicConfig<rustc_query_system[27a374b315d29eb0]::query::caches::VecCache<rustc_span[193b94924c128396]::def_id::LocalDefId, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[2b06c656d5e1ef18]::plumbing::QueryCtxt, false>
  30:     0x7f406538540c - rustc_query_impl[2b06c656d5e1ef18]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7f4065c136f0 - rustc_interface[85a46e65fd36dd64]::passes::analysis
  32:     0x7f4065c131e9 - rustc_query_impl[2b06c656d5e1ef18]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2b06c656d5e1ef18]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 1usize]>>
  33:     0x7f40661b0025 - rustc_query_system[27a374b315d29eb0]::query::plumbing::try_execute_query::<rustc_query_impl[2b06c656d5e1ef18]::DynamicConfig<rustc_query_system[27a374b315d29eb0]::query::caches::SingleCache<rustc_middle[70484f9a8b697519]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[2b06c656d5e1ef18]::plumbing::QueryCtxt, false>
  34:     0x7f40661afd89 - rustc_query_impl[2b06c656d5e1ef18]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7f406602e055 - rustc_interface[85a46e65fd36dd64]::interface::run_compiler::<core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>, rustc_driver_impl[aed52aad4ff697f9]::run_compiler::{closure#0}>::{closure#0}
  36:     0x7f40661c7b58 - std[3c399c9137e7035e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[85a46e65fd36dd64]::util::run_in_thread_with_globals<rustc_interface[85a46e65fd36dd64]::util::run_in_thread_pool_with_globals<rustc_interface[85a46e65fd36dd64]::interface::run_compiler<core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>, rustc_driver_impl[aed52aad4ff697f9]::run_compiler::{closure#0}>::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>
  37:     0x7f40661c7984 - <<std[3c399c9137e7035e]::thread::Builder>::spawn_unchecked_<rustc_interface[85a46e65fd36dd64]::util::run_in_thread_with_globals<rustc_interface[85a46e65fd36dd64]::util::run_in_thread_pool_with_globals<rustc_interface[85a46e65fd36dd64]::interface::run_compiler<core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>, rustc_driver_impl[aed52aad4ff697f9]::run_compiler::{closure#0}>::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7e3aa1eb1ad3b865]::result::Result<(), rustc_span[193b94924c128396]::ErrorGuaranteed>>::{closure#1} as core[7e3aa1eb1ad3b865]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  38:     0x7f40611991e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1516af221a452bc1
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/alloc/src/boxed.rs:2016:9
  39:     0x7f40611991e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hff2a9d52ad89d581
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/alloc/src/boxed.rs:2016:9
  40:     0x7f40611991e5 - std::sys::pal::unix::thread::Thread::new::thread_start::hf359248e8c5b2b25
                               at /rustc/340bb19fea20fd5f9357bbfac542fad84fc7ea2b/library/std/src/sys/pal/unix/thread.rs:108:17
  41:     0x7f4060f4a9eb - <unknown>
  42:     0x7f4060fce7cc - <unknown>
  43:                0x0 - <unknown>

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.78.0-nightly (340bb19fe 2024-02-14) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -Z dump-mir-dir=dir

query stack during panic:
#0 [mir_const_qualif] const checking `reify_to_extern_c_fn_hrt_bridge`
#1 [mir_promoted] promoting constants in MIR for `reify_to_extern_c_fn_hrt_bridge`
#2 [mir_borrowck] borrow-checking `reify_to_extern_c_fn_hrt_bridge`
#3 [analysis] running analysis passes on this crate
end of query stack
error[E0433]: failed to resolve: there are too many leading `super` keywords
  --> /tmp/icemaker_global_tempdir.PRGFGEJ4g674/rustc_testrunner_tmpdir_reporting.rq3zBfWzREvH/mvce.rs:53:69
   |
53 |     fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
   |                                                                     ^^^^^ there are too many leading `super` keywords

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0433`.

@matthiaskrgr matthiaskrgr added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Feb 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 14, 2024
@matthiaskrgr
Copy link
Member Author

Test is from library/proc_macro/src/bridge/selfless_reify.rs originally

cleaned up a bit

use std::mem;

macro_rules! define_reify_functions {
    ($(
        fn $name:ident $(<$($param:ident),*>)?
            for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
    )+) => {
        $(pub const fn $name<
            $($($param,)*)?
            F: Fn($($arg_ty),*) -> $ret_ty + Copy
        >(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {


            assert!(mem::size_of::<F>() == 0, );

            $(extern $abi)? fn wrapper<
                $($($param,)*)?
                F: Fn($($arg_ty),*) -> $ret_ty + Copy
            >($($arg: $arg_ty),*) -> $ret_ty {
                let f = unsafe {


                    mem::MaybeUninit::<F>::uninit().assume_init()
                };
                f($($arg),*)
            }
            let _f_proof = f;
            wrapper::<
                $($($param,)*)?
                F
            >
        })+
    }
}

define_reify_functions! {
    fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;

    fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
}

@matthiaskrgr
Copy link
Member Author

Regression in #121071 cc @nnethercote

@matthiaskrgr
Copy link
Member Author

Another smaller example :3

fn main(_: <lib2::GenericType<42> as lib2::TypeFn>::Output) {}

@nnethercote nnethercote self-assigned this Feb 14, 2024
@jieyouxu
Copy link
Contributor

jieyouxu commented Feb 14, 2024

@rustbot label +A-mir -needs-triage

@rustbot rustbot added A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 14, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 15, 2024
…li-obk

Reinstate some delayed bugs.

These were changed to `has_errors` assertions in rust-lang#121071 because that seemed reasonable, but evidently not.

Fixes rust-lang#121103.
Fixes rust-lang#121108.
@bors bors closed this as completed in 64a9c9c Feb 15, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 15, 2024
Rollup merge of rust-lang#121116 - nnethercote:fix-121103-121108, r=oli-obk

Reinstate some delayed bugs.

These were changed to `has_errors` assertions in rust-lang#121071 because that seemed reasonable, but evidently not.

Fixes rust-lang#121103.
Fixes rust-lang#121108.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants