Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const_item_mutation incorrectly fires when using raw pointers #78819

Closed
jonas-schievink opened this issue Nov 6, 2020 · 0 comments · Fixed by #78865
Closed

const_item_mutation incorrectly fires when using raw pointers #78819

jonas-schievink opened this issue Nov 6, 2020 · 0 comments · Fixed by #78865
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-raw-pointers Area: raw pointers, MaybeUninit, NonNull C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

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

@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. A-raw-pointers Area: raw pointers, MaybeUninit, NonNull labels Nov 6, 2020
@Aaron1011 Aaron1011 self-assigned this Nov 7, 2020
m-ou-se added a commit to m-ou-se/rust that referenced this issue Nov 8, 2020
…ow, r=varkor

Don't fire `CONST_ITEM_MUTATION` lint when borrowing a deref

Fixes rust-lang#78819

This extends the check for dereferences added in PR rust-lang#77324
to cover mutable borrows, as well as direct writes. If we're operating
on a dereference of a `const` item, we shouldn't be firing the lint.
@bors bors closed this as completed in e4e9bb4 Nov 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-raw-pointers Area: raw pointers, MaybeUninit, NonNull C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants