From 998a8777379d8f1cd7b263eff50411ba56f1f9e7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 29 Apr 2017 09:11:51 +0200 Subject: [PATCH 1/2] MutexGuard may be Sync only if T is Sync Also remove some unnecessary unsafe impl from the tests. --- src/libstd/sync/mutex.rs | 8 +++----- src/test/compile-fail/mutexguard-sync.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/test/compile-fail/mutexguard-sync.rs diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index a27f621e6b2ae..bd5a0fd9828c1 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -10,7 +10,6 @@ use cell::UnsafeCell; use fmt; -use marker; use mem; use ops::{Deref, DerefMut}; use ptr; @@ -153,7 +152,9 @@ pub struct MutexGuard<'a, T: ?Sized + 'a> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> !marker::Send for MutexGuard<'a, T> {} +impl<'a, T: ?Sized> !Send for MutexGuard<'a, T> { } +#[stable(feature = "rust1", since = "1.18.0")] +unsafe impl<'a, T: ?Sized + Sync> Sync for MutexGuard<'a, T> { } impl Mutex { /// Creates a new mutex in an unlocked state ready for use. @@ -459,9 +460,6 @@ mod tests { #[derive(Eq, PartialEq, Debug)] struct NonCopy(i32); - unsafe impl Send for Packet {} - unsafe impl Sync for Packet {} - #[test] fn smoke() { let m = Mutex::new(()); diff --git a/src/test/compile-fail/mutexguard-sync.rs b/src/test/compile-fail/mutexguard-sync.rs new file mode 100644 index 0000000000000..861714720c57a --- /dev/null +++ b/src/test/compile-fail/mutexguard-sync.rs @@ -0,0 +1,22 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// MutexGuard> must not be Sync, that would be unsound. +use std::sync::Mutex; +use std::cell::Cell; + +fn test_sync(_t: T) {} + +fn main() +{ + let m = Mutex::new(Cell::new(0i32)); + let guard = m.lock().unwrap(); + test_sync(guard); //~ ERROR the trait bound +} From 23522f6cabf9fc5803411c3dec4a90c56cc3dab2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 29 Apr 2017 19:15:59 +0200 Subject: [PATCH 2/2] need to pick a new feature name --- src/libstd/sync/mutex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index bd5a0fd9828c1..5c3eaa4668d28 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -153,7 +153,7 @@ pub struct MutexGuard<'a, T: ?Sized + 'a> { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T: ?Sized> !Send for MutexGuard<'a, T> { } -#[stable(feature = "rust1", since = "1.18.0")] +#[stable(feature = "mutexguard", since = "1.18.0")] unsafe impl<'a, T: ?Sized + Sync> Sync for MutexGuard<'a, T> { } impl Mutex {