Closed
Description
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