Closed
Description
I tried this code:
fn main() {
const PTR: *mut () = 0x8000_0000 as *mut _;
unsafe { &mut *PTR };
}
I expected to see this happen: This should compile without warnings, as I am just turning the 0x8000_0000
raw pointer into a mutable reference.
Instead, this happened:
warning: taking a mutable reference to a `const` item
--> src/main.rs:4:14
|
4 | unsafe { &mut *PTR };
| ^^^^^^^^^
|
= note: `#[warn(const_item_mutation)]` on by default
= note: each usage of a `const` item creates a new temporary
= note: the mutable reference will refer to this temporary, not the original `const` item
note: `const` item defined here
--> src/main.rs:2:5
|
2 | const PTR: *mut () = 0x8000_0000 as *mut _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The lint claims that I'm "taking a mutable reference to a const
item", but I'm not – the const item is the reference.
This warning started popping up a lot for the cortex-m crate, which uses this pattern heavily.
Meta
This is on current nightly (2020-11-05).