Rust errors / Ownership and borrowing
Rust compiler error · checked with rustc 1.99.0
Rust error E0492: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed
What it meansA borrow of a constant containing interior mutability was attempted.
What rustc prints
error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed --> main.rs:4:33 | 4 | const B: &'static AtomicUsize = &A; | ^^ this borrow of an interior mutable value refers to such a temporary | = note: temporaries in constants and statics can have their lifetime extended until the end of the program = note: to avoid accidentally creating global mutable state, such temporaries must be immutable = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
A borrow of a constant containing interior mutability was attempted.
Erroneous code example:
use std::sync::atomic::AtomicUsize;
const A: AtomicUsize = AtomicUsize::new(0);
const B: &'static AtomicUsize = &A;
// error: cannot borrow a constant which may contain interior mutability,
// create a static instead
A const represents a constant value that should never change. If one takes a & reference to the constant, then one is taking a pointer to some memory location containing the value. Normally this is perfectly fine: most values 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, use statics which are Sync:
use std::sync::atomic::AtomicUsize;
static A: AtomicUsize = AtomicUsize::new(0);
static B: &'static AtomicUsize = &A; // ok!
You can also have this error while using a cell type:
use std::cell::Cell;
const A: Cell<usize> = Cell::new(1);
const B: &Cell<usize> = &A;
// error: cannot borrow a constant which may contain interior mutability,
// create a static instead
// or:
struct C { a: Cell<usize> }
const D: C = C { a: Cell::new(1) };
const E: &Cell<usize> = &D.a; // error
// or:
const F: &C = &D; // error
This is because cell types do operations that are not thread-safe. Due to this, they don't implement Sync and thus can't be placed in statics.
However, if you still wish to use these types, you can achieve this by an unsafe wrapper:
use std::cell::Cell;
struct NotThreadSafe<T> {
value: Cell<T>,
}
unsafe impl<T> Sync for NotThreadSafe<T> {}
static A: NotThreadSafe<usize> = NotThreadSafe { value : Cell::new(1) };
static B: &'static NotThreadSafe<usize> = &A; // ok!
Remember this solution is unsafe! You will have to ensure that accesses to the cell are synchronized.
From rustc --explain E0492 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0382borrow of moved value
- E0499cannot borrow as mutable more than once at a time
- E0502cannot borrow as mutable because it is also borrowed as immutable
- E0106missing lifetime specifier
- E0478A lifetime bound was not satisfied
- E0491in type `&'a Foo<'b>`, reference has a longer lifetime than the data it references
- E0493destructor of `(DropType, DropType)` cannot be evaluated at compile-time
- E0496lifetime name `'a` shadows a lifetime name that is already in scope
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?