Skip to content

"cannot borrow a constant which may contain interior mutability" for local variable inside constant evaluation #69908

@comex

Description

@comex

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.

This issue affects memoffset.

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.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions