Rollup merge of #80133 - Aaron1011:fix/const-mut-deref, r=estebank

Suppress `CONST_ITEM_MUTATION` lint if a dereference occurs anywhere

Fixes #79971
This commit is contained in:
Yuki Okushi 2020-12-19 15:16:08 +09:00 committed by GitHub
commit 6b52475c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 11 deletions

View File

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

View File

@ -30,6 +30,8 @@ const MUTABLE: Mutable = Mutable { msg: "" };
const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
const VEC: Vec<i32> = Vec::new();
const PTR: *mut () = 1 as *mut _;
const PTR_TO_ARRAY: *mut [u32; 4] = 0x12345678 as _;
const ARRAY_OF_PTR: [*mut u32; 1] = [1 as *mut _];
fn main() {
ARRAY[0] = 5; //~ WARN attempting to modify
@ -55,4 +57,10 @@ fn main() {
// Test that we don't warn when converting a raw pointer
// into a mutable reference
unsafe { &mut *PTR };
// Test that we don't warn when there's a dereference involved.
// If we ever 'leave' the const via a deference, we're going
// to end up modifying something other than the temporary
unsafe { (*PTR_TO_ARRAY)[0] = 1 };
unsafe { *ARRAY_OF_PTR[0] = 25; }
}

View File

@ -1,5 +1,5 @@
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:35:5
--> $DIR/lint-const-item-mutation.rs:37:5
|
LL | ARRAY[0] = 5;
| ^^^^^^^^^^^^
@ -13,7 +13,7 @@ LL | const ARRAY: [u8; 1] = [25];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:36:5
--> $DIR/lint-const-item-mutation.rs:38:5
|
LL | MY_STRUCT.field = false;
| ^^^^^^^^^^^^^^^^^^^^^^^
@ -26,7 +26,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:37:5
--> $DIR/lint-const-item-mutation.rs:39:5
|
LL | MY_STRUCT.inner_array[0] = 'b';
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -39,7 +39,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:38:5
--> $DIR/lint-const-item-mutation.rs:40:5
|
LL | MY_STRUCT.use_mut();
| ^^^^^^^^^^^^^^^^^^^
@ -58,7 +58,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:39:5
--> $DIR/lint-const-item-mutation.rs:41:5
|
LL | &mut MY_STRUCT;
| ^^^^^^^^^^^^^^
@ -72,7 +72,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:40:5
--> $DIR/lint-const-item-mutation.rs:42:5
|
LL | (&mut MY_STRUCT).use_mut();
| ^^^^^^^^^^^^^^^^
@ -86,7 +86,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:52:5
--> $DIR/lint-const-item-mutation.rs:54:5
|
LL | MUTABLE2.msg = "wow";
| ^^^^^^^^^^^^^^^^^^^^
@ -99,7 +99,7 @@ LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:53:5
--> $DIR/lint-const-item-mutation.rs:55:5
|
LL | VEC.push(0);
| ^^^^^^^^^^^