Skip to content

ICE: missing type for static item crashes due to LazyCell/LazyLock #128198

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mbfm opened this issue Jul 25, 2024 · 1 comment
Closed

ICE: missing type for static item crashes due to LazyCell/LazyLock #128198

mbfm opened this issue Jul 25, 2024 · 1 comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mbfm
Copy link

mbfm commented Jul 25, 2024

Omitting the explicit type when specifying a static variable crashes the compiler, if the right-hand side of the assignment is a LazyCell or LazyLock.

Code

use std::sync::LazyLock;

static WORKS: LazyLock<u8> = LazyLock::new(|| 123);

static BROKEN = LazyLock::new(|| 123);

Rustc Version

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7

Error output

error: missing type for `static` item
 --> src/lib.rs:5:14
  |
5 | static BROKEN = LazyLock::new(|| 123);
  |              ^
  |
note: however, the inferred type `LazyLock<i32, {closure@lib.rs:5:31}>` cannot be named
 --> src/lib.rs:5:17
  |
5 | static BROKEN = LazyLock::new(|| 123);
  |                 ^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/validity.rs:740:21:
assertion `left == right` failed
  left: Mut
 right: Not
Backtrace
stack backtrace:
   0:     0x7fb83f652f05 - std::backtrace_rs::backtrace::libunwind::trace::h58eed11393533053
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7fb83f652f05 - std::backtrace_rs::backtrace::trace_unsynchronized::h6af9bae28ebb6388
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fb83f652f05 - std::sys_common::backtrace::_print_fmt::hb6748916642a4fb2
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7fb83f652f05 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3692694645b1bb6a
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fb83f6a3c4b - core::fmt::rt::Argument::fmt::h7aa93977ba74ae0f
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/fmt/rt.rs:165:63
   5:     0x7fb83f6a3c4b - core::fmt::write::h5131d80b4c69b88d
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/fmt/mod.rs:1168:21
   6:     0x7fb83f647bdf - std::io::Write::write_fmt::h1fb327a7d8b0eb36
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/io/mod.rs:1835:15
   7:     0x7fb83f652cde - std::sys_common::backtrace::_print::he6ebb7b9d89f4456
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fb83f652cde - std::sys_common::backtrace::print::h998d75b840f75a73
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fb83f655719 - std::panicking::default_hook::{{closure}}::h18ec7fe6a38b9da0
  10:     0x7fb83f6554ba - std::panicking::default_hook::hfb3f22c2e4075a6a
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:298:9
  11:     0x7fb83c01e731 - std[dece285a2424a0b]::panicking::update_hook::<alloc[1cfb511ed242f20]::boxed::Box<rustc_driver_impl[aab422d80b3fe9fb]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7fb83f655e4b - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hb8210adad49183e7
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/boxed.rs:2077:9
  13:     0x7fb83f655e4b - std::panicking::rust_panic_with_hook::h51af00bcb4660c4e
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:799:13
  14:     0x7fb83f655bc4 - std::panicking::begin_panic_handler::{{closure}}::h39f76aa863fbe8ce
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:664:13
  15:     0x7fb83f6533c9 - std::sys_common::backtrace::__rust_end_short_backtrace::h4d10fc2251b89840
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7fb83f6558f7 - rust_begin_unwind
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:652:5
  17:     0x7fb83f6a01e3 - core::panicking::panic_fmt::h319840fcbcd912ef
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:72:14
  18:     0x7fb83f6a070e - core::panicking::assert_failed_inner::h18aff98a9c33883a
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:408:17
  19:     0x7fb83bf9d0b3 - core[8d9e01cfbf9cd659]::panicking::assert_failed::<rustc_ast_ir[47e0a5a2ad409e62]::Mutability, rustc_ast_ir[47e0a5a2ad409e62]::Mutability>
  20:     0x7fb83e8f0a32 - rustc_const_eval[e576072d8e619833]::interpret::validity::mutability::<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>.cold
  21:     0x7fb83db2c36a - <rustc_const_eval[e576072d8e619833]::interpret::validity::ValidityVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[e576072d8e619833]::interpret::visitor::ValueVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  22:     0x7fb83db2b6f0 - <rustc_const_eval[e576072d8e619833]::interpret::validity::ValidityVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[e576072d8e619833]::interpret::visitor::ValueVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  23:     0x7fb83db2b6f0 - <rustc_const_eval[e576072d8e619833]::interpret::validity::ValidityVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[e576072d8e619833]::interpret::visitor::ValueVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  24:     0x7fb83db2b6f0 - <rustc_const_eval[e576072d8e619833]::interpret::validity::ValidityVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[e576072d8e619833]::interpret::visitor::ValueVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  25:     0x7fb83db2b6f0 - <rustc_const_eval[e576072d8e619833]::interpret::validity::ValidityVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[e576072d8e619833]::interpret::visitor::ValueVisitor<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  26:     0x7fb83db311e1 - <rustc_const_eval[e576072d8e619833]::interpret::eval_context::InterpCx<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::validate_operand_internal
  27:     0x7fb83e478d77 - rustc_const_eval[e576072d8e619833]::const_eval::eval_queries::eval_body_using_ecx::<rustc_middle[806dc37a95cc1433]::mir::interpret::allocation::ConstAllocation>
  28:     0x7fb83e440623 - rustc_const_eval[e576072d8e619833]::const_eval::eval_queries::eval_static_initializer_provider
  29:     0x7fb83e440367 - rustc_query_impl[e1fdd24b038f677d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e1fdd24b038f677d]::query_impl::eval_static_initializer::dynamic_query::{closure#2}::{closure#0}, rustc_middle[806dc37a95cc1433]::query::erase::Erased<[u8; 16usize]>>
  30:     0x7fb83e440349 - <rustc_query_impl[e1fdd24b038f677d]::query_impl::eval_static_initializer::dynamic_query::{closure#2} as core[8d9e01cfbf9cd659]::ops::function::FnOnce<(rustc_middle[806dc37a95cc1433]::ty::context::TyCtxt, rustc_span[6fd0e9f3c9cf86ed]::def_id::DefId)>>::call_once
  31:     0x7fb83d9935f5 - rustc_query_system[181e9214754b2580]::query::plumbing::try_execute_query::<rustc_query_impl[e1fdd24b038f677d]::DynamicConfig<rustc_query_system[181e9214754b2580]::query::caches::DefIdCache<rustc_middle[806dc37a95cc1433]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[e1fdd24b038f677d]::plumbing::QueryCtxt, true>
  32:     0x7fb83dee778a - rustc_query_impl[e1fdd24b038f677d]::query_impl::eval_static_initializer::get_query_incr::__rust_end_short_backtrace
  33:     0x7fb83d6e39bc - rustc_hir_analysis[41ce6d1fd0be162]::check_crate
  34:     0x7fb83de0a0eb - rustc_interface[e93a829871290abb]::passes::analysis
  35:     0x7fb83de09ae5 - rustc_query_impl[e1fdd24b038f677d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e1fdd24b038f677d]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[806dc37a95cc1433]::query::erase::Erased<[u8; 1usize]>>
  36:     0x7fb83e328b75 - rustc_query_system[181e9214754b2580]::query::plumbing::try_execute_query::<rustc_query_impl[e1fdd24b038f677d]::DynamicConfig<rustc_query_system[181e9214754b2580]::query::caches::SingleCache<rustc_middle[806dc37a95cc1433]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[e1fdd24b038f677d]::plumbing::QueryCtxt, true>
  37:     0x7fb83e3287b8 - rustc_query_impl[e1fdd24b038f677d]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  38:     0x7fb83e02b7cd - rustc_interface[e93a829871290abb]::interface::run_compiler::<core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>, rustc_driver_impl[aab422d80b3fe9fb]::run_compiler::{closure#0}>::{closure#1}
  39:     0x7fb83e13cc69 - std[dece285a2424a0b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[e93a829871290abb]::util::run_in_thread_with_globals<rustc_interface[e93a829871290abb]::interface::run_compiler<core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>, rustc_driver_impl[aab422d80b3fe9fb]::run_compiler::{closure#0}>::{closure#1}, core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>>
  40:     0x7fb83e13ca6a - <<std[dece285a2424a0b]::thread::Builder>::spawn_unchecked_<rustc_interface[e93a829871290abb]::util::run_in_thread_with_globals<rustc_interface[e93a829871290abb]::interface::run_compiler<core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>, rustc_driver_impl[aab422d80b3fe9fb]::run_compiler::{closure#0}>::{closure#1}, core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8d9e01cfbf9cd659]::result::Result<(), rustc_span[6fd0e9f3c9cf86ed]::ErrorGuaranteed>>::{closure#2} as core[8d9e01cfbf9cd659]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  41:     0x7fb83f65fe3b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3af90da315d4b185
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/boxed.rs:2063:9
  42:     0x7fb83f65fe3b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h4e7f3b3405b4b88b
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/boxed.rs:2063:9
  43:     0x7fb83f65fe3b - std::sys::pal::unix::thread::Thread::new::thread_start::h3b8e81128811868f
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys/pal/unix/thread.rs:108:17
  44:     0x7fb838c92ba2 - start_thread
  45:     0x7fb838d1400c - clone3
  46:                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: rustc 1.80.0 (051478957 2024-07-21) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [eval_static_initializer] evaluating initializer of static `BROKEN`
#1 [analysis] running analysis passes on this crate
end of query stack

Relevant Rustc code

The assertion in line 740 fails.

/// Returns whether the allocation is mutable, and whether it's actually a static.
/// For "root" statics we look at the type to account for interior
/// mutability; for nested statics we have no type and directly use the annotated mutability.
fn mutability<'tcx>(ecx: &InterpCx<'tcx, impl Machine<'tcx>>, alloc_id: AllocId) -> Mutability {
// Let's see what kind of memory this points to.
// We're not using `try_global_alloc` since dangling pointers have already been handled.
match ecx.tcx.global_alloc(alloc_id) {
GlobalAlloc::Static(did) => {
let DefKind::Static { safety: _, mutability, nested } = ecx.tcx.def_kind(did) else {
bug!()
};
if nested {
assert!(
ecx.memory.alloc_map.get(alloc_id).is_none(),
"allocations of nested statics are already interned: {alloc_id:?}, {did:?}"
);
// Nested statics in a `static` are never interior mutable,
// so just use the declared mutability.
mutability
} else {
let mutability = match mutability {
Mutability::Not
if !ecx
.tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(*ecx.tcx, ty::ParamEnv::reveal_all()) =>
{
Mutability::Mut
}
_ => mutability,
};
if let Some((_, alloc)) = ecx.memory.alloc_map.get(alloc_id) {
assert_eq!(alloc.mutability, mutability);
}
mutability
}
}
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {
// These are immutable, we better don't allow mutable pointers here.
Mutability::Not
}
}
}

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 25, 2024
@matthiaskrgr
Copy link
Member

duplicate of #124164

@matthiaskrgr matthiaskrgr closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2024
@saethlin saethlin added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 25, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants