-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Description
This code (playground link):
#![feature(const_raw_ptr_deref)]
use std::cell::UnsafeCell;
const FOO: u32 = {
let x = UnsafeCell::new(42);
unsafe { *x.get() }
};
produces on latest nightly:
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
However, following the advice to create a static simply results in a "constants cannot refer to statics" error.
The limitation seems unnecessary. We're not really borrowing a constant; x
only exists during constant evaluation and will not be emitted into the binary, so there's no reason it would be problematic to mutate it.
jgarvin
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team