Repository: rust
Issue: [ICE]: Both stable and nightly crash at `compiler/rustc_const_eval/src/interpret/operand.rs:84:42` on the invalid code under `opt-level=3`
Labels: I-ICE,T-compiler,C-bug,A-const-eval,S-has-mcve,S-bug-has-test

### Code

```Rust
#![allow(unused)]
#![allow(type_alias_bounds)]
pub trait Foo {
    fn test(&self);
}
async fn generic_function<'g, X: Foo>(x: &'g X) {}
enum E
where
    i32: Foo,
{
    V,
}
struct S
where
    i32: Foo;
trait T where
    i32: Foo,
{
}
union U
where
    i32: Foo,
{
    f: i32,
}
type Y
where
    i32: Foo,
= ();
impl Foo for ()
where
    i32: Foo,
{
    fn test(&self) {
        3i32.test();
        Foo::test(&4i32);
        generic_function(5i32);
    }
}
async fn f()
where
    i32: Foo,
{
    let s = S;
    3i32.test();
    Foo::test(&4i32);
    generic_function(5i32);
}
async fn use_op<'g>(s: &'g String) -> String
where
    String: ::std::ops::Neg<Output = String>,
{
    -s
}
async fn use_for()
where
    i32: Iterator,
{
    for _ in 2i32 {}
}
trait A {}
impl A for i32 {}
struct Dst {
    x: X,
}
struct TwoStrs(str, str)
where
    str: Sized;
async fn unsized_local()
where
    Dst<dyn A>: Sized,
{
    let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
}
async fn return_str() -> str
where
    str: Sized,
{
    *"Sized".to_string().into_boxed_str()
}
async fn global_hr<'g>(x: &'g fn(&()))
where
    fn(&()): Foo,
{
    x.test();
}
fn main() {}
```

### Affected release channels

- [ ] Previous Stable
- [x] Current Stable
- [ ] Current Beta
- [x] Current Nightly

### Rust Version

```Shell
$ rustc --version --verbose
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
```

### Current error output

```Shell
$ rustc -C opt-level=3 --edition=2021 1.rs 
error[E0412]: cannot find type `X` in this scope
  --> 1.rs:64:8
   |
61 | trait A {}
   | ------- similarly named trait `A` defined here
...
64 |     x: X,
   |        ^
   |
help: a trait with a similar name exists
   |
64 |     x: A,
   |        ~
help: you might be missing a type parameter
   |
63 | struct Dst<X> {
   |           +++

error[E0277]: the trait bound `i32: Foo` is not satisfied
 --> 1.rs:9:5
  |
9 |     i32: Foo,
  |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
  |
  = help: the trait `Foo` is implemented for `()`
  = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:15:5
   |
15 |     i32: Foo;
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:17:5
   |
17 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:22:5
   |
22 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:32:5
   |
32 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:42:5
   |
42 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `String: Neg` is not satisfied
  --> 1.rs:51:5
   |
51 |     String: ::std::ops::Neg<Output = String>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Neg` is not implemented for `String`
   |
   = help: see issue #48214

error[E0277]: `i32` is not an iterator
  --> 1.rs:57:5
   |
57 |     i32: Iterator,
   |     ^^^^^^^^^^^^^ `i32` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `i32`
   = help: see issue #48214

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> 1.rs:68:5
   |
68 |     str: Sized;
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = help: see issue #48214

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:71:5
   |
71 |     Dst<dyn A>: Sized,
   |     ^^^------- help: remove the unnecessary generics
   |     |
   |     expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> 1.rs:77:5
   |
77 |     str: Sized,
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = help: see issue #48214

error[E0308]: mismatched types
  --> 1.rs:47:22
   |
47 |     generic_function(5i32);
   |     ---------------- ^^^^ expected `&_`, found `i32`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected reference `&_`
                   found type `i32`
note: function defined here
  --> 1.rs:6:10
   |
6  | async fn generic_function<'g, X: Foo>(x: &'g X) {}
   |          ^^^^^^^^^^^^^^^^             --------
help: consider borrowing here
   |
47 |     generic_function(&5i32);
   |                      +

error[E0600]: cannot apply unary operator `-` to type `&String`
  --> 1.rs:53:5
   |
53 |     -s
   |     ^^ cannot apply unary operator `-`

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:73:12
   |
73 |     let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
   |            ^^^------- help: remove the unnecessary generics
   |            |
   |            expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:73:57
   |
73 |     let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
   |                                                         ^^^------- help: remove the unnecessary generics
   |                                                         |
   |                                                         expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0308]: mismatched types
  --> 1.rs:37:26
   |
37 |         generic_function(5i32);
   |         ---------------- ^^^^ expected `&_`, found `i32`
   |         |
   |         arguments to this function are incorrect
   |
   = note: expected reference `&_`
                   found type `i32`
note: function defined here
  --> 1.rs:6:10
   |
6  | async fn generic_function<'g, X: Foo>(x: &'g X) {}
   |          ^^^^^^^^^^^^^^^^             --------
help: consider borrowing here
   |
37 |         generic_function(&5i32);
   |                          +
```

### Backtrace

```Shell
error: internal compiler error: /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/compiler/rustc_const_eval/src/interpret/operand.rs:84:42: Got a scalar pair where a scalar was expected

thread 'rustc' panicked at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/compiler/rustc_const_eval/src/interpret/operand.rs:84:42:
Box<dyn Any>
stack backtrace:
   0:     0x7f57f8acb12a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h5b6bd5631a6d1f6b
   1:     0x7f57f92ac8f8 - core::fmt::write::h7550c97b06c86515
   2:     0x7f57fa4e3b91 - std::io::Write::write_fmt::h7b09c64fe0be9c84
   3:     0x7f57f8acaf82 - std::sys::backtrace::BacktraceLock::print::h2395ccd2c84ba3aa
   4:     0x7f57f8acd456 - std::panicking::default_hook::{{closure}}::he19d4c7230e07961
   5:     0x7f57f8acd2a0 - std::panicking::default_hook::hf614597d3c67bbdb
   6:     0x7f57f7b8f556 - std[c6eb78587944e35c]::panicking::update_hook::<alloc[148a978a4a62f5d]::boxed::Box<rustc_driver_impl[4c2d2ad79fb810ac]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x7f57f8acdb68 - std::panicking::rust_panic_with_hook::h8942133a8b252070
   8:     0x7f57f7bc6371 - std[c6eb78587944e35c]::panicking::begin_panic::<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>::{closure#0}
   9:     0x7f57f7bb9976 - std[c6eb78587944e35c]::sys::backtrace::__rust_end_short_backtrace::<std[c6eb78587944e35c]::panicking::begin_panic<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>::{closure#0}, !>
  10:     0x7f57f7bb9933 - std[c6eb78587944e35c]::panicking::begin_panic::<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>
  11:     0x7f57f7bcfa31 - <rustc_errors[7f4c80274b6ccf5]::diagnostic::BugAbort as rustc_errors[7f4c80274b6ccf5]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7f57f81f9e74 - rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt::<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}
  13:     0x7f57f81dff6a - rustc_middle[a886f61dbc61428a]::ty::context::tls::with_opt::<rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7f57f81dfdfb - rustc_middle[a886f61dbc61428a]::ty::context::tls::with_context_opt::<rustc_middle[a886f61dbc61428a]::ty::context::tls::with_opt<rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7f57f6b0e9a0 - rustc_middle[a886f61dbc61428a]::util::bug::bug_fmt
  16:     0x7f57fab8f291 - <rustc_mir_transform[b36c87ceb4bb9a8e]::gvn::VnState>::simplify_operand.cold
  17:     0x7f57f6bcb95b - <rustc_mir_transform[b36c87ceb4bb9a8e]::gvn::GVN as rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::MirPass>::run_pass
  18:     0x7f57f92963dd - rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::run_passes_inner
  19:     0x7f57f97959fa - rustc_mir_transform[b36c87ceb4bb9a8e]::optimized_mir
  20:     0x7f57f9793369 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>
  21:     0x7f57f92afb2d - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefIdCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  22:     0x7f57f92af1b3 - rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7f57f9d5939f - rustc_middle[a886f61dbc61428a]::query::plumbing::query_get_at::<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefIdCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>>
  24:     0x7f57f81f98b5 - <rustc_middle[a886f61dbc61428a]::ty::context::TyCtxt>::coroutine_layout
  25:     0x7f57f98fec28 - rustc_ty_utils[45bf0f8bee683616]::layout::layout_of_uncached
  26:     0x7f57f98efa46 - rustc_ty_utils[45bf0f8bee683616]::layout::layout_of
  27:     0x7f57f98ef9d1 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>
  28:     0x7f57f98ef0b2 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefaultCache<rustc_middle[a886f61dbc61428a]::ty::ParamEnvAnd<rustc_middle[a886f61dbc61428a]::ty::Ty>, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  29:     0x7f57f98eeda9 - rustc_query_impl[db795c774d495014]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7f57f689e0da - <rustc_mir_transform[b36c87ceb4bb9a8e]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::MirLint>::run_lint
  31:     0x7f57f92916b9 - rustc_mir_transform[b36c87ceb4bb9a8e]::run_analysis_to_runtime_passes
  32:     0x7f57f94812de - rustc_mir_transform[b36c87ceb4bb9a8e]::mir_drops_elaborated_and_const_checked
  33:     0x7f57f9480cfd - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>
  34:     0x7f57f957ee68 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::VecCache<rustc_span[3e5cf3424d44936d]::def_id::LocalDefId, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  35:     0x7f57f957e6cd - rustc_query_impl[db795c774d495014]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f57f9b7b705 - rustc_interface[88a02114bbdb2383]::passes::run_required_analyses
  37:     0x7f57f9b722e5 - rustc_interface[88a02114bbdb2383]::passes::analysis
  38:     0x7f57f9b722c9 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 1usize]>>
  39:     0x7f57fa07d662 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::SingleCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  40:     0x7f57fa07d38f - rustc_query_impl[db795c774d495014]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7f57f9fed0bb - rustc_interface[88a02114bbdb2383]::interface::run_compiler::<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}
  42:     0x7f57f9fde3d9 - std[c6eb78587944e35c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>
  43:     0x7f57fa0adfac - <<std[c6eb78587944e35c]::thread::Builder>::spawn_unchecked_<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#1} as core[c06ff78fa456ca03]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7f57fa0aea6b - std::sys::pal::unix::thread::Thread::new::thread_start::hcc78f3943333fa94
  45:     0x7f57f4649609 - start_thread
                               at /build/glibc-LcI20x/glibc-2.31/nptl/pthread_create.c:477:8
  46:     0x7f57f456e353 - clone
                               at /build/glibc-LcI20x/glibc-2.31/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  47:                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.83.0 (90b35a623 2024-11-26) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=3

query stack during panic:
#0 [optimized_mir] optimizing MIR for `return_str::{closure#0}`
#1 [layout_of] computing layout of `{async fn body of return_str()}`
end of query stack
error: aborting due to 18 previous errors

Some errors have detailed explanations: E0107, E0277, E0308, E0412, E0600.
For more information about an error, try `rustc --explain E0107`.
```

### Anything else?

Note that the bug can only be reproduced by `rustc -C opt-level=3 --edition=2021 1.rs `.
If we remove `-C opt-level=3`, the ICE disappears.