Skip to content

Regression: Fail to evaluate static slice with reference to internally-mutable data #120968

Closed
@nvzqz

Description

@nvzqz

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

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-compilerRelevant 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions