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

False positive warning from dereference-assignment of const pointer to mutable data #77321

Closed
nic-hartley opened this issue Sep 29, 2020 · 4 comments · Fixed by #77324
Closed
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nic-hartley
Copy link

I have quite a lot of MMIO in an embedded project, defined and used like this:

const REGISTER: *mut u32 = 0xDEADBEEF as *mut u32;
unsafe fn func() {
    *REGISTER = 1;
}

(see a playground link)

When I cargo build, I'm getting a whole host of warnings of the type:

warning: attempting to modify a `const` item
 --> src/lib.rs:3:5
  |
3 |     *REGISTER = 1;
  |     ^^^^^^^^^^^^^
  |
  = note: `#[warn(const_item_mutation)]` on by default
  = note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
note: `const` item defined here
 --> src/lib.rs:1:1
  |
1 | const REGISTER: *mut u32 = 0xDEADBEEF as *mut u32;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I expect no warnings, because I'm not assigning to a const, I'm assigning to a dereferenced *mut u32, aka a mutable u32.

I couldn't figure out a playground example that would show the value actually mutating, but by hitting hardware registers I can prove that the value does get mutated, and it makes sense that it would: The pointer is const, but it's pointing to a mutable u32. Dereferencing shouldn't care whether the pointer variable is const.

Meta

rustc --version --verbose:

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0

No backtrace for this bug; it's a warning.

@nic-hartley nic-hartley added the C-bug Category: This is a bug. label Sep 29, 2020
@nic-hartley nic-hartley changed the title dereference-assign with const VAR: *mut u32 causes false positive warning const VAR: *mut u32; *VAR = 1 causes false positive warning Sep 29, 2020
@nic-hartley nic-hartley changed the title const VAR: *mut u32; *VAR = 1 causes false positive warning Dereferenced assignment with const pointer to mutable data causes false positive warning Sep 29, 2020
@nic-hartley
Copy link
Author

There's a... fine, I guess, workaround of #![allow(const_item_mutation)] in my case, since all the MMIO code is bundled into a module away from the rest.

@nic-hartley nic-hartley changed the title Dereferenced assignment with const pointer to mutable data causes false positive warning False positive warning from dereference-assignment of const pointer to mutable data Sep 29, 2020
@jyn514 jyn514 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. labels Sep 29, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 29, 2020

cc @Aaron1011

@Aaron1011
Copy link
Member

@nic-hartley This will be fixed by #77324

@nic-hartley
Copy link
Author

@Aaron1011 Awesome, thanks so much!

@camelid camelid added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Sep 30, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Sep 30, 2020
…tr, r=petrochenkov

Don't fire `const_item_mutation` lint on writes through a pointer

Fixes rust-lang#77321
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 1, 2020
…tr, r=petrochenkov

Don't fire `const_item_mutation` lint on writes through a pointer

Fixes rust-lang#77321
@bors bors closed this as completed in c6107c5 Oct 1, 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. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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.

4 participants