Static checks for &mut in final value of constant with #![feature(const_mut_refs)] · Issue #71212 · rust-lang/rust (original) (raw)

On stable, we prevent users from creating an &mut that points to memory inside a const by forbidding the creation of mutable references during const-eval. This limitation is only temporary, see #57349. We have a feature flag, const_mut_refs, that allows users to create mutable references, but no attempt is made to prevent &mut from escaping into the final value of a const like so:

#![feature(const_mut_refs)] const FOO: &mut i32 = &mut 4;

fn main() { *FOO = 2; }

This errors on the current nightly, and if there were a feature gate that allowed it, we would get an ICE:

error: internal compiler error: src/librustc_mir/interpret/intern.rs:238: const qualif failed to prevent mutable references

I think we've not yet settled on the semantics we want. We're allowing them in const fn and relying on the borrow checker to prevent references from escaping.