Skip to content

Commit 775b795

Browse files
committed
Add test for drop-before-await FP
1 parent b63dddb commit 775b795

File tree

2 files changed

+64
-33
lines changed

2 files changed

+64
-33
lines changed

tests/ui/await_holding_lock.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate parking_lot;
44

55
// When adding or modifying a test, please do the same for parking_lot::Mutex.
66
mod std_mutex {
7+
use super::baz;
78
use std::sync::{Mutex, RwLock};
89

910
pub async fn bad(x: &Mutex<u32>) -> u32 {
@@ -45,10 +46,6 @@ mod std_mutex {
4546
47
4647
}
4748

48-
pub async fn baz() -> u32 {
49-
42
50-
}
51-
5249
pub async fn also_bad(x: &Mutex<u32>) -> u32 {
5350
let first = baz().await;
5451

@@ -85,6 +82,7 @@ mod std_mutex {
8582

8683
// When adding or modifying a test, please do the same for std::Mutex.
8784
mod parking_lot_mutex {
85+
use super::baz;
8886
use parking_lot::{Mutex, RwLock};
8987

9088
pub async fn bad(x: &Mutex<u32>) -> u32 {
@@ -126,10 +124,6 @@ mod parking_lot_mutex {
126124
47
127125
}
128126

129-
pub async fn baz() -> u32 {
130-
42
131-
}
132-
133127
pub async fn also_bad(x: &Mutex<u32>) -> u32 {
134128
let first = baz().await;
135129

@@ -164,6 +158,26 @@ mod parking_lot_mutex {
164158
}
165159
}
166160

161+
async fn baz() -> u32 {
162+
42
163+
}
164+
165+
async fn no_await(x: std::sync::Mutex<u32>) {
166+
let mut guard = x.lock().unwrap();
167+
*guard += 1;
168+
}
169+
170+
// FIXME: FP, because the `MutexGuard` is dropped before crossing the await point. This is
171+
// something the needs to be fixed in rustc. There's already drop-tracking, but this is currently
172+
// disabled, see rust-lang/rust#93751. This case isn't picked up by drop-tracking though. If the
173+
// `*guard += 1` is removed it is picked up.
174+
async fn dropped_before_await(x: std::sync::Mutex<u32>) {
175+
let mut guard = x.lock().unwrap();
176+
*guard += 1;
177+
drop(guard);
178+
baz().await;
179+
}
180+
167181
fn main() {
168182
let m = std::sync::Mutex::new(100);
169183
std_mutex::good(&m);

tests/ui/await_holding_lock.stderr

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
error: this `MutexGuard` is held across an `await` point
2-
--> $DIR/await_holding_lock.rs:10:13
2+
--> $DIR/await_holding_lock.rs:11:13
33
|
44
LL | let guard = x.lock().unwrap();
55
| ^^^^^
66
|
77
= note: `-D clippy::await-holding-lock` implied by `-D warnings`
88
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
99
note: these are all the `await` points this lock is held through
10-
--> $DIR/await_holding_lock.rs:10:9
10+
--> $DIR/await_holding_lock.rs:11:9
1111
|
1212
LL | / let guard = x.lock().unwrap();
1313
LL | | baz().await
1414
LL | | }
1515
| |_____^
1616

1717
error: this `MutexGuard` is held across an `await` point
18-
--> $DIR/await_holding_lock.rs:25:13
18+
--> $DIR/await_holding_lock.rs:26:13
1919
|
2020
LL | let guard = x.read().unwrap();
2121
| ^^^^^
2222
|
2323
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
2424
note: these are all the `await` points this lock is held through
25-
--> $DIR/await_holding_lock.rs:25:9
25+
--> $DIR/await_holding_lock.rs:26:9
2626
|
2727
LL | / let guard = x.read().unwrap();
2828
LL | | baz().await
2929
LL | | }
3030
| |_____^
3131

3232
error: this `MutexGuard` is held across an `await` point
33-
--> $DIR/await_holding_lock.rs:30:13
33+
--> $DIR/await_holding_lock.rs:31:13
3434
|
3535
LL | let mut guard = x.write().unwrap();
3636
| ^^^^^^^^^
3737
|
3838
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
3939
note: these are all the `await` points this lock is held through
40-
--> $DIR/await_holding_lock.rs:30:9
40+
--> $DIR/await_holding_lock.rs:31:9
4141
|
4242
LL | / let mut guard = x.write().unwrap();
4343
LL | | baz().await
4444
LL | | }
4545
| |_____^
4646

4747
error: this `MutexGuard` is held across an `await` point
48-
--> $DIR/await_holding_lock.rs:55:13
48+
--> $DIR/await_holding_lock.rs:52:13
4949
|
5050
LL | let guard = x.lock().unwrap();
5151
| ^^^^^
5252
|
5353
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
5454
note: these are all the `await` points this lock is held through
55-
--> $DIR/await_holding_lock.rs:55:9
55+
--> $DIR/await_holding_lock.rs:52:9
5656
|
5757
LL | / let guard = x.lock().unwrap();
5858
LL | |
@@ -64,89 +64,89 @@ LL | | }
6464
| |_____^
6565

6666
error: this `MutexGuard` is held across an `await` point
67-
--> $DIR/await_holding_lock.rs:68:17
67+
--> $DIR/await_holding_lock.rs:65:17
6868
|
6969
LL | let guard = x.lock().unwrap();
7070
| ^^^^^
7171
|
7272
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
7373
note: these are all the `await` points this lock is held through
74-
--> $DIR/await_holding_lock.rs:68:13
74+
--> $DIR/await_holding_lock.rs:65:13
7575
|
7676
LL | / let guard = x.lock().unwrap();
7777
LL | | baz().await
7878
LL | | };
7979
| |_________^
8080

8181
error: this `MutexGuard` is held across an `await` point
82-
--> $DIR/await_holding_lock.rs:80:17
82+
--> $DIR/await_holding_lock.rs:77:17
8383
|
8484
LL | let guard = x.lock().unwrap();
8585
| ^^^^^
8686
|
8787
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
8888
note: these are all the `await` points this lock is held through
89-
--> $DIR/await_holding_lock.rs:80:13
89+
--> $DIR/await_holding_lock.rs:77:13
9090
|
9191
LL | / let guard = x.lock().unwrap();
9292
LL | | baz().await
9393
LL | | }
9494
| |_________^
9595

9696
error: this `MutexGuard` is held across an `await` point
97-
--> $DIR/await_holding_lock.rs:91:13
97+
--> $DIR/await_holding_lock.rs:89:13
9898
|
9999
LL | let guard = x.lock();
100100
| ^^^^^
101101
|
102102
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
103103
note: these are all the `await` points this lock is held through
104-
--> $DIR/await_holding_lock.rs:91:9
104+
--> $DIR/await_holding_lock.rs:89:9
105105
|
106106
LL | / let guard = x.lock();
107107
LL | | baz().await
108108
LL | | }
109109
| |_____^
110110

111111
error: this `MutexGuard` is held across an `await` point
112-
--> $DIR/await_holding_lock.rs:106:13
112+
--> $DIR/await_holding_lock.rs:104:13
113113
|
114114
LL | let guard = x.read();
115115
| ^^^^^
116116
|
117117
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
118118
note: these are all the `await` points this lock is held through
119-
--> $DIR/await_holding_lock.rs:106:9
119+
--> $DIR/await_holding_lock.rs:104:9
120120
|
121121
LL | / let guard = x.read();
122122
LL | | baz().await
123123
LL | | }
124124
| |_____^
125125

126126
error: this `MutexGuard` is held across an `await` point
127-
--> $DIR/await_holding_lock.rs:111:13
127+
--> $DIR/await_holding_lock.rs:109:13
128128
|
129129
LL | let mut guard = x.write();
130130
| ^^^^^^^^^
131131
|
132132
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
133133
note: these are all the `await` points this lock is held through
134-
--> $DIR/await_holding_lock.rs:111:9
134+
--> $DIR/await_holding_lock.rs:109:9
135135
|
136136
LL | / let mut guard = x.write();
137137
LL | | baz().await
138138
LL | | }
139139
| |_____^
140140

141141
error: this `MutexGuard` is held across an `await` point
142-
--> $DIR/await_holding_lock.rs:136:13
142+
--> $DIR/await_holding_lock.rs:130:13
143143
|
144144
LL | let guard = x.lock();
145145
| ^^^^^
146146
|
147147
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
148148
note: these are all the `await` points this lock is held through
149-
--> $DIR/await_holding_lock.rs:136:9
149+
--> $DIR/await_holding_lock.rs:130:9
150150
|
151151
LL | / let guard = x.lock();
152152
LL | |
@@ -158,34 +158,51 @@ LL | | }
158158
| |_____^
159159

160160
error: this `MutexGuard` is held across an `await` point
161-
--> $DIR/await_holding_lock.rs:149:17
161+
--> $DIR/await_holding_lock.rs:143:17
162162
|
163163
LL | let guard = x.lock();
164164
| ^^^^^
165165
|
166166
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
167167
note: these are all the `await` points this lock is held through
168-
--> $DIR/await_holding_lock.rs:149:13
168+
--> $DIR/await_holding_lock.rs:143:13
169169
|
170170
LL | / let guard = x.lock();
171171
LL | | baz().await
172172
LL | | };
173173
| |_________^
174174

175175
error: this `MutexGuard` is held across an `await` point
176-
--> $DIR/await_holding_lock.rs:161:17
176+
--> $DIR/await_holding_lock.rs:155:17
177177
|
178178
LL | let guard = x.lock();
179179
| ^^^^^
180180
|
181181
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
182182
note: these are all the `await` points this lock is held through
183-
--> $DIR/await_holding_lock.rs:161:13
183+
--> $DIR/await_holding_lock.rs:155:13
184184
|
185185
LL | / let guard = x.lock();
186186
LL | | baz().await
187187
LL | | }
188188
| |_________^
189189

190-
error: aborting due to 12 previous errors
190+
error: this `MutexGuard` is held across an `await` point
191+
--> $DIR/await_holding_lock.rs:175:9
192+
|
193+
LL | let mut guard = x.lock().unwrap();
194+
| ^^^^^^^^^
195+
|
196+
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
197+
note: these are all the `await` points this lock is held through
198+
--> $DIR/await_holding_lock.rs:175:5
199+
|
200+
LL | / let mut guard = x.lock().unwrap();
201+
LL | | *guard += 1;
202+
LL | | drop(guard);
203+
LL | | baz().await;
204+
LL | | }
205+
| |_^
206+
207+
error: aborting due to 13 previous errors
191208

0 commit comments

Comments
 (0)