Skip to content

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

Closed

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

Activity

  1. added
    C-bugCategory: This is a bug.
    regression-untriagedUntriaged performance or correctness regression.
    on Feb 12, 2024
  2. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    regression-untriagedUntriaged performance or correctness regression.
    on Feb 12, 2024
  3. nvzqz commented on Feb 12, 2024

    @nvzqz
    ContributorAuthor

    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 BenchOptions via OnceLock. The simple example I gave is just a minimal reproduction of the issue. This appears to happen with any internally-mutable types such as AtomicUsize.

  4. oli-obk commented on Feb 12, 2024

    @oli-obk
    Contributor

    Yea, this is a duplicate of #120949

  5. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Feb 12, 2024
  6. nvzqz commented on Feb 12, 2024

    @nvzqz
    ContributorAuthor

    @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.

  7. oli-obk commented on Feb 12, 2024

    @oli-obk
    Contributor

    Let's keep it open an check

  8. RalfJung commented on Feb 12, 2024

    @RalfJung
    Member

    Yeah it's the same root cause; #120970 adds a test.

  9. added 2 commits that reference this issue on Feb 12, 2024
  10. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 13, 2024
  11. added a commit that references this issue on Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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