Skip to content

ICE when trying to create a static reference to a structure enclosing an external static value #143174

Open
@im-0

Description

@im-0

Code

type Fun = unsafe extern "C" fn();

struct Foo(Fun);

static FOO: &Foo = &Foo(BAR);

unsafe extern "C" {
    static BAR: Fun;
}

Meta

Reproducible with current stable, beta and nightly: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=89a7cf328a28e0ed33ab673bfb13981f

rustc --version --verbose:

rustc 1.87.0 (17067e9ac 2025-05-09) (Fedora 1.87.0-2.fc42)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.3

Error output

   Compiling playground v0.0.1 (/playground)
error[E0080]: could not evaluate static initializer
 --> src/lib.rs:5:25
  |
5 | static FOO: &Foo = &Foo(BAR);
  |                         ^^^ cannot access extern static `BAR`

error: internal compiler error: compiler/rustc_const_eval/src/interpret/eval_context.rs:553:33: interpret const eval failure of Unevaluated(UnevaluatedConst { def: DefId(0:7 ~ playground[cc9b]::FOO), args: [], promoted: Some(promoted[0]) }, &'{erased} Foo) which is not in required_consts
 --> src/lib.rs:5:20
  |
5 | static FOO: &Foo = &Foo(BAR);
  |                    ^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/eval_context.rs:553:33:
Box<dyn Any>
stack backtrace:
   0:     0x7bf596146543 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h46a716bba2450163
   1:     0x7bf596805947 - core::fmt::write::h275e5980d7008551
   2:     0x7bf5978c16d1 - std::io::Write::write_fmt::h561a66a0340b6995
   3:     0x7bf5961463a2 - std::sys::backtrace::BacktraceLock::print::hafb9d5969adc39a0
   4:     0x7bf596149f9a - std::panicking::default_hook::{{closure}}::hae2e97a5c4b2b777
   5:     0x7bf596149b1f - std::panicking::default_hook::h3db1b505cfc4eb79
   6:     0x7bf595175b93 - std[61bb86a81f0491]::panicking::update_hook::<alloc[7c55b5a5cf9e3c5f]::boxed::Box<rustc_driver_impl[b7d73271a09ede7d]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7bf59614a813 - std::panicking::rust_panic_with_hook::h409da73ddef13937
   8:     0x7bf5951b1f41 - std[61bb86a81f0491]::panicking::begin_panic::<rustc_errors[38d461c0e4a8a2b3]::ExplicitBug>::{closure#0}
   9:     0x7bf5951a5e16 - std[61bb86a81f0491]::sys::backtrace::__rust_end_short_backtrace::<std[61bb86a81f0491]::panicking::begin_panic<rustc_errors[38d461c0e4a8a2b3]::ExplicitBug>::{closure#0}, !>
  10:     0x7bf5951a5bf3 - std[61bb86a81f0491]::panicking::begin_panic::<rustc_errors[38d461c0e4a8a2b3]::ExplicitBug>
  11:     0x7bf5951bc5c1 - <rustc_errors[38d461c0e4a8a2b3]::diagnostic::BugAbort as rustc_errors[38d461c0e4a8a2b3]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7bf59578b8bc - <rustc_errors[38d461c0e4a8a2b3]::DiagCtxtHandle>::span_bug::<rustc_span[1f7aec696a0836ce]::span_encoding::Span, alloc[7c55b5a5cf9e3c5f]::string::String>
  13:     0x7bf59582ad87 - rustc_middle[1134768a56a896b4]::util::bug::opt_span_bug_fmt::<rustc_span[1f7aec696a0836ce]::span_encoding::Span>::{closure#0}
  14:     0x7bf59580331a - rustc_middle[1134768a56a896b4]::ty::context::tls::with_opt::<rustc_middle[1134768a56a896b4]::util::bug::opt_span_bug_fmt<rustc_span[1f7aec696a0836ce]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7bf59580318b - rustc_middle[1134768a56a896b4]::ty::context::tls::with_context_opt::<rustc_middle[1134768a56a896b4]::ty::context::tls::with_opt<rustc_middle[1134768a56a896b4]::util::bug::opt_span_bug_fmt<rustc_span[1f7aec696a0836ce]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7bf593507947 - rustc_middle[1134768a56a896b4]::util::bug::span_bug_fmt::<rustc_span[1f7aec696a0836ce]::span_encoding::Span>
  17:     0x7bf5968720b1 - rustc_const_eval[1f8ce8110e3e9be6]::const_eval::eval_queries::eval_body_using_ecx::<rustc_middle[1134768a56a896b4]::mir::interpret::allocation::ConstAllocation>
  18:     0x7bf59727240d - rustc_const_eval[1f8ce8110e3e9be6]::const_eval::eval_queries::eval_static_initializer_provider
  19:     0x7bf597271fd9 - rustc_query_impl[6f0ca6887c150a69]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[6f0ca6887c150a69]::query_impl::eval_static_initializer::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1134768a56a896b4]::query::erase::Erased<[u8; 16usize]>>
  20:     0x7bf597271fa3 - <rustc_query_impl[6f0ca6887c150a69]::query_impl::eval_static_initializer::dynamic_query::{closure#2} as core[105cb658ab12d76f]::ops::function::FnOnce<(rustc_middle[1134768a56a896b4]::ty::context::TyCtxt, rustc_span[1f7aec696a0836ce]::def_id::DefId)>>::call_once
  21:     0x7bf5969fa9d2 - rustc_query_system[48caa8a8a4cfcb68]::query::plumbing::try_execute_query::<rustc_query_impl[6f0ca6887c150a69]::DynamicConfig<rustc_query_system[48caa8a8a4cfcb68]::query::caches::DefIdCache<rustc_middle[1134768a56a896b4]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[6f0ca6887c150a69]::plumbing::QueryCtxt, false>
  22:     0x7bf5978c432b - rustc_query_impl[6f0ca6887c150a69]::query_impl::eval_static_initializer::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7bf596bc75c3 - rustc_hir_analysis[5a87eb6962d34b42]::check_crate
  24:     0x7bf5971e0237 - rustc_interface[a43b584b4c4124b4]::passes::run_required_analyses
  25:     0x7bf5976edb5e - rustc_interface[a43b584b4c4124b4]::passes::analysis
  26:     0x7bf5976edb35 - rustc_query_impl[6f0ca6887c150a69]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[6f0ca6887c150a69]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1134768a56a896b4]::query::erase::Erased<[u8; 0usize]>>
  27:     0x7bf5976efc3a - rustc_query_system[48caa8a8a4cfcb68]::query::plumbing::try_execute_query::<rustc_query_impl[6f0ca6887c150a69]::DynamicConfig<rustc_query_system[48caa8a8a4cfcb68]::query::caches::SingleCache<rustc_middle[1134768a56a896b4]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[6f0ca6887c150a69]::plumbing::QueryCtxt, false>
  28:     0x7bf5976ef90f - rustc_query_impl[6f0ca6887c150a69]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7bf5979aa0c7 - rustc_interface[a43b584b4c4124b4]::passes::create_and_enter_global_ctxt::<core[105cb658ab12d76f]::option::Option<rustc_interface[a43b584b4c4124b4]::queries::Linker>, rustc_driver_impl[b7d73271a09ede7d]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  30:     0x7bf5978fe966 - rustc_interface[a43b584b4c4124b4]::interface::run_compiler::<(), rustc_driver_impl[b7d73271a09ede7d]::run_compiler::{closure#0}>::{closure#1}
  31:     0x7bf5978b523e - std[61bb86a81f0491]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[a43b584b4c4124b4]::util::run_in_thread_with_globals<rustc_interface[a43b584b4c4124b4]::util::run_in_thread_pool_with_globals<rustc_interface[a43b584b4c4124b4]::interface::run_compiler<(), rustc_driver_impl[b7d73271a09ede7d]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  32:     0x7bf5978b56b4 - <<std[61bb86a81f0491]::thread::Builder>::spawn_unchecked_<rustc_interface[a43b584b4c4124b4]::util::run_in_thread_with_globals<rustc_interface[a43b584b4c4124b4]::util::run_in_thread_pool_with_globals<rustc_interface[a43b584b4c4124b4]::interface::run_compiler<(), rustc_driver_impl[b7d73271a09ede7d]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[105cb658ab12d76f]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  33:     0x7bf5978b6aab - std::sys::pal::unix::thread::Thread::new::thread_start::h1822d22fde68314f
  34:     0x7bf59168aaa4 - <unknown>
  35:     0x7bf591717a34 - clone
  36:                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.88.0 (6b00bc388 2025-06-23) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [eval_static_initializer] evaluating initializer of static `FOO`
#1 [analysis] running analysis passes on this crate
end of query stack
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error
Backtrace

   Compiling playground v0.0.1 (/playground)
error[E0080]: could not evaluate static initializer
 --> src/lib.rs:5:25
  |
5 | static FOO: &Foo = &Foo(BAR);
  |                         ^^^ cannot access extern static `BAR`

error: internal compiler error: compiler/rustc_const_eval/src/interpret/eval_context.rs:553:33: interpret const eval failure of Unevaluated(UnevaluatedConst { def: DefId(0:7 ~ playground[cc9b]::FOO), args: [], promoted: Some(promoted[0]) }, &'{erased} Foo) which is not in required_consts
 --> src/lib.rs:5:20
  |
5 | static FOO: &Foo = &Foo(BAR);
  |                    ^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/eval_context.rs:553:33:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: <rustc_errors::DiagCtxtHandle>::span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   5: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   6: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   7: rustc_const_eval::const_eval::eval_queries::eval_body_using_ecx::<rustc_middle::mir::interpret::allocation::ConstAllocation>
   8: rustc_const_eval::const_eval::eval_queries::eval_static_initializer_provider
      [... omitted 2 frames ...]
   9: rustc_hir_analysis::check_crate
  10: rustc_interface::passes::run_required_analyses
  11: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  12: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  13: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.88.0 (6b00bc388 2025-06-23) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [eval_static_initializer] evaluating initializer of static `FOO`
#1 [analysis] running analysis passes on this crate
end of query stack
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions