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? #124848

Closed
matthiaskrgr opened this issue May 7, 2024 · 2 comments · Fixed by #124997
Closed

ICE: should be sized but is not? #124848

matthiaskrgr opened this issue May 7, 2024 · 2 comments · Fixed by #124997
Assignees
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

matthiaskrgr commented May 7, 2024

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`.

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

this seems to bisect to #123491 fyi @gurry

@gurry
Copy link
Contributor

gurry commented May 8, 2024

@rustbot claim

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 10, 2024
jhpratt added a commit to jhpratt/rust that referenced this issue May 14, 2024
…r=Nadrieril

Fix ICE while casting a type with error

Fixes rust-lang#124848

The ICE originates here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 The underlying cause is that a type with error, `MyType` was involved in a cast. During cast checks the below method `pointer_kind` was called: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L87-L91 Thanks to the changes in PR rust-lang#123491, `type_is_sized_modulo_regions` in `pointer_kind` returned `false` which caused control to reach the `span_bug` here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 resulting in an ICE.

This PR fixes the issue by changing the `span_bug` to a `span_delayed_bug`.
@bors bors closed this as completed in 18d9c03 May 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 14, 2024
Rollup merge of rust-lang#124997 - gurry:124848-ice-should-be-sized, r=Nadrieril

Fix ICE while casting a type with error

Fixes rust-lang#124848

The ICE originates here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 The underlying cause is that a type with error, `MyType` was involved in a cast. During cast checks the below method `pointer_kind` was called: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L87-L91 Thanks to the changes in PR rust-lang#123491, `type_is_sized_modulo_regions` in `pointer_kind` returned `false` which caused control to reach the `span_bug` here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 resulting in an ICE.

This PR fixes the issue by changing the `span_bug` to a `span_delayed_bug`.
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