Skip to content

Commit dea1363

Browse files
committed
Suppress CONST_ITEM_MUTATION lint if a dereference occurs anywhere
Fixes #79971
1 parent d23e084 commit dea1363

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

compiler/rustc_mir/src/transform/check_const_item_mutation.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
6666
location: Location,
6767
decorate: impl for<'b> FnOnce(LintDiagnosticBuilder<'b>) -> DiagnosticBuilder<'b>,
6868
) {
69-
// Don't lint on borrowing/assigning to a dereference
70-
// e.g:
69+
// Don't lint on borrowing/assigning when a dereference is involved.
70+
// If we 'leave' the temporary via a dereference, we must
71+
// be modifying something else
7172
//
7273
// `unsafe { *FOO = 0; *BAR.field = 1; }`
7374
// `unsafe { &mut *FOO }`
74-
if !matches!(place.projection.last(), Some(PlaceElem::Deref)) {
75+
// `unsafe { (*ARRAY)[0] = val; }
76+
if !place.projection.iter().any(|p| matches!(p, PlaceElem::Deref)) {
7577
let source_info = self.body.source_info(location);
7678
let lint_root = self.body.source_scopes[source_info.scope]
7779
.local_data

src/test/ui/lint/lint-const-item-mutation.rs

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const MUTABLE: Mutable = Mutable { msg: "" };
3030
const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
3131
const VEC: Vec<i32> = Vec::new();
3232
const PTR: *mut () = 1 as *mut _;
33+
const PTR_TO_ARRAY: *mut [u32; 4] = 0x12345678 as _;
34+
const ARRAY_OF_PTR: [*mut u32; 1] = [1 as *mut _];
3335

3436
fn main() {
3537
ARRAY[0] = 5; //~ WARN attempting to modify
@@ -55,4 +57,10 @@ fn main() {
5557
// Test that we don't warn when converting a raw pointer
5658
// into a mutable reference
5759
unsafe { &mut *PTR };
60+
61+
// Test that we don't warn when there's a dereference involved.
62+
// If we ever 'leave' the const via a deference, we're going
63+
// to end up modifying something other than the temporary
64+
unsafe { (*PTR_TO_ARRAY)[0] = 1 };
65+
unsafe { *ARRAY_OF_PTR[0] = 25; }
5866
}

src/test/ui/lint/lint-const-item-mutation.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: attempting to modify a `const` item
2-
--> $DIR/lint-const-item-mutation.rs:35:5
2+
--> $DIR/lint-const-item-mutation.rs:37:5
33
|
44
LL | ARRAY[0] = 5;
55
| ^^^^^^^^^^^^
@@ -13,7 +13,7 @@ LL | const ARRAY: [u8; 1] = [25];
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: attempting to modify a `const` item
16-
--> $DIR/lint-const-item-mutation.rs:36:5
16+
--> $DIR/lint-const-item-mutation.rs:38:5
1717
|
1818
LL | MY_STRUCT.field = false;
1919
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -26,7 +26,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
warning: attempting to modify a `const` item
29-
--> $DIR/lint-const-item-mutation.rs:37:5
29+
--> $DIR/lint-const-item-mutation.rs:39:5
3030
|
3131
LL | MY_STRUCT.inner_array[0] = 'b';
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -39,7 +39,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

4141
warning: taking a mutable reference to a `const` item
42-
--> $DIR/lint-const-item-mutation.rs:38:5
42+
--> $DIR/lint-const-item-mutation.rs:40:5
4343
|
4444
LL | MY_STRUCT.use_mut();
4545
| ^^^^^^^^^^^^^^^^^^^
@@ -58,7 +58,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959

6060
warning: taking a mutable reference to a `const` item
61-
--> $DIR/lint-const-item-mutation.rs:39:5
61+
--> $DIR/lint-const-item-mutation.rs:41:5
6262
|
6363
LL | &mut MY_STRUCT;
6464
| ^^^^^^^^^^^^^^
@@ -72,7 +72,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7373

7474
warning: taking a mutable reference to a `const` item
75-
--> $DIR/lint-const-item-mutation.rs:40:5
75+
--> $DIR/lint-const-item-mutation.rs:42:5
7676
|
7777
LL | (&mut MY_STRUCT).use_mut();
7878
| ^^^^^^^^^^^^^^^^
@@ -86,7 +86,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8787

8888
warning: attempting to modify a `const` item
89-
--> $DIR/lint-const-item-mutation.rs:52:5
89+
--> $DIR/lint-const-item-mutation.rs:54:5
9090
|
9191
LL | MUTABLE2.msg = "wow";
9292
| ^^^^^^^^^^^^^^^^^^^^
@@ -99,7 +99,7 @@ LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
9999
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100100

101101
warning: taking a mutable reference to a `const` item
102-
--> $DIR/lint-const-item-mutation.rs:53:5
102+
--> $DIR/lint-const-item-mutation.rs:55:5
103103
|
104104
LL | VEC.push(0);
105105
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)