Skip to content

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

Open
@ecstatic-morse

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions