Skip to content

static containing UnsafeCell can circumvent const-checking with certain feature gates #77353

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

Open
ecstatic-morse opened this issue Sep 30, 2020 · 0 comments
Assignees
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) B-unstable Blocker: Implemented in the nightly compiler and unstable. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

The following example passes the static const checks, reaching const-eval and erroring post-monomorphization. I need to figure out what's going wrong here before const_mut_refs is stabilized.

cc #57349

#![feature(const_raw_ptr_deref, const_mut_refs)]
#![allow(unused)]

use std::cell::UnsafeCell;

struct Foo(UnsafeCell<u32>);

unsafe impl Send for Foo {}
unsafe impl Sync for Foo {}

static FOO: Foo = Foo(UnsafeCell::new(42));

static BAR: () = unsafe {
    *FOO.0.get() = 5; //~ ERROR
};

fn main() {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0080]: could not evaluate static initializer
  --> src/main.rs:14:5
   |
14 |     *FOO.0.get() = 5; //~ ERROR
   |     ^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

@ecstatic-morse ecstatic-morse added B-unstable Blocker: Implemented in the nightly compiler and unstable. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 30, 2020
@ecstatic-morse ecstatic-morse self-assigned this Sep 30, 2020
@jyn514 jyn514 added the C-bug Category: This is a bug. label Sep 30, 2020
@ecstatic-morse ecstatic-morse changed the title UnsafeCell can circumvent const-checking with certain feature gates static containing UnsafeCell can circumvent const-checking with certain feature gates Sep 30, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) B-unstable Blocker: Implemented in the nightly compiler and unstable. C-bug Category: This is a bug. 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

2 participants