Skip to content

const_item_mutation incorrectly fires when using raw pointers #78819

Closed

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

Activity

  1. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    A-raw-pointersArea: raw pointers, MaybeUninit, NonNull
    on Nov 6, 2020
  2. self-assigned this
    on Nov 7, 2020
  3. added a commit that references this issue on Nov 8, 2020
    e4e9bb4
  4. added a commit that references this issue on Nov 8, 2020
    91759b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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