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: compiler/rustc_const_eval/src/interpret/discriminant.rs assertion failed 0 != 1 #125811

Closed
matthiaskrgr opened this issue May 31, 2024 · 3 comments · Fixed by #126358
Closed
Assignees
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) C-bug Category: This is a bug. F-transmutability `#![feature(transmutability)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 31, 2024

auto-reduced (treereduce-rust):

mod assert {
    use std::mem::{Assume, BikeshedIntrinsicFrom};

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src>,
    {
    }
}

#[repr(C)]
struct Zst;

enum V0 {
    B(!),
}

enum V2 {
    V = 2,
}

enum Lopsided {
    Smol(Zst),
    Lorg(V0),
}

#[repr(C)]
#[repr(C)]
struct Dst(Lopsided, V2);

fn should_pad_variants() {
    assert::is_transmutable::<Src, Dst>();
}

original:

mod assert {
    use std::mem::{Assume, BikeshedIntrinsicFrom};

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src>,
    {
    }
}

#[repr(C)]
struct Zst;

#[repr(u8)]
enum V0 {
    B(!),
}

enum V2 {
    V = 2,
}

enum Lopsided {
    Smol(Zst),
    Lorg(V0),
}

#[repr(C)]
#[repr(C)]
struct Dst(Lopsided, V2);

fn should_pad_variants() {
    assert::is_transmutable::<Src, Dst>();
}

Version information

rustc 1.80.0-nightly (366da30d5 2024-05-31)
binary: rustc
commit-hash: 366da30d554719e7490950aeea5d3f3a5927e313
commit-date: 2024-05-31
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0412]: cannot find type `Src` in this scope
  --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:32:31
   |
32 |     assert::is_transmutable::<Src, Dst>();
   |                               ^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
31 | fn should_pad_variants<Src>() {
   |                       +++++

error[E0658]: the `!` type is experimental
  --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:15:7
   |
15 |     B(!),
   |       ^
   |
   = note: see issue #35121 <https://github.com/rust-lang/rust/issues/35121> for more information
   = help: add `#![feature(never_type)]` to the crate attributes to enable
   = note: this compiler was built on 2024-05-31; consider upgrading it if it is out of date

warning: unused import: `Assume`
 --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:2:20
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                    ^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:33:2
   |
33 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs`

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:2:20
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                    ^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-05-31; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:2:28
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                            ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-05-31; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.LMPz7J6APUgI/rustc_testrunner_tmpdir_reporting.aTyVK8HgNdI3/mvce.rs:6:14
  |
6 |         Dst: BikeshedIntrinsicFrom<Src>,
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-05-31; consider upgrading it if it is out of date

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/discriminant.rs:245:17:
assertion `left == right` failed
  left: 0
 right: 1
stack backtrace:
   0:     0x7463a68cacc5 - std::backtrace_rs::backtrace::libunwind::trace::h55528d66201b21b3
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7463a68cacc5 - std::backtrace_rs::backtrace::trace_unsynchronized::hfeb2c1023d01cc7a
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7463a68cacc5 - std::sys_common::backtrace::_print_fmt::hb117330f69c2368b
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7463a68cacc5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hff545fcc3c187885
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7463a691b6bb - core::fmt::rt::Argument::fmt::h4fbcc3839f9a0a6b
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/core/src/fmt/rt.rs:165:63
   5:     0x7463a691b6bb - core::fmt::write::h5decd52c3971b2af
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/core/src/fmt/mod.rs:1168:21
   6:     0x7463a68bf99f - std::io::Write::write_fmt::heb68647b6f7cb352
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/io/mod.rs:1835:15
   7:     0x7463a68caa9e - std::sys_common::backtrace::_print::he9a1f2ef1a819ce6
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7463a68caa9e - std::sys_common::backtrace::print::heb35be0a4b6f2604
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7463a68cd4d9 - std::panicking::default_hook::{{closure}}::h49a0dd07936dc944
  10:     0x7463a68cd27a - std::panicking::default_hook::h0e4c38f9d2d8989e
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/panicking.rs:298:9
  11:     0x7463a2f2e83f - std[711d1e0fc2f4251c]::panicking::update_hook::<alloc[faffb073d5b42204]::boxed::Box<rustc_driver_impl[9ac8f6a1f13e57e3]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7463a68cdc0b - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9c4d9467e29e4ff0
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/alloc/src/boxed.rs:2077:9
  13:     0x7463a68cdc0b - std::panicking::rust_panic_with_hook::h6bace15d559d4f77
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/panicking.rs:799:13
  14:     0x7463a68cd984 - std::panicking::begin_panic_handler::{{closure}}::h92e6857419a9a52d
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/panicking.rs:664:13
  15:     0x7463a68cb189 - std::sys_common::backtrace::__rust_end_short_backtrace::h7769155b47e63eff
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7463a68cd6b7 - rust_begin_unwind
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/panicking.rs:652:5
  17:     0x7463a6917c53 - core::panicking::panic_fmt::h8522358087565600
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/core/src/panicking.rs:72:14
  18:     0x7463a691817e - core::panicking::assert_failed_inner::ha5757fde6fbecdca
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/core/src/panicking.rs:408:17
  19:     0x7463a2ea55c7 - core[f7f07073f776a82d]::panicking::assert_failed::<rustc_target[5bc042617586e777]::abi::VariantIdx, rustc_target[5bc042617586e777]::abi::VariantIdx>
  20:     0x7463a2eff83b - rustc_const_eval[ff2960df392dfda1]::const_eval::tag_for_variant_provider
  21:     0x7463a37f7fb7 - rustc_query_impl[a6f92fde4fe57a4a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a6f92fde4fe57a4a]::query_impl::tag_for_variant::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 17usize]>>
  22:     0x7463a37cda19 - <rustc_query_impl[a6f92fde4fe57a4a]::query_impl::tag_for_variant::dynamic_query::{closure#2} as core[f7f07073f776a82d]::ops::function::FnOnce<(rustc_middle[1225d84d7bed8722]::ty::context::TyCtxt, (rustc_middle[1225d84d7bed8722]::ty::Ty, rustc_target[5bc042617586e777]::abi::VariantIdx))>>::call_once
  23:     0x7463a375574c - rustc_query_system[37998a691976304d]::query::plumbing::try_execute_query::<rustc_query_impl[a6f92fde4fe57a4a]::DynamicConfig<rustc_query_system[37998a691976304d]::query::caches::DefaultCache<(rustc_middle[1225d84d7bed8722]::ty::Ty, rustc_target[5bc042617586e777]::abi::VariantIdx), rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 17usize]>>, false, false, false>, rustc_query_impl[a6f92fde4fe57a4a]::plumbing::QueryCtxt, false>
  24:     0x7463a3803999 - rustc_query_impl[a6f92fde4fe57a4a]::query_impl::tag_for_variant::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7463a3b597ae - <rustc_transmute[e0ca7b5b45f72b6e]::layout::tree::Tree<rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Def, rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Ref>>::from_enum
  26:     0x7463a3b5a22e - <rustc_transmute[e0ca7b5b45f72b6e]::layout::tree::Tree<rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Def, rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Ref>>::from_variant
  27:     0x7463a3b58ebb - <rustc_transmute[e0ca7b5b45f72b6e]::layout::tree::Tree<rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Def, rustc_transmute[e0ca7b5b45f72b6e]::layout::rustc::Ref>>::from_struct
  28:     0x7463a3b5d2a3 - <rustc_transmute[e0ca7b5b45f72b6e]::rustc::TransmuteTypeEnv>::is_transmutable
  29:     0x7463a4cca758 - <rustc_trait_selection[3420cb8758153cdd]::traits::select::SelectionContext>::confirm_candidate
  30:     0x7463a4802f66 - <rustc_trait_selection[3420cb8758153cdd]::traits::select::SelectionContext>::evaluate_trait_predicate_recursively
  31:     0x7463a47fa881 - <rustc_trait_selection[3420cb8758153cdd]::traits::select::SelectionContext>::evaluate_root_obligation
  32:     0x7463a47f8e53 - rustc_traits[44550ec2fdf392c0]::evaluate_obligation::evaluate_obligation
  33:     0x7463a47f87e9 - rustc_query_impl[a6f92fde4fe57a4a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a6f92fde4fe57a4a]::query_impl::evaluate_obligation::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 2usize]>>
  34:     0x7463a47f7d52 - rustc_query_system[37998a691976304d]::query::plumbing::try_execute_query::<rustc_query_impl[a6f92fde4fe57a4a]::DynamicConfig<rustc_query_system[37998a691976304d]::query::caches::DefaultCache<rustc_type_ir[aeff5eebb819ca37]::canonical::Canonical<rustc_middle[1225d84d7bed8722]::ty::context::TyCtxt, rustc_middle[1225d84d7bed8722]::ty::ParamEnvAnd<rustc_middle[1225d84d7bed8722]::ty::predicate::Predicate>>, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 2usize]>>, false, false, false>, rustc_query_impl[a6f92fde4fe57a4a]::plumbing::QueryCtxt, false>
  35:     0x7463a47f79a4 - rustc_query_impl[a6f92fde4fe57a4a]::query_impl::evaluate_obligation::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7463a0ed8ebf - <rustc_trait_selection[3420cb8758153cdd]::traits::fulfill::FulfillProcessor as rustc_data_structures[a28f11aa20c63872]::obligation_forest::ObligationProcessor>::process_obligation
  37:     0x7463a44060f6 - <rustc_data_structures[a28f11aa20c63872]::obligation_forest::ObligationForest<rustc_trait_selection[3420cb8758153cdd]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[3420cb8758153cdd]::traits::fulfill::FulfillProcessor>
  38:     0x7463a17bb7b9 - <rustc_hir_typeck[88ff56c2c2c4cdca]::fn_ctxt::FnCtxt>::confirm_builtin_call
  39:     0x7463a4df87e5 - <rustc_hir_typeck[88ff56c2c2c4cdca]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  40:     0x7463a4df4073 - <rustc_hir_typeck[88ff56c2c2c4cdca]::fn_ctxt::FnCtxt>::check_block_with_expected
  41:     0x7463a4df91a5 - <rustc_hir_typeck[88ff56c2c2c4cdca]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  42:     0x7463a479538c - rustc_hir_typeck[88ff56c2c2c4cdca]::check::check_fn
  43:     0x7463a478aaac - rustc_hir_typeck[88ff56c2c2c4cdca]::typeck
  44:     0x7463a478a511 - rustc_query_impl[a6f92fde4fe57a4a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a6f92fde4fe57a4a]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 8usize]>>
  45:     0x7463a4a97eae - rustc_query_system[37998a691976304d]::query::plumbing::try_execute_query::<rustc_query_impl[a6f92fde4fe57a4a]::DynamicConfig<rustc_query_system[37998a691976304d]::query::caches::VecCache<rustc_span[8a26b4a25cb297da]::def_id::LocalDefId, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a6f92fde4fe57a4a]::plumbing::QueryCtxt, false>
  46:     0x7463a4a969d5 - rustc_query_impl[a6f92fde4fe57a4a]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  47:     0x7463a4a9660b - <rustc_middle[1225d84d7bed8722]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[2c3a795cad3589b7]::check_crate::{closure#4}>::{closure#0}
  48:     0x7463a4a950c7 - rustc_hir_analysis[2c3a795cad3589b7]::check_crate
  49:     0x7463a4aade7e - rustc_interface[d08288d03eacdbc]::passes::analysis
  50:     0x7463a4aad9db - rustc_query_impl[a6f92fde4fe57a4a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a6f92fde4fe57a4a]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 1usize]>>
  51:     0x7463a519f525 - rustc_query_system[37998a691976304d]::query::plumbing::try_execute_query::<rustc_query_impl[a6f92fde4fe57a4a]::DynamicConfig<rustc_query_system[37998a691976304d]::query::caches::SingleCache<rustc_middle[1225d84d7bed8722]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a6f92fde4fe57a4a]::plumbing::QueryCtxt, false>
  52:     0x7463a519f28f - rustc_query_impl[a6f92fde4fe57a4a]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  53:     0x7463a5004e9c - rustc_interface[d08288d03eacdbc]::interface::run_compiler::<core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>, rustc_driver_impl[9ac8f6a1f13e57e3]::run_compiler::{closure#0}>::{closure#1}
  54:     0x7463a4fb7089 - std[711d1e0fc2f4251c]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[d08288d03eacdbc]::util::run_in_thread_with_globals<rustc_interface[d08288d03eacdbc]::util::run_in_thread_pool_with_globals<rustc_interface[d08288d03eacdbc]::interface::run_compiler<core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>, rustc_driver_impl[9ac8f6a1f13e57e3]::run_compiler::{closure#0}>::{closure#1}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>::{closure#0}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>
  55:     0x7463a4fb6e40 - <<std[711d1e0fc2f4251c]::thread::Builder>::spawn_unchecked_<rustc_interface[d08288d03eacdbc]::util::run_in_thread_with_globals<rustc_interface[d08288d03eacdbc]::util::run_in_thread_pool_with_globals<rustc_interface[d08288d03eacdbc]::interface::run_compiler<core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>, rustc_driver_impl[9ac8f6a1f13e57e3]::run_compiler::{closure#0}>::{closure#1}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>::{closure#0}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f7f07073f776a82d]::result::Result<(), rustc_span[8a26b4a25cb297da]::ErrorGuaranteed>>::{closure#2} as core[f7f07073f776a82d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  56:     0x7463a68d7bfb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h8e95645a49757ace
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/alloc/src/boxed.rs:2063:9
  57:     0x7463a68d7bfb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hff008d758cac814f
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/alloc/src/boxed.rs:2063:9
  58:     0x7463a68d7bfb - std::sys::pal::unix::thread::Thread::new::thread_start::he9481017308a5f43
                               at /rustc/366da30d554719e7490950aeea5d3f3a5927e313/library/std/src/sys/pal/unix/thread.rs:108:17
  59:     0x7463a66741cf - <unknown>
  60:     0x7463a66f56ec - <unknown>
  61:                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: please make sure that you have updated to the latest nightly

note: rustc 1.80.0-nightly (366da30d5 2024-05-31) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [tag_for_variant] computing variant tag for enum
#1 [evaluate_obligation] evaluating trait selection obligation `Dst: core::mem::transmutability::BikeshedIntrinsicFrom<{type error}, core::mem::transmutability::Assume { alignment: false, lifetimes: false, safety: false, validity: false }>`
end of query stack
error: aborting due to 6 previous errors; 1 warning emitted

Some errors have detailed explanations: E0412, E0601, E0658.
For more information about an error, try `rustc --explain E0412`.

@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 May 31, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 31, 2024
@jieyouxu jieyouxu added A-const-eval Area: Constant evaluation (MIR interpretation) S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 4, 2024
@olafes
Copy link
Contributor

olafes commented Jun 6, 2024

fn foo<T: std::mem::BikeshedIntrinsicFrom<Enum>>() {}

enum Enum {
    X,
    Y(!)
}

fn main() {
    foo::<_>();
}

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Jun 9, 2024
@RalfJung
Copy link
Member

That is the transmutability check asking for the discriminant of an uninhabited variant.

I'm not sure what that code is doing with this information, but probably it makes sense to just remove the assertion and treat uninhabited variants like niched variants -- writing their discriminant is a NOP, but then the read_discriminant validation we do will definitely fail as the uninhabited variant cannot actually be represented in-memory.

Cc @jswrenn

@RalfJung RalfJung added the F-transmutability `#![feature(transmutability)]` label Jun 10, 2024
@jswrenn
Copy link
Member

jswrenn commented Jun 10, 2024

@rustbot claim

jswrenn added a commit to jswrenn/rust that referenced this issue Jun 12, 2024
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Jun 13, 2024
safe transmute: support `Single` enums

Previously, the implementation of `Tree::from_enum` incorrectly treated enums with `Variants::Single` and `Variants::Multiple` identically. This is incorrect for `Variants::Single` enums, which delegate their layout to that of a variant with a particular index (or no variant at all if the enum is empty).

This flaw manifested first as an ICE. `Tree::from_enum` attempted to compute the tag of variants other than the one at `Variants::Single`'s `index`, and fell afoul of a sanity-checking assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`. This assertion is non-load-bearing, and can be removed; the routine its in is well-behaved even without it.

With the assertion removed, the proximate issue becomes apparent: calling `Tree::from_variant` on a variant that does not exist is ill-defined. A sanity check the given variant has `FieldShapes::Arbitrary` fails, and the analysis is (correctly) aborted with `Err::NotYetSupported`.

This commit corrects this chain of failures by ensuring that `Tree::from_variant` is not called on variants that are, as far as layout is concerned, nonexistent. Specifically, the implementation of `Tree::from_enum` is now partitioned into three cases:

  1. enums that are uninhabited
  2. enums for which all but one variant is uninhabited
  3. enums with multiple inhabited variants

`Tree::from_variant` is now only invoked in the third case. In the first case, `Tree::uninhabited()` is produced. In the second case, the layout is delegated to `Variants::Single`'s index.

Fixes rust-lang#125811
@bors bors closed this as completed in fb662f2 Jun 13, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 13, 2024
Rollup merge of rust-lang#126358 - jswrenn:fix-125811, r=compiler-errors

safe transmute: support `Single` enums

Previously, the implementation of `Tree::from_enum` incorrectly treated enums with `Variants::Single` and `Variants::Multiple` identically. This is incorrect for `Variants::Single` enums, which delegate their layout to that of a variant with a particular index (or no variant at all if the enum is empty).

This flaw manifested first as an ICE. `Tree::from_enum` attempted to compute the tag of variants other than the one at `Variants::Single`'s `index`, and fell afoul of a sanity-checking assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`. This assertion is non-load-bearing, and can be removed; the routine its in is well-behaved even without it.

With the assertion removed, the proximate issue becomes apparent: calling `Tree::from_variant` on a variant that does not exist is ill-defined. A sanity check the given variant has `FieldShapes::Arbitrary` fails, and the analysis is (correctly) aborted with `Err::NotYetSupported`.

This commit corrects this chain of failures by ensuring that `Tree::from_variant` is not called on variants that are, as far as layout is concerned, nonexistent. Specifically, the implementation of `Tree::from_enum` is now partitioned into three cases:

  1. enums that are uninhabited
  2. enums for which all but one variant is uninhabited
  3. enums with multiple inhabited variants

`Tree::from_variant` is now only invoked in the third case. In the first case, `Tree::uninhabited()` is produced. In the second case, the layout is delegated to `Variants::Single`'s index.

Fixes rust-lang#125811
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) C-bug Category: This is a bug. F-transmutability `#![feature(transmutability)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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.

6 participants