Skip to content

Promotion of non-ZST mutable references is incompatible with const_mut_refs #75556

Closed
@RalfJung

Description

@RalfJung

Consider the following code:

#![feature(const_mut_refs)]

static mut TEST: () = {
    let x = &mut [1,2,3];
    x[0] += 1;
};

This should just work. But instead it throws an error:

error[E0080]: could not evaluate static initializer
 --> src/lib.rs:5:5
  |
5 |     x[0] += 1;
  |     ^^^^^^^^^ modifying a static's initial value from another static's initializer

The reason for this is that &mut [1,2,3] gets lifetime extended via promotion, so this reference now points to a separate mutable static -- but mutable statics cannot be mutated during CTFE.

I see no way to fix this, other than stopping to promote mutable references -- which we should IMO do anyway, they are a very strange and unprincipled special case. Just imagine doing this in a loop, suddenly all these different and mutable allocations share the same address...

Cc @rust-lang/wg-const-eval

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.F-const_mut_refs`#![feature(const_mut_refs)]`requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions