1
Fork 0

Allow references to interior mutable data behind a feature gate

This commit is contained in:
oli 2020-12-27 17:33:56 +00:00
parent a609fb45ef
commit 0b841846ba
23 changed files with 171 additions and 41 deletions

View file

@ -3,10 +3,11 @@ A borrow of a constant containing interior mutability was attempted.
Erroneous code example:
```compile_fail,E0492
#![feature(const_refs_to_cell)]
use std::sync::atomic::AtomicUsize;
const A: AtomicUsize = AtomicUsize::new(0);
static B: &'static AtomicUsize = &A;
const B: &'static AtomicUsize = &A;
// error: cannot borrow a constant which may contain interior mutability,
// create a static instead
```
@ -18,7 +19,7 @@ can't be changed via a shared `&` pointer, but interior mutability would allow
it. That is, a constant value could be mutated. On the other hand, a `static` is
explicitly a single memory location, which can be mutated at will.
So, in order to solve this error, either use statics which are `Sync`:
So, in order to solve this error, use statics which are `Sync`:
```
use std::sync::atomic::AtomicUsize;
@ -30,6 +31,7 @@ static B: &'static AtomicUsize = &A; // ok!
You can also have this error while using a cell type:
```compile_fail,E0492
#![feature(const_refs_to_cell)]
use std::cell::Cell;
const A: Cell<usize> = Cell::new(1);