Repository navigation
Closed
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.Performance or correctness regression from stable to nightly.
Description
Code
I tried this code:
use std::sync::OnceLock;
static LAZY_INIT: OnceLock<u32> = OnceLock::new();
static LAZY_INIT_REF: &[&OnceLock<u32>] = &[&LAZY_INIT];I expected to see this happen: it compiles with LAZY_INIT being reachable through LAZY_INIT_REF.
Instead, this happened:
error[E0080]: it is undefined behavior to use this value
--> src/main.rs:5:1
|
5 | static LAZY_INIT_REF: &[&OnceLock<u32>] = &[&LAZY_INIT];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered reference to mutable memory in `const`
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾─────alloc4<imm>─────╼ │ ╾──────╼
}
note: erroneous constant encountered
--> src/main.rs:5:43
|
5 | static LAZY_INIT_REF: &[&OnceLock<u32>] = &[&LAZY_INIT];
| ^^^^^^^^^^^^^
Version it worked on
It most recently worked on: nightly-2024-02-10
Version with regression
nightly-2024-02-11
rustc --version --verbose:
rustc 1.78.0-nightly (6cc484351 2024-02-10)
binary: rustc
commit-hash: 6cc4843512d613f51ec81aba689180c31b0b28b6
commit-date: 2024-02-10
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 17.0.6
@rustbot modify labels: +regression-from-stable-to-nightly +A-const-eval +T-compiler -regression-untriaged
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.Performance or correctness regression from stable to nightly.
Activity
nvzqz commentedon Feb 12, 2024
Like #120949, this issue is not contrived since I also use this technique in Divan in code generation for benchmarks of generic functions, specifically for caching
BenchOptionsviaOnceLock. The simple example I gave is just a minimal reproduction of the issue. This appears to happen with any internally-mutable types such asAtomicUsize.oli-obk commentedon Feb 12, 2024
Yea, this is a duplicate of #120949
nvzqz commentedon Feb 12, 2024
@oli-obk this seems similar but the error messages suggest to me that it's not a duplicate. This is about referencing mutable data while the other is a cycle caused by self-referencing.
oli-obk commentedon Feb 12, 2024
Let's keep it open an check
RalfJung commentedon Feb 12, 2024
Yeah it's the same root cause; #120970 adds a test.
Rollup merge of rust-lang#120970 - RalfJung:static-promoted-test, r=o…
Rollup merge of rust-lang#120970 - RalfJung:static-promoted-test, r=o…
Unrolled build for rust-lang#120970
Rollup merge of #120970 - RalfJung:static-promoted-test, r=oli-obk