Skip to content

Commit

Permalink
Add test for drop-before-await FP
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Feb 12, 2022
1 parent b63dddb commit 775b795
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
30 changes: 22 additions & 8 deletions tests/ui/await_holding_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate parking_lot;

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

pub async fn bad(x: &Mutex<u32>) -> u32 {
Expand Down Expand Up @@ -45,10 +46,6 @@ mod std_mutex {
47
}

pub async fn baz() -> u32 {
42
}

pub async fn also_bad(x: &Mutex<u32>) -> u32 {
let first = baz().await;

Expand Down Expand Up @@ -85,6 +82,7 @@ mod std_mutex {

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

pub async fn bad(x: &Mutex<u32>) -> u32 {
Expand Down Expand Up @@ -126,10 +124,6 @@ mod parking_lot_mutex {
47
}

pub async fn baz() -> u32 {
42
}

pub async fn also_bad(x: &Mutex<u32>) -> u32 {
let first = baz().await;

Expand Down Expand Up @@ -164,6 +158,26 @@ mod parking_lot_mutex {
}
}

async fn baz() -> u32 {
42
}

async fn no_await(x: std::sync::Mutex<u32>) {
let mut guard = x.lock().unwrap();
*guard += 1;
}

// FIXME: FP, because the `MutexGuard` is dropped before crossing the await point. This is
// something the needs to be fixed in rustc. There's already drop-tracking, but this is currently
// disabled, see rust-lang/rust#93751. This case isn't picked up by drop-tracking though. If the
// `*guard += 1` is removed it is picked up.
async fn dropped_before_await(x: std::sync::Mutex<u32>) {
let mut guard = x.lock().unwrap();
*guard += 1;
drop(guard);
baz().await;
}

fn main() {
let m = std::sync::Mutex::new(100);
std_mutex::good(&m);
Expand Down
67 changes: 42 additions & 25 deletions tests/ui/await_holding_lock.stderr
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:10:13
--> $DIR/await_holding_lock.rs:11:13
|
LL | let guard = x.lock().unwrap();
| ^^^^^
|
= note: `-D clippy::await-holding-lock` implied by `-D warnings`
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:10:9
--> $DIR/await_holding_lock.rs:11:9
|
LL | / let guard = x.lock().unwrap();
LL | | baz().await
LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:25:13
--> $DIR/await_holding_lock.rs:26:13
|
LL | let guard = x.read().unwrap();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:25:9
--> $DIR/await_holding_lock.rs:26:9
|
LL | / let guard = x.read().unwrap();
LL | | baz().await
LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:30:13
--> $DIR/await_holding_lock.rs:31:13
|
LL | let mut guard = x.write().unwrap();
| ^^^^^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:30:9
--> $DIR/await_holding_lock.rs:31:9
|
LL | / let mut guard = x.write().unwrap();
LL | | baz().await
LL | | }
| |_____^

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

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:68:17
--> $DIR/await_holding_lock.rs:65:17
|
LL | let guard = x.lock().unwrap();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:68:13
--> $DIR/await_holding_lock.rs:65:13
|
LL | / let guard = x.lock().unwrap();
LL | | baz().await
LL | | };
| |_________^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:80:17
--> $DIR/await_holding_lock.rs:77:17
|
LL | let guard = x.lock().unwrap();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:80:13
--> $DIR/await_holding_lock.rs:77:13
|
LL | / let guard = x.lock().unwrap();
LL | | baz().await
LL | | }
| |_________^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:91:13
--> $DIR/await_holding_lock.rs:89:13
|
LL | let guard = x.lock();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:91:9
--> $DIR/await_holding_lock.rs:89:9
|
LL | / let guard = x.lock();
LL | | baz().await
LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:106:13
--> $DIR/await_holding_lock.rs:104:13
|
LL | let guard = x.read();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:106:9
--> $DIR/await_holding_lock.rs:104:9
|
LL | / let guard = x.read();
LL | | baz().await
LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:111:13
--> $DIR/await_holding_lock.rs:109:13
|
LL | let mut guard = x.write();
| ^^^^^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:111:9
--> $DIR/await_holding_lock.rs:109:9
|
LL | / let mut guard = x.write();
LL | | baz().await
LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:136:13
--> $DIR/await_holding_lock.rs:130:13
|
LL | let guard = x.lock();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:136:9
--> $DIR/await_holding_lock.rs:130:9
|
LL | / let guard = x.lock();
LL | |
Expand All @@ -158,34 +158,51 @@ LL | | }
| |_____^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:149:17
--> $DIR/await_holding_lock.rs:143:17
|
LL | let guard = x.lock();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:149:13
--> $DIR/await_holding_lock.rs:143:13
|
LL | / let guard = x.lock();
LL | | baz().await
LL | | };
| |_________^

error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:161:17
--> $DIR/await_holding_lock.rs:155:17
|
LL | let guard = x.lock();
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:161:13
--> $DIR/await_holding_lock.rs:155:13
|
LL | / let guard = x.lock();
LL | | baz().await
LL | | }
| |_________^

error: aborting due to 12 previous errors
error: this `MutexGuard` is held across an `await` point
--> $DIR/await_holding_lock.rs:175:9
|
LL | let mut guard = x.lock().unwrap();
| ^^^^^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> $DIR/await_holding_lock.rs:175:5
|
LL | / let mut guard = x.lock().unwrap();
LL | | *guard += 1;
LL | | drop(guard);
LL | | baz().await;
LL | | }
| |_^

error: aborting due to 13 previous errors

0 comments on commit 775b795

Please sign in to comment.