Skip to content

const_item_mutation warning is triggered when modifying an array by pointer #79971

Closed
@MSxDOS

Description

@MSxDOS
const ARRAY: *mut [u32; 4] = 0x12345678 as _;

pub unsafe fn test(val: u32) {
    (*ARRAY)[0] = val;
}
Warning: attempting to modify a `const` item
 --> <source>:4:5
  |
4 |     (*ARRAY)[0] = val;
  |     ^^^^^^^^^^^^^^^^^
  |
  = 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
 --> <source>:1:1
  |
1 | const ARRAY: *mut [u32; 4] = 0x12345678 as _;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#77324 missed that case.

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-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