Skip to content

ICE: should be sized but is not? #124848

Closed
@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

use std::cell::Cell;

struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);

fn main() {
    let mut unpinned = MyType(Cell::new(None));
    let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
}

original:

use std::cell::Cell;
use std::marker::PhantomPinned;
use std::pin::Pin;

struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);

impl<'a> Clone for &mut MyType<'_> {
    //~^ ERROR E0751
    fn clone(&self) -> &'a mut MyType<'a> {
        self.0.take().unwrap()
    }
}

fn main() {
    let mut unpinned = MyType(Cell::new(None), PhantomPinned);
    let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
    let mut p = Box::pin(MyType(Cell::_(Some(&mut unpinned)), PhantomPinned));

    // p_mut1 is okay: it does not point to the bad_addr
    let p_mut1: Cell<&mut MyType<'_>> = p.as_mut();
    assert_ne!(bad_addr, &*p_mut1 as *const _ as usize);

    // but p_mut2 does point to bad_addr! this is unsound
    let p_mut2: Pin<&mut Cell<Option<&'a mut MyType<'a>>>> = p_mut1.clone();
    assert_eq!(bad_addr, &*p_mut2 as *const _ as usize);
}

Version information

rustc 1.80.0-nightly (5ae5d1353 2024-05-07)
binary: rustc
commit-hash: 5ae5d135372c4b576edc73c191d2dc86b1d16b5c
commit-date: 2024-05-07
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

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

Program output

error[E0261]: use of undeclared lifetime name `'unpinned`
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:3:32
  |
3 | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
  |               -                ^^^^^^^^^ undeclared lifetime
  |               |
  |               help: consider introducing lifetime `'unpinned` here: `'unpinned,`

error[E0261]: use of undeclared lifetime name `'a`
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:7:53
  |
5 | fn main() {
  |        - help: consider introducing lifetime `'a` here: `<'a>`
6 |     let mut unpinned = MyType(Cell::new(None));
7 |     let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
  |                                                     ^^ undeclared lifetime

error[E0261]: use of undeclared lifetime name `'a`
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:7:67
  |
5 | fn main() {
  |        - help: consider introducing lifetime `'a` here: `<'a>`
6 |     let mut unpinned = MyType(Cell::new(None));
7 |     let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
  |                                                                   ^^ undeclared lifetime

error[E0412]: cannot find type `Pin` in this scope
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:3:60
  |
3 | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
  |                                                            ^^^ not found in this scope
  |
help: consider importing this struct
  |
1 + use std::pin::Pin;
  |

error[E0061]: this struct takes 2 arguments but 1 argument was supplied
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:6:24
  |
6 |     let mut unpinned = MyType(Cell::new(None));
  |                        ^^^^^^----------------- an argument is missing
  |
note: tuple struct defined here
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:3:8
  |
3 | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
  |        ^^^^^^
help: provide the argument
  |
6 |     let mut unpinned = MyType(Cell::new(None), /* value */);
  |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0606]: casting `&MyType<'_>` as `*const Cell<Option<&mut MyType<'_>>>` is invalid
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:7:20
  |
7 |     let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: `std::option::Option<&'?1 mut MyType<'?2>>` should be sized but is not?
 --> /tmp/icemaker_global_tempdir.TzqNyU4s1g5U/rustc_testrunner_tmpdir_reporting.lbK1htfMHgGW/mvce.rs:7:20
  |
7 |     let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/cast.rs:143:28:
Box<dyn Any>
stack backtrace:
   0:     0x75eced407b35 - std::backtrace_rs::backtrace::libunwind::trace::h898f790b668aa360
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x75eced407b35 - std::backtrace_rs::backtrace::trace_unsynchronized::h37e2d3a328252c3c
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x75eced407b35 - std::sys_common::backtrace::_print_fmt::h8001ddad7b617164
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x75eced407b35 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha1863cfb25e180ba
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x75eced456cfb - core::fmt::rt::Argument::fmt::hed259047e582e1e9
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/core/src/fmt/rt.rs:165:63
   5:     0x75eced456cfb - core::fmt::write::hc5409d094221c111
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/core/src/fmt/mod.rs:1157:21
   6:     0x75eced3fc87f - std::io::Write::write_fmt::h186f3d4838dbd4c1
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/io/mod.rs:1835:15
   7:     0x75eced40790e - std::sys_common::backtrace::_print::hdcd4a776fda551b4
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x75eced40790e - std::sys_common::backtrace::print::h32eb678d18f393f0
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x75eced40a279 - std::panicking::default_hook::{{closure}}::h93a0745beb866f63
  10:     0x75eced409fbd - std::panicking::default_hook::h3a7275adeb4af036
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/panicking.rs:298:9
  11:     0x75ece9cb898f - std[5264994d61690aef]::panicking::update_hook::<alloc[f2c8b6b7fb69080e]::boxed::Box<rustc_driver_impl[1a38415264a51164]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x75eced40a976 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h2e6bab372b2f3333
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2036:9
  13:     0x75eced40a976 - std::panicking::rust_panic_with_hook::h6f02a76ae371ed1e
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/panicking.rs:799:13
  14:     0x75ece9ce7f64 - std[5264994d61690aef]::panicking::begin_panic::<rustc_errors[4530deb080854ecc]::ExplicitBug>::{closure#0}
  15:     0x75ece9ce4ac6 - std[5264994d61690aef]::sys_common::backtrace::__rust_end_short_backtrace::<std[5264994d61690aef]::panicking::begin_panic<rustc_errors[4530deb080854ecc]::ExplicitBug>::{closure#0}, !>
  16:     0x75ece9ce0076 - std[5264994d61690aef]::panicking::begin_panic::<rustc_errors[4530deb080854ecc]::ExplicitBug>
  17:     0x75ece9cf12b1 - <rustc_errors[4530deb080854ecc]::diagnostic::BugAbort as rustc_errors[4530deb080854ecc]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x75ece9e3b5b8 - <rustc_errors[4530deb080854ecc]::DiagCtxt>::span_bug::<rustc_span[ec3d05ef96c4664f]::span_encoding::Span, alloc[f2c8b6b7fb69080e]::string::String>
  19:     0x75ecec5d37c2 - <rustc_hir_typeck[29efaf7b7ecf6620]::fn_ctxt::FnCtxt>::pointer_kind.cold
  20:     0x75ecec1ed786 - <rustc_hir_typeck[29efaf7b7ecf6620]::fn_ctxt::FnCtxt>::pointer_kind.warm
  21:     0x75ecec1ed786 - <rustc_hir_typeck[29efaf7b7ecf6620]::fn_ctxt::FnCtxt>::pointer_kind.warm
  22:     0x75ecec053962 - <rustc_hir_typeck[29efaf7b7ecf6620]::cast::CastCheck>::do_check
  23:     0x75eceb7dc97e - <rustc_hir_typeck[29efaf7b7ecf6620]::fn_ctxt::FnCtxt>::check_casts
  24:     0x75eceb7df365 - rustc_hir_typeck[29efaf7b7ecf6620]::typeck
  25:     0x75eceb7deda3 - rustc_query_impl[76a2b42baa80c404]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[76a2b42baa80c404]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 8usize]>>
  26:     0x75eceb5f0df1 - rustc_query_system[c302203c886e3cb8]::query::plumbing::try_execute_query::<rustc_query_impl[76a2b42baa80c404]::DynamicConfig<rustc_query_system[c302203c886e3cb8]::query::caches::VecCache<rustc_span[ec3d05ef96c4664f]::def_id::LocalDefId, rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[76a2b42baa80c404]::plumbing::QueryCtxt, false>
  27:     0x75eceb5efb16 - rustc_query_impl[76a2b42baa80c404]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  28:     0x75eceb5ef74e - <rustc_middle[c1626214f4cb476]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[bed0fd88d68629cc]::check_crate::{closure#4}>::{closure#0}
  29:     0x75eceb5ee42a - rustc_hir_analysis[bed0fd88d68629cc]::check_crate
  30:     0x75eceb9ba647 - rustc_interface[25c67061aa4894d2]::passes::analysis
  31:     0x75eceb9ba187 - rustc_query_impl[76a2b42baa80c404]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[76a2b42baa80c404]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>
  32:     0x75ecebde5025 - rustc_query_system[c302203c886e3cb8]::query::plumbing::try_execute_query::<rustc_query_impl[76a2b42baa80c404]::DynamicConfig<rustc_query_system[c302203c886e3cb8]::query::caches::SingleCache<rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[76a2b42baa80c404]::plumbing::QueryCtxt, false>
  33:     0x75ecebde4d89 - rustc_query_impl[76a2b42baa80c404]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  34:     0x75ecebc2604e - rustc_interface[25c67061aa4894d2]::interface::run_compiler::<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}
  35:     0x75ecebc027c9 - std[5264994d61690aef]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[25c67061aa4894d2]::util::run_in_thread_with_globals<rustc_interface[25c67061aa4894d2]::util::run_in_thread_pool_with_globals<rustc_interface[25c67061aa4894d2]::interface::run_compiler<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>
  36:     0x75ecebc02578 - <<std[5264994d61690aef]::thread::Builder>::spawn_unchecked_<rustc_interface[25c67061aa4894d2]::util::run_in_thread_with_globals<rustc_interface[25c67061aa4894d2]::util::run_in_thread_pool_with_globals<rustc_interface[25c67061aa4894d2]::interface::run_compiler<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#2} as core[5e4926a15c2dbf93]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  37:     0x75eced4147bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hcefa1c6fdca2a888
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2022:9
  38:     0x75eced4147bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3decfc9e691a0d82
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2022:9
  39:     0x75eced4147bb - std::sys::pal::unix::thread::Thread::new::thread_start::hcc86eaf6d07d8e1c
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys/pal/unix/thread.rs:108:17
  40:     0x75eced1b355a - <unknown>
  41:     0x75eced230a3c - <unknown>
  42:                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: please make sure that you have updated to the latest nightly

note: rustc 1.80.0-nightly (5ae5d1353 2024-05-07) running on x86_64-unknown-linux-gnu

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

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions