Skip to content

const_item_mutation incorrectly fires when using raw pointers #78819

Closed
@jonas-schievink

Description

@jonas-schievink

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).

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-raw-pointersArea: raw pointers, MaybeUninit, NonNullC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions