Skip to content

Commit 992d05b

Browse files
committed
Auto merge of #107421 - cjgillot:drop-tracking-mir, r=oli-obk
Enable -Zdrop-tracking-mir by default This PR enables the `drop-tracking-mir` flag by default. This flag was initially implemented in rust-lang/rust#101692. This flag computes auto-traits on generators based on their analysis MIR, instead of trying to compute on the HIR body. This removes the need for HIR-based drop-tracking, as we can now reuse the same code to compute generator witness types and to compute generator interior fields.
2 parents 47e59ce + 98620b2 commit 992d05b

File tree

8 files changed

+112
-185
lines changed

8 files changed

+112
-185
lines changed

clippy_lints/src/await_holding_invalid.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::{match_def_path, paths};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::DefId;
5-
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, GeneratorKind};
5+
use rustc_hir::{AsyncGeneratorKind, Body, GeneratorKind};
66
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty::GeneratorInteriorTypeCause;
7+
use rustc_middle::mir::GeneratorLayout;
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::{sym, Span};
1010

@@ -197,36 +197,43 @@ impl LateLintPass<'_> for AwaitHolding {
197197
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
198198
use AsyncGeneratorKind::{Block, Closure, Fn};
199199
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
200-
let body_id = BodyId {
201-
hir_id: body.value.hir_id,
202-
};
203-
let typeck_results = cx.tcx.typeck_body(body_id);
204-
self.check_interior_types(
205-
cx,
206-
typeck_results.generator_interior_types.as_ref().skip_binder(),
207-
body.value.span,
208-
);
200+
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
201+
if let Some(generator_layout) = cx.tcx.mir_generator_witnesses(def_id) {
202+
self.check_interior_types(cx, generator_layout);
203+
}
209204
}
210205
}
211206
}
212207

213208
impl AwaitHolding {
214-
fn check_interior_types(&self, cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
215-
for ty_cause in ty_causes {
209+
fn check_interior_types(&self, cx: &LateContext<'_>, generator: &GeneratorLayout<'_>) {
210+
for (ty_index, ty_cause) in generator.field_tys.iter_enumerated() {
216211
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
212+
let await_points = || {
213+
generator
214+
.variant_source_info
215+
.iter_enumerated()
216+
.filter_map(|(variant, source_info)| {
217+
generator.variant_fields[variant]
218+
.raw
219+
.contains(&ty_index)
220+
.then_some(source_info.span)
221+
})
222+
.collect::<Vec<_>>()
223+
};
217224
if is_mutex_guard(cx, adt.did()) {
218225
span_lint_and_then(
219226
cx,
220227
AWAIT_HOLDING_LOCK,
221-
ty_cause.span,
228+
ty_cause.source_info.span,
222229
"this `MutexGuard` is held across an `await` point",
223230
|diag| {
224231
diag.help(
225232
"consider using an async-aware `Mutex` type or ensuring the \
226233
`MutexGuard` is dropped before calling await",
227234
);
228235
diag.span_note(
229-
ty_cause.scope_span.unwrap_or(span),
236+
await_points(),
230237
"these are all the `await` points this lock is held through",
231238
);
232239
},
@@ -235,18 +242,18 @@ impl AwaitHolding {
235242
span_lint_and_then(
236243
cx,
237244
AWAIT_HOLDING_REFCELL_REF,
238-
ty_cause.span,
245+
ty_cause.source_info.span,
239246
"this `RefCell` reference is held across an `await` point",
240247
|diag| {
241248
diag.help("ensure the reference is dropped before calling `await`");
242249
diag.span_note(
243-
ty_cause.scope_span.unwrap_or(span),
250+
await_points(),
244251
"these are all the `await` points this reference is held through",
245252
);
246253
},
247254
);
248255
} else if let Some(disallowed) = self.def_ids.get(&adt.did()) {
249-
emit_invalid_type(cx, ty_cause.span, disallowed);
256+
emit_invalid_type(cx, ty_cause.source_info.span, disallowed);
250257
}
251258
}
252259
}

clippy_lints/src/dereference.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,6 @@ impl TyCoercionStability {
940940
| ty::FnDef(..)
941941
| ty::Generator(..)
942942
| ty::GeneratorWitness(..)
943-
| ty::GeneratorWitnessMIR(..)
944943
| ty::Closure(..)
945944
| ty::Never
946945
| ty::Tuple(_)

tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ async fn bad() -> u32 {
77
}
88

99
async fn bad_reason() -> u32 {
10-
let _x = Ipv4Addr::new(127, 0, 0, 1);
11-
baz().await
10+
let x = Ipv4Addr::new(127, 0, 0, 1);
11+
let y = baz().await;
12+
let _x = x;
13+
y
1214
}
1315

1416
async fn good() -> u32 {

tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ LL | let _x = String::from("hello");
1111
error: `std::net::Ipv4Addr` may not be held across an `await` point per `clippy.toml`
1212
--> $DIR/await_holding_invalid_type.rs:10:9
1313
|
14-
LL | let _x = Ipv4Addr::new(127, 0, 0, 1);
15-
| ^^
14+
LL | let x = Ipv4Addr::new(127, 0, 0, 1);
15+
| ^
1616

1717
error: `std::string::String` may not be held across an `await` point per `clippy.toml`
18-
--> $DIR/await_holding_invalid_type.rs:31:13
18+
--> $DIR/await_holding_invalid_type.rs:33:13
1919
|
2020
LL | let _x = String::from("hi!");
2121
| ^^

tests/ui/await_holding_lock.stderr

Lines changed: 48 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ LL | let guard = x.lock().unwrap();
66
|
77
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
88
note: these are all the `await` points this lock is held through
9-
--> $DIR/await_holding_lock.rs:9:9
9+
--> $DIR/await_holding_lock.rs:11:15
1010
|
11-
LL | / let guard = x.lock().unwrap();
12-
LL | |
13-
LL | | baz().await
14-
LL | | }
15-
| |_____^
11+
LL | baz().await
12+
| ^^^^^
1613
= note: `-D clippy::await-holding-lock` implied by `-D warnings`
1714
= help: to override `-D warnings` add `#[allow(clippy::await_holding_lock)]`
1815

@@ -24,13 +21,10 @@ LL | let guard = x.read().unwrap();
2421
|
2522
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
2623
note: these are all the `await` points this lock is held through
27-
--> $DIR/await_holding_lock.rs:25:9
24+
--> $DIR/await_holding_lock.rs:27:15
2825
|
29-
LL | / let guard = x.read().unwrap();
30-
LL | |
31-
LL | | baz().await
32-
LL | | }
33-
| |_____^
26+
LL | baz().await
27+
| ^^^^^
3428

3529
error: this `MutexGuard` is held across an `await` point
3630
--> $DIR/await_holding_lock.rs:31:13
@@ -40,13 +34,10 @@ LL | let mut guard = x.write().unwrap();
4034
|
4135
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
4236
note: these are all the `await` points this lock is held through
43-
--> $DIR/await_holding_lock.rs:31:9
37+
--> $DIR/await_holding_lock.rs:33:15
4438
|
45-
LL | / let mut guard = x.write().unwrap();
46-
LL | |
47-
LL | | baz().await
48-
LL | | }
49-
| |_____^
39+
LL | baz().await
40+
| ^^^^^
5041

5142
error: this `MutexGuard` is held across an `await` point
5243
--> $DIR/await_holding_lock.rs:53:13
@@ -56,16 +47,13 @@ LL | let guard = x.lock().unwrap();
5647
|
5748
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
5849
note: these are all the `await` points this lock is held through
59-
--> $DIR/await_holding_lock.rs:53:9
60-
|
61-
LL | / let guard = x.lock().unwrap();
62-
LL | |
63-
LL | |
64-
LL | | let second = baz().await;
65-
... |
66-
LL | | first + second + third
67-
LL | | }
68-
| |_____^
50+
--> $DIR/await_holding_lock.rs:56:28
51+
|
52+
LL | let second = baz().await;
53+
| ^^^^^
54+
LL |
55+
LL | let third = baz().await;
56+
| ^^^^^
6957

7058
error: this `MutexGuard` is held across an `await` point
7159
--> $DIR/await_holding_lock.rs:67:17
@@ -75,13 +63,10 @@ LL | let guard = x.lock().unwrap();
7563
|
7664
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
7765
note: these are all the `await` points this lock is held through
78-
--> $DIR/await_holding_lock.rs:67:13
66+
--> $DIR/await_holding_lock.rs:69:19
7967
|
80-
LL | / let guard = x.lock().unwrap();
81-
LL | |
82-
LL | | baz().await
83-
LL | | };
84-
| |_________^
68+
LL | baz().await
69+
| ^^^^^
8570

8671
error: this `MutexGuard` is held across an `await` point
8772
--> $DIR/await_holding_lock.rs:80:17
@@ -91,13 +76,10 @@ LL | let guard = x.lock().unwrap();
9176
|
9277
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
9378
note: these are all the `await` points this lock is held through
94-
--> $DIR/await_holding_lock.rs:80:13
79+
--> $DIR/await_holding_lock.rs:82:19
9580
|
96-
LL | / let guard = x.lock().unwrap();
97-
LL | |
98-
LL | | baz().await
99-
LL | | }
100-
| |_________^
81+
LL | baz().await
82+
| ^^^^^
10183

10284
error: this `MutexGuard` is held across an `await` point
10385
--> $DIR/await_holding_lock.rs:93:13
@@ -107,13 +89,10 @@ LL | let guard = x.lock();
10789
|
10890
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
10991
note: these are all the `await` points this lock is held through
110-
--> $DIR/await_holding_lock.rs:93:9
92+
--> $DIR/await_holding_lock.rs:95:15
11193
|
112-
LL | / let guard = x.lock();
113-
LL | |
114-
LL | | baz().await
115-
LL | | }
116-
| |_____^
94+
LL | baz().await
95+
| ^^^^^
11796

11897
error: this `MutexGuard` is held across an `await` point
11998
--> $DIR/await_holding_lock.rs:109:13
@@ -123,13 +102,10 @@ LL | let guard = x.read();
123102
|
124103
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
125104
note: these are all the `await` points this lock is held through
126-
--> $DIR/await_holding_lock.rs:109:9
105+
--> $DIR/await_holding_lock.rs:111:15
127106
|
128-
LL | / let guard = x.read();
129-
LL | |
130-
LL | | baz().await
131-
LL | | }
132-
| |_____^
107+
LL | baz().await
108+
| ^^^^^
133109

134110
error: this `MutexGuard` is held across an `await` point
135111
--> $DIR/await_holding_lock.rs:115:13
@@ -139,13 +115,10 @@ LL | let mut guard = x.write();
139115
|
140116
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
141117
note: these are all the `await` points this lock is held through
142-
--> $DIR/await_holding_lock.rs:115:9
118+
--> $DIR/await_holding_lock.rs:117:15
143119
|
144-
LL | / let mut guard = x.write();
145-
LL | |
146-
LL | | baz().await
147-
LL | | }
148-
| |_____^
120+
LL | baz().await
121+
| ^^^^^
149122

150123
error: this `MutexGuard` is held across an `await` point
151124
--> $DIR/await_holding_lock.rs:137:13
@@ -155,16 +128,13 @@ LL | let guard = x.lock();
155128
|
156129
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
157130
note: these are all the `await` points this lock is held through
158-
--> $DIR/await_holding_lock.rs:137:9
159-
|
160-
LL | / let guard = x.lock();
161-
LL | |
162-
LL | |
163-
LL | | let second = baz().await;
164-
... |
165-
LL | | first + second + third
166-
LL | | }
167-
| |_____^
131+
--> $DIR/await_holding_lock.rs:140:28
132+
|
133+
LL | let second = baz().await;
134+
| ^^^^^
135+
LL |
136+
LL | let third = baz().await;
137+
| ^^^^^
168138

169139
error: this `MutexGuard` is held across an `await` point
170140
--> $DIR/await_holding_lock.rs:151:17
@@ -174,13 +144,10 @@ LL | let guard = x.lock();
174144
|
175145
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
176146
note: these are all the `await` points this lock is held through
177-
--> $DIR/await_holding_lock.rs:151:13
147+
--> $DIR/await_holding_lock.rs:153:19
178148
|
179-
LL | / let guard = x.lock();
180-
LL | |
181-
LL | | baz().await
182-
LL | | };
183-
| |_________^
149+
LL | baz().await
150+
| ^^^^^
184151

185152
error: this `MutexGuard` is held across an `await` point
186153
--> $DIR/await_holding_lock.rs:164:17
@@ -190,13 +157,10 @@ LL | let guard = x.lock();
190157
|
191158
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
192159
note: these are all the `await` points this lock is held through
193-
--> $DIR/await_holding_lock.rs:164:13
160+
--> $DIR/await_holding_lock.rs:166:19
194161
|
195-
LL | / let guard = x.lock();
196-
LL | |
197-
LL | | baz().await
198-
LL | | }
199-
| |_________^
162+
LL | baz().await
163+
| ^^^^^
200164

201165
error: this `MutexGuard` is held across an `await` point
202166
--> $DIR/await_holding_lock.rs:185:9
@@ -206,15 +170,10 @@ LL | let mut guard = x.lock().unwrap();
206170
|
207171
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
208172
note: these are all the `await` points this lock is held through
209-
--> $DIR/await_holding_lock.rs:185:5
210-
|
211-
LL | / let mut guard = x.lock().unwrap();
212-
LL | |
213-
LL | | *guard += 1;
214-
LL | | drop(guard);
215-
LL | | baz().await;
216-
LL | | }
217-
| |_^
173+
--> $DIR/await_holding_lock.rs:189:11
174+
|
175+
LL | baz().await;
176+
| ^^^^^
218177

219178
error: aborting due to 13 previous errors
220179

0 commit comments

Comments
 (0)