Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
nvzqz opened this issue Feb 12, 2024 · 5 comments · Fixed by #120970
Closed

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

nvzqz opened this issue Feb 12, 2024 · 5 comments · Fixed by #120970
Labels
A-const-eval Area: constant evaluation (mir interpretation) C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nvzqz
Copy link
Contributor

nvzqz commented Feb 12, 2024

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

@nvzqz nvzqz added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Feb 12, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-const-eval Area: constant evaluation (mir interpretation) regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed regression-untriaged Untriaged performance or correctness regression. labels Feb 12, 2024
@nvzqz
Copy link
Contributor Author

nvzqz commented 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 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.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 12, 2024

Yea, this is a duplicate of #120949

@oli-obk oli-obk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 12, 2024
@nvzqz
Copy link
Contributor Author

nvzqz commented 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
Copy link
Contributor

oli-obk commented Feb 12, 2024

Let's keep it open an check

@RalfJung
Copy link
Member

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

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 12, 2024
…li-obk

add another test for promoteds-in-static

rust-lang#119614 led to validation of promoteds recursing into statics. These statics can point to `static mut` and interior mutable `static` and do other things we don't allow in `const`, but promoteds are validated as `const`, so we get strange errors (saying "in `const`" when there is no const) and surprising validation failures.

rust-lang#120960 fixes that; this here adds another test.

r? `@oli-obk`
Fixes rust-lang#120968
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 13, 2024
@bors bors closed this as completed in f759c23 Feb 13, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 13, 2024
Rollup merge of rust-lang#120970 - RalfJung:static-promoted-test, r=oli-obk

add another test for promoteds-in-static

rust-lang#119614 led to validation of promoteds recursing into statics. These statics can point to `static mut` and interior mutable `static` and do other things we don't allow in `const`, but promoteds are validated as `const`, so we get strange errors (saying "in `const`" when there is no const) and surprising validation failures.

rust-lang#120960 fixes that; this here adds another test.

r? ``@oli-obk``
Fixes rust-lang#120968
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Feb 13, 2024
add another test for promoteds-in-static

rust-lang/rust#119614 led to validation of promoteds recursing into statics. These statics can point to `static mut` and interior mutable `static` and do other things we don't allow in `const`, but promoteds are validated as `const`, so we get strange errors (saying "in `const`" when there is no const) and surprising validation failures.

rust-lang/rust#120960 fixes that; this here adds another test.

r? ``@oli-obk``
Fixes rust-lang/rust#120968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants