Skip to content

Commit f522d88

Browse files
committed
Explicitly opt out of Sync for cell and mpsc types
These types were already `!Sync`, but this improves error messages when they are used in contexts that require `Sync`, aligning them with conventions used with `Rc`, among others.
1 parent 52cb8a9 commit f522d88

File tree

6 files changed

+46
-54
lines changed

6 files changed

+46
-54
lines changed

src/libcore/cell.rs

+6
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ impl<T:Copy> Cell<T> {
241241
#[stable(feature = "rust1", since = "1.0.0")]
242242
unsafe impl<T> Send for Cell<T> where T: Send {}
243243

244+
#[stable(feature = "rust1", since = "1.0.0")]
245+
impl<T> !Sync for Cell<T> {}
246+
244247
#[stable(feature = "rust1", since = "1.0.0")]
245248
impl<T:Copy> Clone for Cell<T> {
246249
#[inline]
@@ -461,6 +464,9 @@ impl<T: ?Sized> RefCell<T> {
461464
#[stable(feature = "rust1", since = "1.0.0")]
462465
unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {}
463466

467+
#[stable(feature = "rust1", since = "1.0.0")]
468+
impl<T: ?Sized> !Sync for RefCell<T> {}
469+
464470
#[stable(feature = "rust1", since = "1.0.0")]
465471
impl<T: Clone> Clone for RefCell<T> {
466472
#[inline]

src/libstd/sync/mpsc/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ pub struct Receiver<T> {
299299
#[stable(feature = "rust1", since = "1.0.0")]
300300
unsafe impl<T: Send> Send for Receiver<T> { }
301301

302+
#[stable(feature = "rust1", since = "1.0.0")]
303+
impl<T> !Sync for Receiver<T> { }
304+
302305
/// An iterator over messages on a receiver, this iterator will block
303306
/// whenever `next` is called, waiting for a new message, and `None` will be
304307
/// returned when the corresponding channel has hung up.
@@ -327,6 +330,9 @@ pub struct Sender<T> {
327330
#[stable(feature = "rust1", since = "1.0.0")]
328331
unsafe impl<T: Send> Send for Sender<T> { }
329332

333+
#[stable(feature = "rust1", since = "1.0.0")]
334+
impl<T> !Sync for Sender<T> { }
335+
330336
/// The sending-half of Rust's synchronous channel type. This half can only be
331337
/// owned by one thread, but it can be cloned to send to other threads.
332338
#[stable(feature = "rust1", since = "1.0.0")]

src/test/compile-fail/comm-not-freeze-receiver.rs

-17
This file was deleted.

src/test/compile-fail/comm-not-freeze.rs

-17
This file was deleted.

src/test/compile-fail/no_share-rc.rs

-20
This file was deleted.

src/test/compile-fail/not-sync.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::cell::{Cell, RefCell};
12+
use std::rc::{Rc, Weak};
13+
use std::sync::mpsc::{Receiver, Sender, SyncSender};
14+
15+
fn test<T: Sync>() {}
16+
17+
fn main() {
18+
test::<Cell<i32>>();
19+
//~^ ERROR marker::Sync` is not implemented for the type `core::cell::Cell<i32>`
20+
test::<RefCell<i32>>();
21+
//~^ ERROR marker::Sync` is not implemented for the type `core::cell::RefCell<i32>`
22+
23+
test::<Rc<i32>>();
24+
//~^ ERROR marker::Sync` is not implemented for the type `alloc::rc::Rc<i32>`
25+
test::<Weak<i32>>();
26+
//~^ ERROR marker::Sync` is not implemented for the type `alloc::rc::Weak<i32>`
27+
28+
test::<Receiver<i32>>();
29+
//~^ ERROR marker::Sync` is not implemented for the type `std::sync::mpsc::Receiver<i32>`
30+
test::<Sender<i32>>();
31+
//~^ ERROR marker::Sync` is not implemented for the type `std::sync::mpsc::Sender<i32>`
32+
test::<SyncSender<i32>>();
33+
//~^ ERROR marker::Sync` is not implemented for the type `std::sync::mpsc::SyncSender<i32>`
34+
}

0 commit comments

Comments
 (0)