Skip to content

Compiler Panic on Missing Type Annotation for Static LazyLock Variable #128993

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
rootCircle opened this issue Aug 11, 2024 · 3 comments
Closed
Labels
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

@rootCircle
Copy link

When declaring a static variable of type LazyLock in Rust without providing an explicit type annotation, the Rust compiler panics with an internal error. Providing a type annotation for the LazyLock variable however resolves the issue.

Code

use std::sync::LazyLock;

struct DoAnything {
    name: String
}

impl Anything for DoAnything {
    fn new(name: &str) -> Self {
        Self {
            name: name.to_string()
        }
    }
}

trait Anything {
    fn new(name: &str) -> Self where Self:Sized;
}

// Panics if there is no type annotations
static SOMETHING = LazyLock::new(|| DoAnything::new("David"));

// This works
// static SOMETHING: LazyLock<Anything> = LazyLock::new(|| DoAnything::new("David"));

fn main() {
    println!("Hello, world!");
}

Meta

(Same error on nightly as well! rustc 1.82.0-nightly (e57f309 2024-08-05))

rustc --version --verbose:

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/main.rs:20:17
   |
20 | static SOMETHING = LazyLock::new(|| DoAnything::new("David"));
   |                 ^
   |
note: however, the inferred type `LazyLock<DoAnything, {closure@main.rs:20:34}>` cannot be named
  --> src/main.rs:20:20
   |
20 | static SOMETHING = LazyLock::new(|| DoAnything::new("David"));
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/validity.rs:740:21:
assertion `left == right` failed
  left: Mut
 right: Not
stack backtrace:
   0:     0x71959eb2ff05 - std::backtrace_rs::backtrace::libunwind::trace::h58eed11393533053
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x71959eb2ff05 - std::backtrace_rs::backtrace::trace_unsynchronized::h6af9bae28ebb6388
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x71959eb2ff05 - std::sys_common::backtrace::_print_fmt::hb6748916642a4fb2
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x71959eb2ff05 - <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:     0x71959eb80c4b - core::fmt::rt::Argument::fmt::h7aa93977ba74ae0f
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/fmt/rt.rs:165:63
   5:     0x71959eb80c4b - core::fmt::write::h5131d80b4c69b88d
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/fmt/mod.rs:1168:21
   6:     0x71959eb24bdf - std::io::Write::write_fmt::h1fb327a7d8b0eb36
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/io/mod.rs:1835:15
   7:     0x71959eb2fcde - std::sys_common::backtrace::_print::he6ebb7b9d89f4456
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x71959eb2fcde - std::sys_common::backtrace::print::h998d75b840f75a73
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x71959eb32719 - std::panicking::default_hook::{{closure}}::h18ec7fe6a38b9da0
  10:     0x71959eb324ba - std::panicking::default_hook::hfb3f22c2e4075a6a
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:298:9
  11:     0x71959b41e731 - std[dece285a2424a0b]::panicking::update_hook::<alloc[1cfb511ed242f20]::boxed::Box<rustc_driver_impl[aab422d80b3fe9fb]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x71959eb32e4b - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hb8210adad49183e7
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/boxed.rs:2077:9
  13:     0x71959eb32e4b - std::panicking::rust_panic_with_hook::h51af00bcb4660c4e
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:799:13
  14:     0x71959eb32bc4 - std::panicking::begin_panic_handler::{{closure}}::h39f76aa863fbe8ce
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:664:13
  15:     0x71959eb303c9 - std::sys_common::backtrace::__rust_end_short_backtrace::h4d10fc2251b89840
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x71959eb328f7 - rust_begin_unwind
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:652:5
  17:     0x71959eb7d1e3 - core::panicking::panic_fmt::h319840fcbcd912ef
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:72:14
  18:     0x71959eb7d70e - core::panicking::assert_failed_inner::h18aff98a9c33883a
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:408:17
  19:     0x71959b39d0b3 - core[8d9e01cfbf9cd659]::panicking::assert_failed::<rustc_ast_ir[47e0a5a2ad409e62]::Mutability, rustc_ast_ir[47e0a5a2ad409e62]::Mutability>
  20:     0x71959dcf0a32 - rustc_const_eval[e576072d8e619833]::interpret::validity::mutability::<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>.cold
  21:     0x71959cf2c36a - <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:     0x71959cf2b6f0 - <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:     0x71959cf2b6f0 - <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:     0x71959cf2b6f0 - <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:     0x71959cf2b6f0 - <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:     0x71959cf311e1 - <rustc_const_eval[e576072d8e619833]::interpret::eval_context::InterpCx<rustc_const_eval[e576072d8e619833]::const_eval::machine::CompileTimeInterpreter>>::validate_operand_internal
  27:     0x71959d878d77 - rustc_const_eval[e576072d8e619833]::const_eval::eval_queries::eval_body_using_ecx::<rustc_middle[806dc37a95cc1433]::mir::interpret::allocation::ConstAllocation>
  28:     0x71959d840623 - rustc_const_eval[e576072d8e619833]::const_eval::eval_queries::eval_static_initializer_provider
  29:     0x71959d840367 - 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:     0x71959d840349 - <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:     0x71959cd935f5 - 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:     0x71959d2e778a - rustc_query_impl[e1fdd24b038f677d]::query_impl::eval_static_initializer::get_query_incr::__rust_end_short_backtrace
  33:     0x71959cae39bc - rustc_hir_analysis[41ce6d1fd0be162]::check_crate
  34:     0x71959d20a0eb - rustc_interface[e93a829871290abb]::passes::analysis
  35:     0x71959d209ae5 - 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:     0x71959d728b75 - 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:     0x71959d7287b8 - rustc_query_impl[e1fdd24b038f677d]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  38:     0x71959d42b7cd - 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:     0x71959d53cc69 - 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:     0x71959d53ca6a - <<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:     0x71959eb3ce3b - <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:     0x71959eb3ce3b - <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:     0x71959eb3ce3b - std::sys::pal::unix::thread::Thread::new::thread_start::h3b8e81128811868f
                               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/sys/pal/unix/thread.rs:108:17
  44:     0x71959e8cd39d - <unknown>
  45:     0x71959e95249c - <unknown>
  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 bin -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 `SOMETHING`
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `bug` (bin "bug") due to 1 previous error
Backtrace

   Compiling bug v0.1.0 (/home/violow/bug)
error: missing type for `static` item
  --> src/main.rs:20:17
   |
20 | static SOMETHING = LazyLock::new(|| DoAnything::new("David"));
   |                 ^
   |
note: however, the inferred type `LazyLock<DoAnything, {closure@main.rs:20:34}>` cannot be named
  --> src/main.rs:20:20
   |
20 | static SOMETHING = LazyLock::new(|| DoAnything::new("David"));
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/validity.rs:763:21:
assertion `left == right` failed
  left: Mut
 right: Not
stack backtrace:
   0: rust_begin_unwind
             at /rustc/e57f3090aec33cdbf66063c866afaa5e1e78b9bb/library/std/src/panicking.rs:662:5
   1: core::panicking::panic_fmt
             at /rustc/e57f3090aec33cdbf66063c866afaa5e1e78b9bb/library/core/src/panicking.rs:74:14
   2: core::panicking::assert_failed_inner
             at /rustc/e57f3090aec33cdbf66063c866afaa5e1e78b9bb/library/core/src/panicking.rs:412:17
   3: core::panicking::assert_failed::<rustc_ast_ir::Mutability, rustc_ast_ir::Mutability>
   4: rustc_const_eval::interpret::validity::mutability::<rustc_const_eval::const_eval::machine::CompileTimeMachine>.cold
   5: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::visit_value
   6: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::visit_value
   7: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::visit_value
   8: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::visit_value
   9: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::visit_value
  10: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::validate_operand_internal
  11: rustc_const_eval::const_eval::eval_queries::eval_body_using_ecx::<rustc_middle::mir::interpret::allocation::ConstAllocation>
  12: rustc_const_eval::const_eval::eval_queries::eval_static_initializer_provider
      [... omitted 2 frames ...]
  13: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
  14: rustc_hir_analysis::check_crate
  15: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  16: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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: please attach the file at `/home/violow/bug/rustc-ice-2024-08-11T22_02_52-164133.txt` to your bug report

note: compiler flags: --crate-type bin -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 `SOMETHING`
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `bug` (bin "bug") due to 1 previous error

@rootCircle rootCircle 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. labels Aug 11, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 11, 2024
@rootCircle
Copy link
Author

Playground URL

@saethlin
Copy link
Member

saethlin commented Aug 11, 2024

Minimized:

#![crate_type = "lib"]
static SOMETHING = std::sync::LazyLock::new(|| {});

(also I suspect this is another tainting issue where const-eval is trying to be run on MIR that didn't pass type-checking)

@theemathas
Copy link
Contributor

Duplicate of #124164

@matthiaskrgr matthiaskrgr closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 12, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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

6 participants