This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +58
-0
lines changed
src/test/ui/lint/must_not_suspend Expand file tree Collapse file tree 5 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 297
297
#![ feature( maybe_uninit_slice) ]
298
298
#![ feature( maybe_uninit_uninit_array) ]
299
299
#![ feature( min_specialization) ]
300
+ #![ cfg_attr( not( bootstrap) , feature( must_not_suspend) ) ]
300
301
#![ feature( needs_panic_runtime) ]
301
302
#![ feature( negative_impls) ]
302
303
#![ feature( never_type) ]
Original file line number Diff line number Diff line change @@ -188,6 +188,12 @@ unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
188
188
/// [`lock`]: Mutex::lock
189
189
/// [`try_lock`]: Mutex::try_lock
190
190
#[ must_use = "if unused the Mutex will immediately unlock" ]
191
+ #[ cfg_attr(
192
+ not( bootstrap) ,
193
+ must_not_suspend = "Holding a MutexGuard across suspend \
194
+ points can cause deadlocks, delays, \
195
+ and cause Future's to not implement `Send`"
196
+ ) ]
191
197
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
192
198
pub struct MutexGuard < ' a , T : ?Sized + ' a > {
193
199
lock : & ' a Mutex < T > ,
Original file line number Diff line number Diff line change @@ -95,6 +95,12 @@ unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
95
95
/// [`read`]: RwLock::read
96
96
/// [`try_read`]: RwLock::try_read
97
97
#[ must_use = "if unused the RwLock will immediately unlock" ]
98
+ #[ cfg_attr(
99
+ not( bootstrap) ,
100
+ must_not_suspend = "Holding a RwLockReadGuard across suspend \
101
+ points can cause deadlocks, delays, \
102
+ and cause Future's to not implement `Send`"
103
+ ) ]
98
104
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
99
105
pub struct RwLockReadGuard < ' a , T : ?Sized + ' a > {
100
106
lock : & ' a RwLock < T > ,
@@ -115,6 +121,12 @@ unsafe impl<T: ?Sized + Sync> Sync for RwLockReadGuard<'_, T> {}
115
121
/// [`write`]: RwLock::write
116
122
/// [`try_write`]: RwLock::try_write
117
123
#[ must_use = "if unused the RwLock will immediately unlock" ]
124
+ #[ cfg_attr(
125
+ not( bootstrap) ,
126
+ must_not_suspend = "Holding a RwLockWriteGuard across suspend \
127
+ points can cause deadlocks, delays, \
128
+ and cause Future's to not implement `Send`"
129
+ ) ]
118
130
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
119
131
pub struct RwLockWriteGuard < ' a , T : ?Sized + ' a > {
120
132
lock : & ' a RwLock < T > ,
Original file line number Diff line number Diff line change
1
+ // edition:2018
2
+ #![ feature( must_not_suspend) ]
3
+ #![ deny( must_not_suspend) ]
4
+
5
+ async fn other ( ) { }
6
+
7
+ pub async fn uhoh ( m : std:: sync:: Mutex < ( ) > ) {
8
+ let _guard = m. lock ( ) . unwrap ( ) ; //~ ERROR `MutexGuard` held across
9
+ other ( ) . await ;
10
+ }
11
+
12
+ fn main ( ) {
13
+ }
Original file line number Diff line number Diff line change
1
+ error: `MutexGuard` held across a suspend point, but should not be
2
+ --> $DIR/mutex.rs:8:9
3
+ |
4
+ LL | let _guard = m.lock().unwrap();
5
+ | ^^^^^^
6
+ LL | other().await;
7
+ | ------------- the value is held across this suspend point
8
+ |
9
+ note: the lint level is defined here
10
+ --> $DIR/mutex.rs:3:9
11
+ |
12
+ LL | #![deny(must_not_suspend)]
13
+ | ^^^^^^^^^^^^^^^^
14
+ note: Holding a MutexGuard across suspend points can cause deadlocks, delays, and cause Future's to not implement `Send`
15
+ --> $DIR/mutex.rs:8:9
16
+ |
17
+ LL | let _guard = m.lock().unwrap();
18
+ | ^^^^^^
19
+ help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point
20
+ --> $DIR/mutex.rs:8:9
21
+ |
22
+ LL | let _guard = m.lock().unwrap();
23
+ | ^^^^^^
24
+
25
+ error: aborting due to previous error
26
+
You can’t perform that action at this time.
0 commit comments