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: should be sized but is not? #122581

Closed
matthiaskrgr opened this issue Mar 16, 2024 · 3 comments · Fixed by #122724
Closed

ICE: should be sized but is not? #122581

matthiaskrgr opened this issue Mar 16, 2024 · 3 comments · Fixed by #122724
Labels
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):

pub union BagOfBits<T: Copy> {
    uninit: (),
    write: Copy,
}

pub fn check_bag<T: Copy>(bag: &BagOfBits<T>) {
    let val = unsafe { (bag as *const _ as *const u8).read() };
}

original:

// run-pass

#![feature(const_ptr_write)]
#![feature(const_mut_refs)]

// Or, equivalently: `MaybeUninit`.
pub union BagOfBits<T: Copy> {
    uninit: (),
    write: Copy,
}

pub const fn make_1u8_bag<T: Copy>() -> BagOfBits<T> {
    assert!(core::mem::size_of::<T>() >= 1);
    let mut bag = BagOfBits { uninit: () };
    unsafe { (&mut bag as *mut _ as *mut u8).write(1); };
    bag
}

pub fn check_bag<T: Copy>(bag: &BagOfBits<T>) {
    let val = unsafe { (bag as *const _ as *const u8).read() };
    assert_eq!(val, 1);
}

fn main() {
    check_bag(&make_1u8_bag::<[usize; 1]>(1)); // Fine
    check_bag(&make_1u8_bag::<usize>()); // Fine

    const CONST_ARRAY_BAG: BagOfBits<[usize; 1]> = make_1u8_bag();
    check_bag(&CONST_ARRAY_BAG); // Fine
    const CONST_USIZE_BAG: BagOfBits<usize> = make_1u8_bag();

    // Used to panic since CTFE would make the entire `BagOfBits<usize>` uninit
    check_bag(&CONST_USIZE_BAG);
}

Version information

rustc 1.78.0-nightly (c03ea3dfd 2024-03-16)
binary: rustc
commit-hash: c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7
commit-date: 2024-03-16
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

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

Program output

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

warning: trait objects without an explicit `dyn` are deprecated
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:3:12
  |
3 |     write: Copy,
  |            ^^^^
  |
  = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
  = note: `#[warn(bare_trait_objects)]` on by default
help: if this is an object-safe trait, use `dyn`
  |
3 |     write: dyn Copy,
  |            +++

error[E0038]: the trait `Copy` cannot be made into an object
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:3:12
  |
3 |     write: Copy,
  |            ^^^^ `Copy` cannot be made into an object
  |
  = note: the trait cannot be made into an object because it requires `Self: Sized`
  = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>

error[E0277]: the size for values of type `(dyn Copy + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:3:12
  |
3 |     write: Copy,
  |            ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Copy + 'static)`
  = note: no field of a union may have a dynamically sized type
  = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
  |
3 |     write: &Copy,
  |            +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
3 |     write: Box<Copy>,
  |            ++++    +

error[E0392]: type parameter `T` is never used
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:1:21
  |
1 | pub union BagOfBits<T: Copy> {
  |                     ^ unused type parameter
  |
  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:3:5
  |
3 |     write: Copy,
  |     ^^^^^^^^^^^
  |
  = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: wrap the field type in `ManuallyDrop<...>`
  |
3 |     write: std::mem::ManuallyDrop<Copy>,
  |            +++++++++++++++++++++++    +

error: internal compiler error: `BagOfBits<T/#0>` should be sized but is not?
 --> /tmp/icemaker_global_tempdir.rFVXcYalVTX7/rustc_testrunner_tmpdir_reporting.LvGGT1V3WKvx/mvce.rs:7:24
  |
7 |     let val = unsafe { (bag as *const _ as *const u8).read() };
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/cast.rs:142:28:
Box<dyn Any>
stack backtrace:
   0:     0x7f9ddfb8c912 - std::backtrace_rs::backtrace::libunwind::trace::h720e7f0c15df28db
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f9ddfb8c912 - std::backtrace_rs::backtrace::trace_unsynchronized::ha9779099a18695a3
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f9ddfb8c912 - std::sys_common::backtrace::_print_fmt::h639c233731a9e3cb
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f9ddfb8c912 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h63ddc66545b2f581
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f9ddfbddc8c - core::fmt::rt::Argument::fmt::h06c657b2e59a0031
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/core/src/fmt/rt.rs:142:9
   5:     0x7f9ddfbddc8c - core::fmt::write::h231dab7207b03bd2
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/core/src/fmt/mod.rs:1153:17
   6:     0x7f9ddfb8180f - std::io::Write::write_fmt::h2228d6605854434b
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/io/mod.rs:1843:15
   7:     0x7f9ddfb8c6e4 - std::sys_common::backtrace::_print::hac0689b492be0682
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f9ddfb8c6e4 - std::sys_common::backtrace::print::he630bda5375f1365
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f9ddfb8f3eb - std::panicking::default_hook::{{closure}}::h9d04fb7bce2d5305
  10:     0x7f9ddfb8f143 - std::panicking::default_hook::h142cecc409c8d640
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/panicking.rs:292:9
  11:     0x7f9de2bb4a9e - std[6ebe52f946dd541]::panicking::update_hook::<alloc[54004d19b3858571]::boxed::Box<rustc_driver_impl[f7a8b498200e65bb]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f9ddfb8fb50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h4d1c095d3d6e9872
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/alloc/src/boxed.rs:2034:9
  13:     0x7f9ddfb8fb50 - std::panicking::rust_panic_with_hook::h69a135916b9adbd2
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/panicking.rs:783:13
  14:     0x7f9de2be0f34 - std[6ebe52f946dd541]::panicking::begin_panic::<rustc_errors[5de0d917113e7fb6]::ExplicitBug>::{closure#0}
  15:     0x7f9de2bddb86 - std[6ebe52f946dd541]::sys_common::backtrace::__rust_end_short_backtrace::<std[6ebe52f946dd541]::panicking::begin_panic<rustc_errors[5de0d917113e7fb6]::ExplicitBug>::{closure#0}, !>
  16:     0x7f9de2bd9096 - std[6ebe52f946dd541]::panicking::begin_panic::<rustc_errors[5de0d917113e7fb6]::ExplicitBug>
  17:     0x7f9de2bea3b1 - <rustc_errors[5de0d917113e7fb6]::diagnostic::BugAbort as rustc_errors[5de0d917113e7fb6]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7f9de2d0f2c8 - <rustc_errors[5de0d917113e7fb6]::DiagCtxt>::span_bug::<rustc_span[6e2b61d9e5bcc941]::span_encoding::Span, alloc[54004d19b3858571]::string::String>
  19:     0x7f9de514996e - <rustc_hir_typeck[6fac15a89779e2b3]::fn_ctxt::FnCtxt>::pointer_kind.cold.0
  20:     0x7f9de1a7bebe - <rustc_hir_typeck[6fac15a89779e2b3]::cast::CastCheck>::do_check
  21:     0x7f9de48e7845 - <rustc_hir_typeck[6fac15a89779e2b3]::cast::CastCheck>::check
  22:     0x7f9de40b0c1e - rustc_hir_typeck[6fac15a89779e2b3]::typeck
  23:     0x7f9de40ae05f - rustc_query_impl[d63bb6c0f452187f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d63bb6c0f452187f]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ea608354392e650]::query::erase::Erased<[u8; 8usize]>>
  24:     0x7f9de40ac52e - rustc_query_system[2114d95bfb45a679]::query::plumbing::try_execute_query::<rustc_query_impl[d63bb6c0f452187f]::DynamicConfig<rustc_query_system[2114d95bfb45a679]::query::caches::VecCache<rustc_span[6e2b61d9e5bcc941]::def_id::LocalDefId, rustc_middle[ea608354392e650]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[d63bb6c0f452187f]::plumbing::QueryCtxt, false>
  25:     0x7f9de40abf8c - rustc_query_impl[d63bb6c0f452187f]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  26:     0x7f9de43ca5d3 - <rustc_middle[ea608354392e650]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[f2100830a8b528cb]::check_crate::{closure#4}>::{closure#0}
  27:     0x7f9de43c926c - rustc_hir_analysis[f2100830a8b528cb]::check_crate
  28:     0x7f9de43a452a - rustc_interface[6fcfc136e89dc42e]::passes::analysis
  29:     0x7f9de43a4165 - rustc_query_impl[d63bb6c0f452187f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d63bb6c0f452187f]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ea608354392e650]::query::erase::Erased<[u8; 1usize]>>
  30:     0x7f9de4c088a5 - rustc_query_system[2114d95bfb45a679]::query::plumbing::try_execute_query::<rustc_query_impl[d63bb6c0f452187f]::DynamicConfig<rustc_query_system[2114d95bfb45a679]::query::caches::SingleCache<rustc_middle[ea608354392e650]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[d63bb6c0f452187f]::plumbing::QueryCtxt, false>
  31:     0x7f9de4c08609 - rustc_query_impl[d63bb6c0f452187f]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  32:     0x7f9de4963284 - rustc_interface[6fcfc136e89dc42e]::interface::run_compiler::<core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>, rustc_driver_impl[f7a8b498200e65bb]::run_compiler::{closure#0}>::{closure#0}
  33:     0x7f9de4e54385 - std[6ebe52f946dd541]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[6fcfc136e89dc42e]::util::run_in_thread_with_globals<rustc_interface[6fcfc136e89dc42e]::util::run_in_thread_pool_with_globals<rustc_interface[6fcfc136e89dc42e]::interface::run_compiler<core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>, rustc_driver_impl[f7a8b498200e65bb]::run_compiler::{closure#0}>::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>
  34:     0x7f9de4e541b2 - <<std[6ebe52f946dd541]::thread::Builder>::spawn_unchecked_<rustc_interface[6fcfc136e89dc42e]::util::run_in_thread_with_globals<rustc_interface[6fcfc136e89dc42e]::util::run_in_thread_pool_with_globals<rustc_interface[6fcfc136e89dc42e]::interface::run_compiler<core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>, rustc_driver_impl[f7a8b498200e65bb]::run_compiler::{closure#0}>::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7931fcdfa73bbef6]::result::Result<(), rustc_span[6e2b61d9e5bcc941]::ErrorGuaranteed>>::{closure#1} as core[7931fcdfa73bbef6]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:     0x7f9ddfb991e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2cd6598022142910
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/alloc/src/boxed.rs:2020:9
  36:     0x7f9ddfb991e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hfc8920891f2f584d
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/alloc/src/boxed.rs:2020:9
  37:     0x7f9ddfb991e5 - std::sys::pal::unix::thread::Thread::new::thread_start::h31aea03b1d783db7
                               at /rustc/c03ea3dfd907e7dc6305ebf20c94f3a1f1d9fed7/library/std/src/sys/pal/unix/thread.rs:108:17
  38:     0x7f9ddf98155a - <unknown>
  39:     0x7f9ddf9fea3c - <unknown>
  40:                0x0 - <unknown>

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 (c03ea3dfd 2024-03-16) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [typeck] type-checking `check_bag`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 6 previous errors; 1 warning emitted

Some errors have detailed explanations: E0038, E0277, E0392, E0601, E0740.
For more information about an error, try `rustc --explain E0038`.

@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 Mar 16, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 16, 2024
@matthiaskrgr
Copy link
Member Author

#121208 cc @nnethercote

@lukas-code
Copy link
Contributor

This is incidentally fixed by #122493 by making unions always sized.

@lukas-code
Copy link
Contributor

lukas-code commented Mar 16, 2024

smaller:

union Union {
    val: [u8],
}

fn cast(ptr: *const ()) -> *const Union {
    ptr as _
}

The union is currently in fact not sized, but this code assumes that non-struct ADTs must be sized:

Ok(match *t.kind() {
ty::Slice(_) | ty::Str => Some(PointerKind::Length),
ty::Dynamic(tty, _, ty::Dyn) => Some(PointerKind::VTable(tty.principal_def_id())),
ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
None => Some(PointerKind::Thin),
Some(f) => {
let field_ty = self.field_ty(span, f, args);
self.pointer_kind(field_ty, span)?
}
},
ty::Tuple(fields) => match fields.last() {
None => Some(PointerKind::Thin),
Some(&f) => self.pointer_kind(f, span)?,
},
// Pointers to foreign types are thin, despite being unsized
ty::Foreign(..) => Some(PointerKind::Thin),
// We should really try to normalize here.
ty::Alias(_, pi) => Some(PointerKind::OfAlias(pi)),
ty::Param(p) => Some(PointerKind::OfParam(p)),
// Insufficient type information.
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
ty::Bool
| ty::Char
| ty::Int(..)
| ty::Uint(..)
| ty::Float(_)
| ty::Array(..)
| ty::CoroutineWitness(..)
| ty::RawPtr(_)
| ty::Ref(..)
| ty::FnDef(..)
| ty::FnPtr(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::Adt(..)
| ty::Never
| ty::Dynamic(_, _, ty::DynStar)
| ty::Error(_) => {
self.dcx().span_bug(span, format!("`{t:?}` should be sized but is not?"));
}
})

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 19, 2024
…test, r=compiler-errors

add test for casting pointer to union with unsized tail

closes rust-lang#122581
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 19, 2024
Rollup merge of rust-lang#122724 - lukas-code:unsized-union-cast-ice-test, r=compiler-errors

add test for casting pointer to union with unsized tail

closes rust-lang#122581
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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