Skip to content

Commit

Permalink
Rollup merge of rust-lang#125527 - programmerjake:patch-2, r=workingj…
Browse files Browse the repository at this point in the history
…ubilee

Add manual Sync impl for ReentrantLockGuard

Fixes: rust-lang#125526
Tracking Issue: rust-lang#121440

this impl is even shown in the summary in the tracking issue, but apparently was forgotten in the actual implementation
  • Loading branch information
matthiaskrgr authored May 25, 2024
2 parents d747148 + f4b9ac6 commit 1d54ba8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/std/src/sync/reentrant_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub struct ReentrantLockGuard<'a, T: ?Sized + 'a> {
#[unstable(feature = "reentrant_lock", issue = "121440")]
impl<T: ?Sized> !Send for ReentrantLockGuard<'_, T> {}

#[unstable(feature = "reentrant_lock", issue = "121440")]
unsafe impl<T: ?Sized + Sync> Sync for ReentrantLockGuard<'_, T> {}

#[unstable(feature = "reentrant_lock", issue = "121440")]
impl<T> ReentrantLock<T> {
/// Creates a new re-entrant lock in an unlocked state ready for use.
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/sync/reentrantlockguard-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(reentrant_lock)]
use std::sync::ReentrantLock;
use std::cell::Cell;

// ReentrantLockGuard<Cell<i32>> must not be Sync, that would be unsound.

fn test_sync<T: Sync>(_t: T) {}

fn main()
{
let m = ReentrantLock::new(Cell::new(0i32));
let guard = m.lock();
test_sync(guard);
//~^ ERROR `Cell<i32>` cannot be shared between threads safely [E0277]
}
20 changes: 20 additions & 0 deletions tests/ui/sync/reentrantlockguard-sync.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0277]: `Cell<i32>` cannot be shared between threads safely
--> $DIR/reentrantlockguard-sync.rs:13:15
|
LL | test_sync(guard);
| --------- ^^^^^ `Cell<i32>` cannot be shared between threads safely
| |
| required by a bound introduced by this call
|
= help: the trait `Sync` is not implemented for `Cell<i32>`, which is required by `ReentrantLockGuard<'_, Cell<i32>>: Sync`
= note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead
= note: required for `ReentrantLockGuard<'_, Cell<i32>>` to implement `Sync`
note: required by a bound in `test_sync`
--> $DIR/reentrantlockguard-sync.rs:7:17
|
LL | fn test_sync<T: Sync>(_t: T) {}
| ^^^^ required by this bound in `test_sync`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit 1d54ba8

Please sign in to comment.