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: assertion failed: layout.is_sized() #121443

Closed
matthiaskrgr opened this issue Feb 22, 2024 · 5 comments · Fixed by #122078
Closed

ICE: assertion failed: layout.is_sized() #121443

matthiaskrgr opened this issue Feb 22, 2024 · 5 comments · Fixed by #122078
Assignees
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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 Feb 22, 2024

auto-reduced (treereduce-rust):

type Fn = FnOnce() -> u8;

const TEST: Fn = my_fn;
const TEST2: (Fn, u8) = (TEST, 0);
original code

original:

//@ run-pass

// The actual regression test from #63479. (Including this because my
// first draft at fn-ptr-is-structurally-matchable.rs failed to actually
// cover the case this hit; I've since expanded it accordingly, but the
// experience left me wary of leaving this regression test out.)

#![warn(pointer_structural_match)]

#[derive(Eq)]
struct A {
  a: i64
}

impl PartialEq for A {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.a.eq(&other.a)
    }
}

type Fn = FnOnce() -> u8;

fn my_fn(_args: &[A]) {
  println!("hello world");
}

const TEST: Fn = my_fn;
const TEST2: (Fn, u8) = (TEST, 0);

struct B(Fn);

fn main() {
  let s = B(my_fn);
  match s {
    B(TEST) => println!("matched"),
    //~^ WARN behave unpredictably
    //~| WARN this was previously accepted by the compiler but is being phased out
    _ => panic!("didn't match")
  };
  match (s.0, 0) {
    TEST2 => println!("matched"),
    //~^ WARN behave unpredictably
    //~| WARN this was previously accepted by the compiler but is being phased out
    _ => panic!("didn't match")
  }
}

Version information

rustc 1.78.0-nightly (f8131a48a 2024-02-21)
binary: rustc
commit-hash: f8131a48a46ac3bc8a3d0fe0477055b132cffdc3
commit-date: 2024-02-21
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[E0425]: cannot find value `my_fn` in this scope
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:3:18
  |
3 | const TEST: Fn = my_fn;
  |                  ^^^^^ not found in this scope

error[E0601]: `main` function not found in crate `mvce`
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:4:35
  |
4 | const TEST2: (Fn, u8) = (TEST, 0);
  |                                   ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs`

warning: trait objects without an explicit `dyn` are deprecated
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:1:11
  |
1 | type Fn = FnOnce() -> u8;
  |           ^^^^^^^^^^^^^^
  |
  = 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`
  |
1 | type Fn = dyn FnOnce() -> u8;
  |           +++

error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:3:13
  |
3 | const TEST: Fn = my_fn;
  |             ^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`

error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:3:18
  |
3 | const TEST: Fn = my_fn;
  |                  ^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
  = note: constant expressions must have a statically known size

error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:4:14
  |
4 | const TEST2: (Fn, u8) = (TEST, 0);
  |              ^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
  = note: only the last element of a tuple may have a dynamically sized type

error[E0161]: cannot move a value of type `dyn FnOnce() -> u8`
 --> /tmp/icemaker_global_tempdir.NzAZRzGYMbbq/rustc_testrunner_tmpdir_reporting.SLbkYvmObPzd/mvce.rs:4:26
  |
4 | const TEST2: (Fn, u8) = (TEST, 0);
  |                          ^^^^ the size of `dyn FnOnce() -> u8` cannot be statically determined

thread 'rustc' panicked at compiler/rustc_const_eval/src/const_eval/eval_queries.rs:50:5:
assertion failed: layout.is_sized()
stack backtrace:
   0:     0x7f501e98caf6 - std::backtrace_rs::backtrace::libunwind::trace::hbec2edc838b433e2
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f501e98caf6 - std::backtrace_rs::backtrace::trace_unsynchronized::h88c30257ee0dd166
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f501e98caf6 - std::sys_common::backtrace::_print_fmt::h9176654d509e3088
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f501e98caf6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h671feaee1776bf19
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f501e9dda7c - core::fmt::rt::Argument::fmt::h59605c83ae32d935
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/fmt/rt.rs:142:9
   5:     0x7f501e9dda7c - core::fmt::write::hf7af8dfcfd20db5b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f501e9814bf - std::io::Write::write_fmt::h92a5dbf489025e71
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/io/mod.rs:1846:15
   7:     0x7f501e98c8a4 - std::sys_common::backtrace::_print::h00080302d23998ae
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f501e98c8a4 - std::sys_common::backtrace::print::h6b23124445400245
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f501e98f5eb - std::panicking::default_hook::{{closure}}::hd67b2f9d36675a28
  10:     0x7f501e98f339 - std::panicking::default_hook::h10a39ef11c714e42
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:292:9
  11:     0x7f502187a94c - std[5a8e7f94216a9138]::panicking::update_hook::<alloc[e14ab1f516d287ab]::boxed::Box<rustc_driver_impl[e279e4f1fedd9109]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f501e98fd50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h37ba37dc48823c1d
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2030:9
  13:     0x7f501e98fd50 - std::panicking::rust_panic_with_hook::he3e3bc7c345bf54b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:786:13
  14:     0x7f501e98fa59 - std::panicking::begin_panic_handler::{{closure}}::h4bfbc62f377d59e6
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:652:13
  15:     0x7f501e98cfd6 - std::sys_common::backtrace::__rust_end_short_backtrace::hc8487cf1bc04a693
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f501e98f7c4 - rust_begin_unwind
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:648:5
  17:     0x7f501e9d9f95 - core::panicking::panic_fmt::h88277cce839da186
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/panicking.rs:72:14
  18:     0x7f501e9da053 - core::panicking::panic::h1330285014ca2664
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/panicking.rs:144:5
  19:     0x7f5023a79bef - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_body_using_ecx::{closure#0}
  20:     0x7f5023a76f23 - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_in_interpreter
  21:     0x7f5022ffa938 - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_to_allocation_raw_provider
  22:     0x7f5022ffa706 - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>
  23:     0x7f502360ba51 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::DefaultCache<rustc_middle[446dd094ad85939e]::ty::ParamEnvAnd<rustc_middle[446dd094ad85939e]::mir::interpret::GlobalId>, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  24:     0x7f502360b62c - rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_allocation_raw::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7f5022ffb60d - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_to_const_value_raw_provider
  26:     0x7f5022ffb40c - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_const_value_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>
  27:     0x7f502360ba14 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::DefaultCache<rustc_middle[446dd094ad85939e]::ty::ParamEnvAnd<rustc_middle[446dd094ad85939e]::mir::interpret::GlobalId>, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  28:     0x7f502360b530 - rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_const_value_raw::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7f50235afe6b - <rustc_middle[446dd094ad85939e]::query::plumbing::TyCtxtEnsure>::const_eval_poly
  30:     0x7f50235adbd3 - rustc_hir_analysis[71d4294717f7f2b7]::check_crate
  31:     0x7f50236a2dd3 - rustc_interface[7ca501d7f8f3a34f]::passes::analysis
  32:     0x7f50236a2a29 - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 1usize]>>
  33:     0x7f502393d4a5 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::SingleCache<rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  34:     0x7f502393d209 - rustc_query_impl[9d4c51777e2bb749]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7f502394c719 - rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler::<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}
  36:     0x7f5023bbe705 - std[5a8e7f94216a9138]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_with_globals<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_pool_with_globals<rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>
  37:     0x7f5023bbe532 - <<std[5a8e7f94216a9138]::thread::Builder>::spawn_unchecked_<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_with_globals<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_pool_with_globals<rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#1} as core[85b4fd477ce5bb21]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  38:     0x7f501e999725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1bff3efb61c3fa6b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2016:9
  39:     0x7f501e999725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h83141cf549dced8a
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2016:9
  40:     0x7f501e999725 - std::sys::pal::unix::thread::Thread::new::thread_start::hbe9287fdd7c325bc
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys/pal/unix/thread.rs:108:17
  41:     0x7f501e7819eb - <unknown>
  42:     0x7f501e8057cc - <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 (f8131a48a 2024-02-21) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `TEST2`
#1 [eval_to_const_value_raw] simplifying constant for the type system `TEST2`
#2 [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: E0161, E0277, E0425, E0601.
For more information about an error, try `rustc --explain E0161`.

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

#121087 cc @oli-obk

@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue A-const-eval Area: Constant evaluation (MIR interpretation) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 22, 2024
@gurry
Copy link
Contributor

gurry commented Feb 26, 2024

@rustbot claim

@gurry
Copy link
Contributor

gurry commented Feb 26, 2024

TEST2 should not get to const eval as its TypeckResult should get tainted but it is not even though it is tainted for TEST

@gurry
Copy link
Contributor

gurry commented Feb 28, 2024

We are returning the type of the last element of a tuple in sized_conditions():

obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),

This seems to be the opposite of what we want. Since only the last element of a tuple is allowed to be unsized, we should be returning types of all elements except the last one. If we do that the present ICE gets fixed because now TypeckResult for TEST is tainted.

However, I'm not confident this is the right solution because a. it's surprising that we've had this "wrong" logic in sized_conditions() all this time and b. I'm now seeing codegen related ICEs in a handful of tests. I have opened a draft PR #121726 with the changes. Can someone please take a quick look and see if it makes sense to go down this path?

Edit
There is one major mistake in the above solution. We should return types of all elements from sized_conditions() not all elements except the last one. If we did the latter that would mean that for a tuple type to be sized it is sufficient for only the non-last fields to be sized. That is obviously wrong.

I have now amended PR #121726 to return all elements.

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 6, 2024
…, r=<try>

Check that return type is WF in typeck

Ensures that non-WF types do not pass typeck and then later ICE in MIR/const eval

Fixes rust-lang#121443
@gurry
Copy link
Contributor

gurry commented Mar 7, 2024

We are returning the type of the last element of a tuple in sized_conditions():

obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),

This seems to be the opposite of what we want. Since only the last element of a tuple is allowed to be unsized, we should be returning types of all elements except the last one. If we do that the present ICE gets fixed because now TypeckResult for TEST is tainted.

However, I'm not confident this is the right solution because a. it's surprising that we've had this "wrong" logic in sized_conditions() all this time and b. I'm now seeing codegen related ICEs in a handful of tests. I have opened a draft PR #121726 with the changes. Can someone please take a quick look and see if it makes sense to go down this path?

Edit There is one major mistake in the above solution. We should return types of all elements from sized_conditions() not all elements except the last one. If we did the latter that would mean that for a tuple type to be sized it is sufficient for only the non-last fields to be sized. That is obviously wrong.

I have now amended PR #121726 to return all elements.

We have abandoned this approach and instead fixed the issue by adding a WF check as a part of typeck (PR #122078)

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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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
4 participants