Skip to content

Commit 6522868

Browse files
Rollup merge of #77251 - dtolnay:drop, r=Aaron1011
Bypass const_item_mutation if const's type has Drop impl Follow-up to #75573. This PR disables the const_item_mutation lint in cases that the const has a Drop impl which observes the mutation. ```rust struct Log { msg: &'static str } const LOG: Log = Log { msg: "" }; impl Drop for Log { fn drop(&mut self) { println!("{}", self.msg); } } LOG.msg = "wow"; // prints "wow" ``` r? @Aaron1011
2 parents ccc020a + 804d159 commit 6522868

File tree

3 files changed

+102
-14
lines changed

3 files changed

+102
-14
lines changed

compiler/rustc_mir/src/transform/check_const_item_mutation.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
3131
None
3232
}
3333
}
34+
35+
fn is_const_item_without_destructor(&self, local: Local) -> Option<DefId> {
36+
let def_id = self.is_const_item(local)?;
37+
let mut any_dtor = |_tcx, _def_id| Ok(());
38+
39+
// We avoid linting mutation of a const item if the const's type has a
40+
// Drop impl. The Drop logic observes the mutation which was performed.
41+
//
42+
// pub struct Log { msg: &'static str }
43+
// pub const LOG: Log = Log { msg: "" };
44+
// impl Drop for Log {
45+
// fn drop(&mut self) { println!("{}", self.msg); }
46+
// }
47+
//
48+
// LOG.msg = "wow"; // prints "wow"
49+
//
50+
// FIXME(https://github.com/rust-lang/rust/issues/77425):
51+
// Drop this exception once there is a stable attribute to suppress the
52+
// const item mutation lint for a single specific const only. Something
53+
// equivalent to:
54+
//
55+
// #[const_mutation_allowed]
56+
// pub const LOG: Log = Log { msg: "" };
57+
match self.tcx.calculate_dtor(def_id, &mut any_dtor) {
58+
Some(_) => None,
59+
None => Some(def_id),
60+
}
61+
}
62+
3463
fn lint_const_item_usage(
3564
&self,
3665
const_item: DefId,
@@ -59,7 +88,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
5988
// Assigning directly to a constant (e.g. `FOO = true;`) is a hard error,
6089
// so emitting a lint would be redundant.
6190
if !lhs.projection.is_empty() {
62-
if let Some(def_id) = self.is_const_item(lhs.local) {
91+
if let Some(def_id) = self.is_const_item_without_destructor(lhs.local) {
6392
// Don't lint on writes through a pointer
6493
// (e.g. `unsafe { *FOO = 0; *BAR.field = 1; }`)
6594
if !matches!(lhs.projection.last(), Some(PlaceElem::Deref)) {

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

+21
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,26 @@ impl MyStruct {
99
fn use_mut(&mut self) {}
1010
}
1111

12+
struct Mutable {
13+
msg: &'static str,
14+
}
15+
impl Drop for Mutable {
16+
fn drop(&mut self) {
17+
println!("{}", self.msg);
18+
}
19+
}
20+
21+
struct Mutable2 { // this one has drop glue but not a Drop impl
22+
msg: &'static str,
23+
other: String,
24+
}
25+
1226
const ARRAY: [u8; 1] = [25];
1327
const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
1428
const RAW_PTR: *mut u8 = 1 as *mut u8;
29+
const MUTABLE: Mutable = Mutable { msg: "" };
30+
const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
31+
const VEC: Vec<i32> = Vec::new();
1532

1633
fn main() {
1734
ARRAY[0] = 5; //~ WARN attempting to modify
@@ -29,4 +46,8 @@ fn main() {
2946
*RAW_PTR = 0;
3047
*MY_STRUCT.raw_ptr = 0;
3148
}
49+
50+
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
51+
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
52+
VEC.push(0); //~ WARN taking a mutable reference to a `const` item
3253
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
warning: attempting to modify a `const` item
2-
--> $DIR/lint-const-item-mutation.rs:17:5
2+
--> $DIR/lint-const-item-mutation.rs:34:5
33
|
44
LL | ARRAY[0] = 5;
55
| ^^^^^^^^^^^^
66
|
77
= note: `#[warn(const_item_mutation)]` on by default
88
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
99
note: `const` item defined here
10-
--> $DIR/lint-const-item-mutation.rs:12:1
10+
--> $DIR/lint-const-item-mutation.rs:26:1
1111
|
1212
LL | const ARRAY: [u8; 1] = [25];
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: attempting to modify a `const` item
16-
--> $DIR/lint-const-item-mutation.rs:18:5
16+
--> $DIR/lint-const-item-mutation.rs:35:5
1717
|
1818
LL | MY_STRUCT.field = false;
1919
| ^^^^^^^^^^^^^^^^^^^^^^^
2020
|
2121
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
2222
note: `const` item defined here
23-
--> $DIR/lint-const-item-mutation.rs:13:1
23+
--> $DIR/lint-const-item-mutation.rs:27:1
2424
|
2525
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
warning: attempting to modify a `const` item
29-
--> $DIR/lint-const-item-mutation.rs:19:5
29+
--> $DIR/lint-const-item-mutation.rs:36:5
3030
|
3131
LL | MY_STRUCT.inner_array[0] = 'b';
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
|
3434
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
3535
note: `const` item defined here
36-
--> $DIR/lint-const-item-mutation.rs:13:1
36+
--> $DIR/lint-const-item-mutation.rs:27:1
3737
|
3838
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

4141
warning: taking a mutable reference to a `const` item
42-
--> $DIR/lint-const-item-mutation.rs:20:5
42+
--> $DIR/lint-const-item-mutation.rs:37:5
4343
|
4444
LL | MY_STRUCT.use_mut();
4545
| ^^^^^^^^^^^^^^^^^^^
@@ -52,38 +52,76 @@ note: mutable reference created due to call to this method
5252
LL | fn use_mut(&mut self) {}
5353
| ^^^^^^^^^^^^^^^^^^^^^
5454
note: `const` item defined here
55-
--> $DIR/lint-const-item-mutation.rs:13:1
55+
--> $DIR/lint-const-item-mutation.rs:27:1
5656
|
5757
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959

6060
warning: taking a mutable reference to a `const` item
61-
--> $DIR/lint-const-item-mutation.rs:21:5
61+
--> $DIR/lint-const-item-mutation.rs:38:5
6262
|
6363
LL | &mut MY_STRUCT;
6464
| ^^^^^^^^^^^^^^
6565
|
6666
= note: each usage of a `const` item creates a new temporary
6767
= note: the mutable reference will refer to this temporary, not the original `const` item
6868
note: `const` item defined here
69-
--> $DIR/lint-const-item-mutation.rs:13:1
69+
--> $DIR/lint-const-item-mutation.rs:27:1
7070
|
7171
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7373

7474
warning: taking a mutable reference to a `const` item
75-
--> $DIR/lint-const-item-mutation.rs:22:5
75+
--> $DIR/lint-const-item-mutation.rs:39:5
7676
|
7777
LL | (&mut MY_STRUCT).use_mut();
7878
| ^^^^^^^^^^^^^^^^
7979
|
8080
= note: each usage of a `const` item creates a new temporary
8181
= note: the mutable reference will refer to this temporary, not the original `const` item
8282
note: `const` item defined here
83-
--> $DIR/lint-const-item-mutation.rs:13:1
83+
--> $DIR/lint-const-item-mutation.rs:27:1
8484
|
8585
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8787

88-
warning: 6 warnings emitted
88+
warning: attempting to modify a `const` item
89+
--> $DIR/lint-const-item-mutation.rs:51:5
90+
|
91+
LL | MUTABLE2.msg = "wow";
92+
| ^^^^^^^^^^^^^^^^^^^^
93+
|
94+
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
95+
note: `const` item defined here
96+
--> $DIR/lint-const-item-mutation.rs:30:1
97+
|
98+
LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100+
101+
warning: taking a mutable reference to a `const` item
102+
--> $DIR/lint-const-item-mutation.rs:52:5
103+
|
104+
LL | VEC.push(0);
105+
| ^^^^^^^^^^^
106+
|
107+
= note: each usage of a `const` item creates a new temporary
108+
= note: the mutable reference will refer to this temporary, not the original `const` item
109+
note: mutable reference created due to call to this method
110+
--> $SRC_DIR/alloc/src/vec.rs:LL:COL
111+
|
112+
LL | / pub fn push(&mut self, value: T) {
113+
LL | | // This will panic or abort if we would allocate > isize::MAX bytes
114+
LL | | // or if the length increment would overflow for zero-sized types.
115+
LL | | if self.len == self.buf.capacity() {
116+
... |
117+
LL | | }
118+
LL | | }
119+
| |_____^
120+
note: `const` item defined here
121+
--> $DIR/lint-const-item-mutation.rs:31:1
122+
|
123+
LL | const VEC: Vec<i32> = Vec::new();
124+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125+
126+
warning: 8 warnings emitted
89127

0 commit comments

Comments
 (0)