Skip to content

Commit 1d54ba8

Browse files
authored
Rollup merge of #125527 - programmerjake:patch-2, r=workingjubilee
Add manual Sync impl for ReentrantLockGuard Fixes: #125526 Tracking Issue: #121440 this impl is even shown in the summary in the tracking issue, but apparently was forgotten in the actual implementation
2 parents d747148 + f4b9ac6 commit 1d54ba8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

library/std/src/sync/reentrant_lock.rs

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ pub struct ReentrantLockGuard<'a, T: ?Sized + 'a> {
116116
#[unstable(feature = "reentrant_lock", issue = "121440")]
117117
impl<T: ?Sized> !Send for ReentrantLockGuard<'_, T> {}
118118

119+
#[unstable(feature = "reentrant_lock", issue = "121440")]
120+
unsafe impl<T: ?Sized + Sync> Sync for ReentrantLockGuard<'_, T> {}
121+
119122
#[unstable(feature = "reentrant_lock", issue = "121440")]
120123
impl<T> ReentrantLock<T> {
121124
/// Creates a new re-entrant lock in an unlocked state ready for use.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(reentrant_lock)]
2+
use std::sync::ReentrantLock;
3+
use std::cell::Cell;
4+
5+
// ReentrantLockGuard<Cell<i32>> must not be Sync, that would be unsound.
6+
7+
fn test_sync<T: Sync>(_t: T) {}
8+
9+
fn main()
10+
{
11+
let m = ReentrantLock::new(Cell::new(0i32));
12+
let guard = m.lock();
13+
test_sync(guard);
14+
//~^ ERROR `Cell<i32>` cannot be shared between threads safely [E0277]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0277]: `Cell<i32>` cannot be shared between threads safely
2+
--> $DIR/reentrantlockguard-sync.rs:13:15
3+
|
4+
LL | test_sync(guard);
5+
| --------- ^^^^^ `Cell<i32>` cannot be shared between threads safely
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `Sync` is not implemented for `Cell<i32>`, which is required by `ReentrantLockGuard<'_, Cell<i32>>: Sync`
10+
= note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead
11+
= note: required for `ReentrantLockGuard<'_, Cell<i32>>` to implement `Sync`
12+
note: required by a bound in `test_sync`
13+
--> $DIR/reentrantlockguard-sync.rs:7:17
14+
|
15+
LL | fn test_sync<T: Sync>(_t: T) {}
16+
| ^^^^ required by this bound in `test_sync`
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)