Repository: rust
Issue: ICE: `Couldn't evaluate constant const`
Labels: I-ICE,T-compiler,C-bug,A-mir-opt,A-mir-opt-inlining,-Zpolymorphize,S-has-mcve,S-bug-has-test

<!--
ICE: Rustc ./a.rs '-Zpolymorphize=on -Zinline-mir=yes -Zinline-mir-threshold=20 -ooutputfile -Zdump-mir-dir=dir' 'error: internal compiler error: compiler/rustc_middle/src/mir/mod.rs:706:17: Couldn't evaluate constant const <T as generic_impl::MagicTrait>::IS_BIG in mono Instance { def: Item(DefId(0:4 ~ a[4021]::generic_impl)), args: [T/#0] }', 'error: internal compiler error: compiler/rustc_middle/src/mir/mod.rs:706:17: Couldn't evaluate constant const <T as generic_impl::MagicTrait>::IS_BIG in mono Instance { def: Item(DefId(0:4 ~ a[4021]::generic_impl)), args: [T/#0] }'
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust
#![crate_type = "lib"]

pub fn demo_for_i32() {
    generic_impl::<bool>();
}

fn generic_impl<T>() {
    trait MagicTrait {
        const IS_BIG: bool;
    }
    impl<T> MagicTrait for T {
        const IS_BIG: bool = std::mem::size_of::<T>() > 10;
    }
    if T::IS_BIG {
        big_impl::<i32>();
    } else {
    }
}

#[inline(never)]
fn big_impl<T>() {}


````

<details><summary><strong>original code</strong></summary>
<p>

original:
````rust
//@ compile-flags: -Cno-prepopulate-passes -Copt-level=0

#![crate_type = "lib"]

#[no_mangle]
pub fn demo_for_i32() {
    generic_impl::<bool>();
}

// Two important things here:
// - We replace the "then" block with `unreachable` to avoid linking problems
// - We neither declare nor define the `big_impl` that said block "calls".

// CHECK-LABEL: ; skip_mono_inside_if_false::generic_impl
// CHECK: start:
// CHECK-NEXT: br label %[[ELSE_BRANCH:bb[0-9]+]]
//@ compile-flags: -Cno-prepopulate-passes -Copt-level=0
// CHECK-NEXT: call skip_mono_inside_if_false::small_impl
// CHECK: bb{{[0-9]+}}:
// CHECK-NEXT: ret void
// CHECK: bb{{[0-9+]}}:
// CHECK-NEXT: unreachable

fn generic_impl<T>() {
    trait MagicTrait {
        const IS_BIG: bool;
    }
    impl<T> MagicTrait for T {
        const IS_BIG: bool = std::mem::size_of::<T>() > 10;
    }
    if T::IS_BIG {
        big_impl::<i32>();
    } else {
        small_impl::<T>();
    }
}

#[no_mangle]
fn small_impl<T>() {}
#[inline(never)]
fn big_impl<T>() {}

````
</p>
</details>

Version information
````
rustc 1.79.0-nightly (f96442b44 2024-04-13)
binary: rustc
commit-hash: f96442b4482fef6a50b65f2284bcfb8921320183
commit-date: 2024-04-13
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.3

````

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc -Zpolymorphize=on -Zinline-mir=yes -Zinline-mir-threshold=20`

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Program output</strong></summary>
<p>

```
error: internal compiler error: compiler/rustc_middle/src/mir/mod.rs:706:17: Couldn't evaluate constant const <T as generic_impl::MagicTrait>::IS_BIG in mono Instance { def: Item(DefId(0:4 ~ mvce[8b78]::generic_impl)), args: [T/#0] }

thread 'rustc' panicked at compiler/rustc_middle/src/mir/mod.rs:706:17:
Box<dyn Any>
stack backtrace:
   0:     0x7a2c31b24c05 - std::backtrace_rs::backtrace::libunwind::trace::h00deba103527a73b
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7a2c31b24c05 - std::backtrace_rs::backtrace::trace_unsynchronized::hf5d25561c7e910bf
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7a2c31b24c05 - std::sys_common::backtrace::_print_fmt::h8c235180ec174889
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7a2c31b24c05 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h97bfcacc988a320c
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7a2c31b73f3b - core::fmt::rt::Argument::fmt::h98a4403414630c7b
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/core/src/fmt/rt.rs:142:9
   5:     0x7a2c31b73f3b - core::fmt::write::h7ed4e82cf6ae16c2
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/core/src/fmt/mod.rs:1153:17
   6:     0x7a2c31b1978f - std::io::Write::write_fmt::h021c9c759a1d994c
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/io/mod.rs:1832:15
   7:     0x7a2c31b249de - std::sys_common::backtrace::_print::h37feb2e2ee8dc03e
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7a2c31b249de - std::sys_common::backtrace::print::h313bef07efe3b3d5
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7a2c31b27359 - std::panicking::default_hook::{{closure}}::h09dd3dc720a66f04
  10:     0x7a2c31b2709d - std::panicking::default_hook::hbca849e2babb7b7b
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/panicking.rs:291:9
  11:     0x7a2c2e4740cb - std[c28e32ecf35ad6f0]::panicking::update_hook::<alloc[7171408d7454e587]::boxed::Box<rustc_driver_impl[6ef106ef01282116]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7a2c31b27a5c - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9e26cddd7f0c9bb2
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/alloc/src/boxed.rs:2032:9
  13:     0x7a2c31b27a5c - std::panicking::rust_panic_with_hook::h6554df54068e3877
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/panicking.rs:792:13
  14:     0x7a2c2e4a4114 - std[c28e32ecf35ad6f0]::panicking::begin_panic::<rustc_errors[880e2e9b97d9d508]::ExplicitBug>::{closure#0}
  15:     0x7a2c2e4a0bb6 - std[c28e32ecf35ad6f0]::sys_common::backtrace::__rust_end_short_backtrace::<std[c28e32ecf35ad6f0]::panicking::begin_panic<rustc_errors[880e2e9b97d9d508]::ExplicitBug>::{closure#0}, !>
  16:     0x7a2c2e49c1a6 - std[c28e32ecf35ad6f0]::panicking::begin_panic::<rustc_errors[880e2e9b97d9d508]::ExplicitBug>
  17:     0x7a2c2e4ad141 - <rustc_errors[880e2e9b97d9d508]::diagnostic::BugAbort as rustc_errors[880e2e9b97d9d508]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7a2c2e952f7c - rustc_middle[4476c877cf51b66a]::util::bug::opt_span_bug_fmt::<rustc_span[96d7dd3a041ddde8]::span_encoding::Span>::{closure#0}
  19:     0x7a2c2e93a3ea - rustc_middle[4476c877cf51b66a]::ty::context::tls::with_opt::<rustc_middle[4476c877cf51b66a]::util::bug::opt_span_bug_fmt<rustc_span[96d7dd3a041ddde8]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7a2c2e93a28b - rustc_middle[4476c877cf51b66a]::ty::context::tls::with_context_opt::<rustc_middle[4476c877cf51b66a]::ty::context::tls::with_opt<rustc_middle[4476c877cf51b66a]::util::bug::opt_span_bug_fmt<rustc_span[96d7dd3a041ddde8]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7a2c2c5595d0 - rustc_middle[4476c877cf51b66a]::util::bug::bug_fmt
  22:     0x7a2c2d51f64d - rustc_monomorphize[aaae63cf39e8f1d7]::collector::collect_items_rec::{closure#0}
  23:     0x7a2c305fee90 - rustc_monomorphize[aaae63cf39e8f1d7]::collector::collect_items_rec
  24:     0x7a2c305ff846 - rustc_monomorphize[aaae63cf39e8f1d7]::collector::collect_items_rec
  25:     0x7a2c305f99af - rustc_monomorphize[aaae63cf39e8f1d7]::partitioning::collect_and_partition_mono_items
  26:     0x7a2c305f8fa8 - rustc_query_impl[eeb8c999b5a8107b]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[eeb8c999b5a8107b]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4476c877cf51b66a]::query::erase::Erased<[u8; 24usize]>>
  27:     0x7a2c305f8f8d - <rustc_query_impl[eeb8c999b5a8107b]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[df923a4d76ddbbb4]::ops::function::FnOnce<(rustc_middle[4476c877cf51b66a]::ty::context::TyCtxt, ())>>::call_once
  28:     0x7a2c305f5b14 - rustc_query_system[70f2acc6e913f1e1]::query::plumbing::try_execute_query::<rustc_query_impl[eeb8c999b5a8107b]::DynamicConfig<rustc_query_system[70f2acc6e913f1e1]::query::caches::SingleCache<rustc_middle[4476c877cf51b66a]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[eeb8c999b5a8107b]::plumbing::QueryCtxt, false>
  29:     0x7a2c305f580b - rustc_query_impl[eeb8c999b5a8107b]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7a2c302e750d - rustc_codegen_ssa[eb7fdc92d8fe1e35]::back::symbol_export::exported_symbols_provider_local
  31:     0x7a2c2f9e3325 - rustc_query_impl[eeb8c999b5a8107b]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[eeb8c999b5a8107b]::query_impl::exported_symbols::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4476c877cf51b66a]::query::erase::Erased<[u8; 16usize]>>
  32:     0x7a2c2f9e32ef - <rustc_query_impl[eeb8c999b5a8107b]::query_impl::exported_symbols::dynamic_query::{closure#2} as core[df923a4d76ddbbb4]::ops::function::FnOnce<(rustc_middle[4476c877cf51b66a]::ty::context::TyCtxt, rustc_span[96d7dd3a041ddde8]::def_id::CrateNum)>>::call_once
  33:     0x7a2c304f7b90 - rustc_query_system[70f2acc6e913f1e1]::query::plumbing::try_execute_query::<rustc_query_impl[eeb8c999b5a8107b]::DynamicConfig<rustc_query_system[70f2acc6e913f1e1]::query::caches::VecCache<rustc_span[96d7dd3a041ddde8]::def_id::CrateNum, rustc_middle[4476c877cf51b66a]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[eeb8c999b5a8107b]::plumbing::QueryCtxt, false>
  34:     0x7a2c304f7765 - rustc_query_impl[eeb8c999b5a8107b]::query_impl::exported_symbols::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7a2c304f75ac - rustc_middle[4476c877cf51b66a]::query::plumbing::query_get_at::<rustc_query_system[70f2acc6e913f1e1]::query::caches::VecCache<rustc_span[96d7dd3a041ddde8]::def_id::CrateNum, rustc_middle[4476c877cf51b66a]::query::erase::Erased<[u8; 16usize]>>>
  36:     0x7a2c2ffbf201 - <rustc_metadata[d787e89e7347da9c]::rmeta::encoder::EncodeContext>::encode_crate_root
  37:     0x7a2c3075bf87 - rustc_metadata[d787e89e7347da9c]::rmeta::encoder::encode_metadata
  38:     0x7a2c307659b6 - rustc_metadata[d787e89e7347da9c]::fs::encode_and_write_metadata
  39:     0x7a2c30764b98 - rustc_interface[f2d8342b28389f0d]::passes::start_codegen
  40:     0x7a2c307642d8 - <rustc_interface[f2d8342b28389f0d]::queries::Queries>::codegen_and_build_linker
  41:     0x7a2c304d1844 - rustc_interface[f2d8342b28389f0d]::interface::run_compiler::<core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>, rustc_driver_impl[6ef106ef01282116]::run_compiler::{closure#0}>::{closure#0}
  42:     0x7a2c305aa79d - std[c28e32ecf35ad6f0]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f2d8342b28389f0d]::util::run_in_thread_with_globals<rustc_interface[f2d8342b28389f0d]::util::run_in_thread_pool_with_globals<rustc_interface[f2d8342b28389f0d]::interface::run_compiler<core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>, rustc_driver_impl[6ef106ef01282116]::run_compiler::{closure#0}>::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>
  43:     0x7a2c305aa5aa - <<std[c28e32ecf35ad6f0]::thread::Builder>::spawn_unchecked_<rustc_interface[f2d8342b28389f0d]::util::run_in_thread_with_globals<rustc_interface[f2d8342b28389f0d]::util::run_in_thread_pool_with_globals<rustc_interface[f2d8342b28389f0d]::interface::run_compiler<core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>, rustc_driver_impl[6ef106ef01282116]::run_compiler::{closure#0}>::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[df923a4d76ddbbb4]::result::Result<(), rustc_span[96d7dd3a041ddde8]::ErrorGuaranteed>>::{closure#2} as core[df923a4d76ddbbb4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7a2c31b3195b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h576ca780b0730923
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/alloc/src/boxed.rs:2018:9
  45:     0x7a2c31b3195b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hfecb3e2e1e75bc77
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/alloc/src/boxed.rs:2018:9
  46:     0x7a2c31b3195b - std::sys::pal::unix::thread::Thread::new::thread_start::h855306464cd28dcd
                               at /rustc/f96442b4482fef6a50b65f2284bcfb8921320183/library/std/src/sys/pal/unix/thread.rs:108:17
  47:     0x7a2c2b2a955a - <unknown>
  48:     0x7a2c2b326a3c - <unknown>
  49:                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.79.0-nightly (f96442b44 2024-04-13) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z polymorphize=on -Z inline-mir=yes -Z inline-mir-threshold=20 -Z dump-mir-dir=dir

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
#1 [exported_symbols] collecting exported symbols for crate `0`
end of query stack
error: aborting due to 1 previous error


```

</p>
</details>

<!--
query stack:
error: internal compiler error: compiler/rustc_middle/src/mir/mod.rs:706:17: Couldn't evaluate constant const <T as generic_impl::MagicTrait>::IS_BIG in mono Instance { def: Item(DefId(0:4 ~ mvce[8b78]::generic_impl)), args: [T/#0] }
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
#1 [exported_symbols] collecting exported symbols for crate `0`

-->