rust-lang / rust-lang/rust

[ICE]: InterpErrorInfo(InterpErrorInfoInner { kind: UndefinedBehavior(ValidationError

Open
#155,496 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-try_as_dyn I-ICE needs-triage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

snippet:

//@compile-flags: -Zmir-opt-level=5 -Zdump-mir=all -Zalways-encode-mir -Zextra-const-ub-checks
#![feature(try_as_dyn)]
use std::any::try_as_dyn;
trait Trait {}
fn store(_: &'static Box<i32>) {}
fn main() {
    let fn_ptr: fn(&'static Box<i32>) = store;
    let dt = try_as_dyn::<_, dyn Trait>(&fn_ptr);
}

Version information

rustc 1.97.0-nightly (8da2d28cb 2026-04-18)
binary: rustc
commit-hash: 8da2d28cbd5a4e2b93e028e709afe09541671663
commit-date: 2026-04-18
host: x86_64-unknown-linux-gnu
release: 1.97.0-nightly
LLVM version: 22.1.2

Possibly related line of code:
https://github.com/rust-lang/rust/blob/8da2d28cbd5a4e2b93e028e709afe09541671663/compiler/rustc_const_eval/src/util/check_validity_requirement.rs#L195-L207

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zdump-mir=all -Zalways-encode-mir -Zextra-const-ub-checks

Program output

warning: unused variable: `dt`
 --> /tmp/icemaker_global_tempdir.40wD6iU9umEx/rustc_testrunner_tmpdir_reporting.sfKgUu6cXCHu/mvce.rs:7:9
  |
7 |     let dt = try_as_dyn::<_, dyn Trait>(&fn_ptr);
  |         ^^ help: if this is intentional, prefix it with an underscore: `_dt`
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default


thread 'rustc' (2380251) panicked at /rustc-dev/8da2d28cbd5a4e2b93e028e709afe09541671663/compiler/rustc_const_eval/src/util/check_validity_requirement.rs:201:41:
called `Result::unwrap()` on an `Err` value: InterpErrorInfo(InterpErrorInfoInner { kind: UndefinedBehavior(ValidationError { orig_ty: pattern_type!(*const Foreign(DefId(2:2767 ~ core[e86e]::ptr::metadata::{extern#0}::VTable)) is !null), path: None, msg: "encountered 0, but expected something greater or equal to 1", ptr_bytes_warning: false }), backtrace: InterpErrorBacktrace { backtrace: None } })
stack backtrace:
   0:     0x7f95b0a22ceb - <<std[d6a62db279dbae46]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[e86e8f32962a9819]::fmt::Display>::fmt
   1:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
   2:     0x7f95b0a39ce6 - <std[d6a62db279dbae46]::sys::stdio::unix::Stderr as std[d6a62db279dbae46]::io::Write>::write_fmt
   3:     0x7f95b09f8f68 - std[d6a62db279dbae46]::panicking::default_hook::{closure#0}
   4:     0x7f95b0a16353 - std[d6a62db279dbae46]::panicking::default_hook
   5:     0x7f95af9db02c - std[d6a62db279dbae46]::panicking::update_hook::<alloc[26814c36517be32d]::boxed::Box<rustc_driver_impl[945e7c8a3fc176fa]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7f95b0a16632 - std[d6a62db279dbae46]::panicking::panic_with_hook
   7:     0x7f95b09f9028 - std[d6a62db279dbae46]::panicking::panic_handler::{closure#0}
   8:     0x7f95b09efd69 - std[d6a62db279dbae46]::sys::backtrace::__rust_end_short_backtrace::<std[d6a62db279dbae46]::panicking::panic_handler::{closure#0}, !>
   9:     0x7f95b09faa8d - __rustc[3f76c9a779ba9221]::rust_begin_unwind
  10:     0x7f95ad58aa3c - core[e86e8f32962a9819]::panicking::panic_fmt
  11:     0x7f95ad104572 - core[e86e8f32962a9819]::result::unwrap_failed
  12:     0x7f95af959661 - rustc_const_eval[2f0023f731c85369]::util::check_validity_requirement::validate_scalar_in_layout
  13:     0x7f95afe6c906 - <rustc_const_eval[2f0023f731c85369]::provide::{closure#3} as core[e86e8f32962a9819]::ops::function::FnOnce<(rustc_middle[253f0d05195ba05e]::ty::context::TyCtxt, rustc_middle[253f0d05195ba05e]::ty::consts::int::ScalarInt, rustc_middle[253f0d05195ba05e]::ty::Ty)>>::call_once
  14:     0x7f95b00a9fa3 - <rustc_middle[253f0d05195ba05e]::ty::print::pretty::FmtPrinter as rustc_middle[253f0d05195ba05e]::ty::print::pretty::PrettyPrinter>::pretty_print_const_scalar_int
  15:     0x7f95b24aeda6 - rustc_middle[253f0d05195ba05e]::mir::pretty::pretty_print_const_value_tcx
  16:     0x7f95b24af58d - rustc_middle[253f0d05195ba05e]::mir::pretty::pretty_print_const_value_tcx
  17:     0x7f95b24af58d - rustc_middle[253f0d05195ba05e]::mir::pretty::pretty_print_const_value_tcx
  18:     0x7f95b24ae81a - <rustc_middle[253f0d05195ba05e]::mir::consts::Const as core[e86e8f32962a9819]::fmt::Display>::fmt
  19:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
  20:     0x7f95b00688f7 - <&alloc[26814c36517be32d]::boxed::Box<rustc_middle[253f0d05195ba05e]::mir::syntax::ConstOperand> as core[e86e8f32962a9819]::fmt::Debug>::fmt
  21:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
  22:     0x7f95b009663e - <rustc_middle[253f0d05195ba05e]::mir::syntax::Operand as core[e86e8f32962a9819]::fmt::Debug>::fmt
  23:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
  24:     0x7f95b00931ca - <rustc_middle[253f0d05195ba05e]::mir::syntax::Rvalue as core[e86e8f32962a9819]::fmt::Debug>::fmt
  25:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
  26:     0x7f95b007cf8d - <rustc_middle[253f0d05195ba05e]::mir::syntax::StatementKind as core[e86e8f32962a9819]::fmt::Debug>::fmt
  27:     0x7f95b102e808 - core[e86e8f32962a9819]::fmt::write
  28:     0x7f95b102e6d5 - alloc[26814c36517be32d]::fmt::format::format_inner
  29:     0x7f95b0027490 - <rustc_middle[253f0d05195ba05e]::mir::pretty::MirWriter>::write_mir_fn
  30:     0x7f95b0033319 - <rustc_middle[253f0d05195ba05e]::mir::pretty::MirDumper>::dump_mir_to_writer
  31:     0x7f95b0033473 - <rustc_middle[253f0d05195ba05e]::mir::pretty::MirDumper>::dump_mir
  32:     0x7f95b1009fe0 - rustc_mir_transform[5e6f23ecb9c29fca]::run_optimization_passes
  33:     0x7f95b147c5d4 - rustc_mir_transform[5e6f23ecb9c29fca]::optimized_mir
  34:     0x7f95b147c221 - rustc_query_impl[a6166eb9fe0387cf]::query_impl::optimized_mir::invoke_provider_fn::__rust_begin_short_backtrace
  35:     0x7f95b10143cd - rustc_query_impl[a6166eb9fe0387cf]::execution::try_execute_query::<rustc_middle[253f0d05195ba05e]::query::caches::DefIdCache<rustc_middle[253f0d05195ba05e]::query::erase::ErasedData<[u8; 8usize]>>, false>
  36:     0x7f95b1013a0e - rustc_query_impl[a6166eb9fe0387cf]::query_impl::optimized_mir::execute_query_non_incr::__rust_end_short_backtrace
  37:     0x7f95b1b0f58e - <rustc_middle[253f0d05195ba05e]::ty::context::TyCtxt>::instance_mir
  38:     0x7f95b1b13e0b - rustc_monomorphize[29aaa413ab07d6fb]::collector::items_of_instance
  39:     0x7f95b1b13d95 - rustc_query_impl[a6166eb9fe0387cf]::query_impl::items_of_instance::invoke_provider_fn::__rust_begin_short_backtrace
  40:     0x7f95b1b108ae - rustc_query_impl[a6166eb9fe0387cf]::execution::try_execute_query::<rustc_middle[253f0d05195ba05e]::query::caches::DefaultCache<(rustc_middle[253f0d05195ba05e]::ty::instance::Instance, rustc_middle[253f0d05195ba05e]::mono::CollectionMode), rustc_middle[253f0d05195ba05e]::query::erase::ErasedData<[u8; 32usize]>>, false>
  41:     0x7f95b1b10511 - rustc_query_impl[a6166eb9fe0387cf]::query_impl::items_of_instance::execute_query_non_incr::__rust_end_short_backtrace
  42:     0x7f95b24947b2 - rustc_monomorphize[29aaa413ab07d6fb]::collector::collect_items_rec
  43:     0x7f95b15fa761 - rustc_monomorphize[29aaa413ab07d6fb]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  44:     0x7f95b16f3759 - rustc_monomorphize[29aaa413ab07d6fb]::partitioning::collect_and_partition_mono_items
  45:     0x7f95b16f33f0 - rustc_query_impl[a6166eb9fe0387cf]::query_impl::collect_and_partition_mono_items::invoke_provider_fn::__rust_begin_short_backtrace
  46:     0x7f95b21b8834 - rustc_query_impl[a6166eb9fe0387cf]::execution::try_execute_query::<rustc_middle[253f0d05195ba05e]::query::caches::SingleCache<rustc_middle[253f0d05195ba05e]::query::erase::ErasedData<[u8; 24usize]>>, false>
  47:     0x7f95b21b85ca - rustc_query_impl[a6166eb9fe0387cf]::query_impl::collect_and_partition_mono_items::execute_query_non_incr::__rust_end_short_backtrace
  48:     0x7f95b22a6679 - rustc_codegen_ssa[c4865c8fd6c4e2a4]::base::codegen_crate::<rustc_codegen_llvm[a5f9bd01c842ba63]::LlvmCodegenBackend>
  49:     0x7f95b22a63a5 - <rustc_codegen_llvm[a5f9bd01c842ba63]::LlvmCodegenBackend as rustc_codegen_ssa[c4865c8fd6c4e2a4]::traits::backend::CodegenBackend>::codegen_crate
  50:     0x7f95b21996b2 - <rustc_interface[1d873664835f04c7]::queries::Linker>::codegen_and_build_linker
  51:     0x7f95b2193926 - rustc_interface[1d873664835f04c7]::interface::run_compiler::<(), rustc_driver_impl[945e7c8a3fc176fa]::run_compiler::{closure#0}>::{closure#1}
  52:     0x7f95b216b28a - std[d6a62db279dbae46]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1d873664835f04c7]::util::run_in_thread_with_globals<rustc_interface[1d873664835f04c7]::util::run_in_thread_pool_with_globals<rustc_interface[1d873664835f04c7]::interface::run_compiler<(), rustc_driver_impl[945e7c8a3fc176fa]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  53:     0x7f95b216b96d - <std[d6a62db279dbae46]::thread::lifecycle::spawn_unchecked<rustc_interface[1d873664835f04c7]::util::run_in_thread_with_globals<rustc_interface[1d873664835f04c7]::util::run_in_thread_pool_with_globals<rustc_interface[1d873664835f04c7]::interface::run_compiler<(), rustc_driver_impl[945e7c8a3fc176fa]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e86e8f32962a9819]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  54:     0x7f95b216c7ec - <std[d6a62db279dbae46]::sys::thread::unix::Thread>::new::thread_start
  55:     0x7f95abca597a - <unknown>
  56:     0x7f95abd292bc - <unknown>
  57:                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: please make sure that you have updated to the latest nightly

note: rustc 1.97.0-nightly (8da2d28cb 2026-04-18) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z mir-opt-level=5 -Z dump-mir=all -Z always-encode-mir -Z extra-const-ub-checks -Z dump-mir-dir=dir

query stack during panic:
#0 [optimized_mir] optimizing MIR for `main`
#1 [items_of_instance] collecting items used by `main`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
warning: 1 warning emitted


@rustbot label +F-try_as_dyn

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the ICE with the supplied main.rs snippet and compiler flags, then start at compiler/rustc_const_eval/src/util/check_validity_requirement.rs:195-207. Trace why validation of the vtable pointer reaches the unwrap during MIR dumping; done means the reproducer no longer panics under these flags, with a regression test covering it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.