Repository: rust
Issue: Compiler crash when `-Z virtual-call-elimination` is used
Labels: I-crash,A-LLVM,C-bug,requires-nightly

This code:

```rust
pub trait Foo {
    fn foo(&self);
}

impl Foo for () {
    fn foo(&self) {}
}

pub fn foo(x: &dyn Foo) {
    x.foo()
}

pub fn bar() -> Box<dyn Foo> {
    Box::new(())
}
```

will cause the compiler to crash when `-Z virtual-call-elimination` is enabled and the crate type is `rlib`: https://godbolt.org/z/97cxreTM5

Running with a locally compiled stage 1 rustc with LLVM debug assertions enabled produce the following backtrace, suggesting that the bug is probably somewhere inside LLVM.

<details><summary>Backtrace</summary>
<p>

```
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737034233408) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140737034233408) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140737034233408, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff389f476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff38857f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff388571b in __assert_fail_base (fmt=0x7ffff3a3a150 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x7fffe9990af0 <str.94.llvm> "isa<To>(Val) && \"cast<Ty>() argument of incompatible type!\"", 
    file=0x7fffe9eefea9 <str.98.llvm> "/checkout/src/llvm-project/llvm/include/llvm/Support/Casting.h", line=579, function=<optimised out>) at ./assert/assert.c:92
#6  0x00007ffff3896e96 in __GI___assert_fail (assertion=0x7fffe9990af0 <str.94.llvm> "isa<To>(Val) && \"cast<Ty>() argument of incompatible type!\"", 
    file=0x7fffe9eefea9 <str.98.llvm> "/checkout/src/llvm-project/llvm/include/llvm/Support/Casting.h", line=579, 
    function=0x7fffe9d71ca5 <__PRETTY_FUNCTION__._ZN4llvm4castINS_6MDNodeENS_8MetadataEEEDcPT0_.llvm.8207459463225464818> "decltype(auto) llvm::cast(From *) [To = llvm::MDNode, From = llvm::Metadata]") at ./assert/assert.c:101
#7  0x00007ffff07347f4 in llvm::SelectionDAGBuilder::getValueImpl(llvm::Value const*) [clone .cold.0] () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#8  0x00007ffff0176250 in llvm::SelectionDAGBuilder::visitTargetIntrinsic(llvm::CallInst const&, unsigned int) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#9  0x00007ffff00752aa in llvm::SelectionDAGBuilder::visitIntrinsicCall(llvm::CallInst const&, unsigned int) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#10 0x00007fffefa96ce8 in llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, bool&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#11 0x00007fffefe44aec in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#12 0x00007fffeff25a48 in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#13 0x00007fffeff253e5 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) [clone .llvm.15000214906597364802] () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#14 0x00007fffef81cccd in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#15 0x00007ffff010ddab in llvm::FPPassManager::runOnFunction(llvm::Function&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#16 0x00007ffff010c532 in llvm::FPPassManager::runOnModule(llvm::Module&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#17 0x00007fffefca67b7 in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /home/gary/.rustup/toolchains/stage1/lib/libLLVM-16-rust-1.72.0-nightly.so
#18 0x00007ffff47a6c48 in LLVMRustWriteOutputFile () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#19 0x00007ffff468afc9 in rustc_codegen_llvm::back::write::write_output_file () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#20 0x00007ffff474fd65 in rustc_codegen_llvm::back::write::codegen::with_codegen::<rustc_codegen_llvm::back::write::codegen::{closure#1}, core::result::Result<(), rustc_span::fatal_error::FatalError>> ()
   from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#21 0x00007ffff468e3ac in rustc_codegen_llvm::back::write::codegen () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#22 0x00007ffff46eac29 in <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::write::WriteBackendMethods>::codegen () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#23 0x00007ffff467c241 in rustc_codegen_ssa::back::write::finish_intra_module_work::<rustc_codegen_llvm::LlvmCodegenBackend> () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#24 0x00007ffff467c660 in rustc_codegen_ssa::back::write::execute_optimize_work_item::<rustc_codegen_llvm::LlvmCodegenBackend> () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#25 0x00007ffff46bf6f9 in std::sys_common::backtrace::__rust_begin_short_backtrace::<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()> () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#26 0x00007ffff46e1a06 in <core::panic::unwind_safe::AssertUnwindSafe<<std::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()>::{closure#1}::{closure#0}> as core::ops::function::FnOnce<()>>::call_once ()
   from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#27 0x00007ffff46bc13e in std::panicking::try::<(), core::panic::unwind_safe::AssertUnwindSafe<<std::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()>::{closure#1}::{closure#0}>> ()
   from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#28 0x00007ffff473fc56 in <<std::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/gary/.rustup/toolchains/stage1/lib/librustc_driver-1f1d317d5136c242.so
#29 0x00007ffff3b1214a in <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once () from /home/gary/.rustup/toolchains/stage1/lib/libstd-bb67072942189535.so
#30 0x00007ffff3b3b4fa in std::sys::unix::thread::Thread::new::thread_start () from /home/gary/.rustup/toolchains/stage1/lib/libstd-bb67072942189535.so
#31 0x00007ffff38f1b43 in start_thread (arg=<optimised out>) at ./nptl/pthread_create.c:442
#32 0x00007ffff3983a00 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
```

</p>
</details>

@rustbot labels: +nightly-only +I-crash +A-LLVM