-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Closed
Copy link
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.F-const_mut_refs`#![feature(const_mut_refs)]``#![feature(const_mut_refs)]`requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.
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
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.F-const_mut_refs`#![feature(const_mut_refs)]``#![feature(const_mut_refs)]`requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.