Skip to content

Commit c570941

Browse files
committed
Add test for drop-before-await FP
1 parent c4944fb commit c570941

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
@@ -2,6 +2,7 @@
22

33
// When adding or modifying a test, please do the same for parking_lot::Mutex.
44
mod std_mutex {
5+
use super::baz;
56
use std::sync::{Mutex, RwLock};
67

78
pub async fn bad(x: &Mutex<u32>) -> u32 {
@@ -43,10 +44,6 @@ mod std_mutex {
4344
47
4445
}
4546

46-
pub async fn baz() -> u32 {
47-
42
48-
}
49-
5047
pub async fn also_bad(x: &Mutex<u32>) -> u32 {
5148
let first = baz().await;
5249

@@ -83,6 +80,7 @@ mod std_mutex {
8380

8481
// When adding or modifying a test, please do the same for std::Mutex.
8582
mod parking_lot_mutex {
83+
use super::baz;
8684
use parking_lot::{Mutex, RwLock};
8785

8886
pub async fn bad(x: &Mutex<u32>) -> u32 {
@@ -124,10 +122,6 @@ mod parking_lot_mutex {
124122
47
125123
}
126124

127-
pub async fn baz() -> u32 {
128-
42
129-
}
130-
131125
pub async fn also_bad(x: &Mutex<u32>) -> u32 {
132126
let first = baz().await;
133127

@@ -162,6 +156,26 @@ mod parking_lot_mutex {
162156
}
163157
}
164158

159+
async fn baz() -> u32 {
160+
42
161+
}
162+
163+
async fn no_await(x: std::sync::Mutex<u32>) {
164+
let mut guard = x.lock().unwrap();
165+
*guard += 1;
166+
}
167+
168+
// FIXME: FP, because the `MutexGuard` is dropped before crossing the await point. This is
169+
// something the needs to be fixed in rustc. There's already drop-tracking, but this is currently
170+
// disabled, see rust-lang/rust#93751. This case isn't picked up by drop-tracking though. If the
171+
// `*guard += 1` is removed it is picked up.
172+
async fn dropped_before_await(x: std::sync::Mutex<u32>) {
173+
let mut guard = x.lock().unwrap();
174+
*guard += 1;
175+
drop(guard);
176+
baz().await;
177+
}
178+
165179
fn main() {
166180
let m = std::sync::Mutex::new(100);
167181
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:8:13
2+
--> $DIR/await_holding_lock.rs:9: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:8:9
10+
--> $DIR/await_holding_lock.rs:9: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:23:13
18+
--> $DIR/await_holding_lock.rs:24: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:23:9
25+
--> $DIR/await_holding_lock.rs:24: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:28:13
33+
--> $DIR/await_holding_lock.rs:29: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:28:9
40+
--> $DIR/await_holding_lock.rs:29: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:53:13
48+
--> $DIR/await_holding_lock.rs:50: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:53:9
55+
--> $DIR/await_holding_lock.rs:50: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:66:17
67+
--> $DIR/await_holding_lock.rs:63: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:66:13
74+
--> $DIR/await_holding_lock.rs:63: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:78:17
82+
--> $DIR/await_holding_lock.rs:75: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:78:13
89+
--> $DIR/await_holding_lock.rs:75: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:89:13
97+
--> $DIR/await_holding_lock.rs:87: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:89:9
104+
--> $DIR/await_holding_lock.rs:87: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:104:13
112+
--> $DIR/await_holding_lock.rs:102: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:104:9
119+
--> $DIR/await_holding_lock.rs:102: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:109:13
127+
--> $DIR/await_holding_lock.rs:107: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:109:9
134+
--> $DIR/await_holding_lock.rs:107: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:134:13
142+
--> $DIR/await_holding_lock.rs:128: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:134:9
149+
--> $DIR/await_holding_lock.rs:128: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:147:17
161+
--> $DIR/await_holding_lock.rs:141: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:147:13
168+
--> $DIR/await_holding_lock.rs:141: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:159:17
176+
--> $DIR/await_holding_lock.rs:153: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:159:13
183+
--> $DIR/await_holding_lock.rs:153: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:173: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:173: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)