From 5f56956b3c7edb9801585850d1f41b0aeb1888ff Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Mon, 1 May 2023 22:22:43 -0400 Subject: [PATCH 1/2] implement `Sync` for `mpsc::Sender` --- library/std/src/sync/mpsc/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/sync/mpsc/mod.rs b/library/std/src/sync/mpsc/mod.rs index 0e0c87d1c74..71ff4237d00 100644 --- a/library/std/src/sync/mpsc/mod.rs +++ b/library/std/src/sync/mpsc/mod.rs @@ -347,8 +347,8 @@ pub struct Sender { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl Send for Sender {} -#[stable(feature = "rust1", since = "1.0.0")] -impl !Sync for Sender {} +#[stable(feature = "mpsc_sender_sync", since = "CURRENT_RUSTC_VERSION")] +unsafe impl Sync for Sender {} /// The sending-half of Rust's synchronous [`sync_channel`] type. /// From 4ceca095869244ef3026fd90e2c5e10216be3fe7 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Sat, 10 Jun 2023 19:05:07 -0400 Subject: [PATCH 2/2] update failing ui tests --- ...e-70935-complex-spans.drop_tracking.stderr | 31 ++++++++++++------- ...935-complex-spans.drop_tracking_mir.stderr | 31 ++++++++++++------- ...0935-complex-spans.no_drop_tracking.stderr | 16 +++++----- .../async-await/issue-70935-complex-spans.rs | 17 ++++++---- tests/ui/closures/closure-move-sync.rs | 6 ---- tests/ui/closures/closure-move-sync.stderr | 20 +----------- tests/ui/stdlib-unit-tests/not-sync.rs | 2 -- tests/ui/stdlib-unit-tests/not-sync.stderr | 15 +-------- 8 files changed, 59 insertions(+), 79 deletions(-) diff --git a/tests/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr b/tests/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr index 721234aa4a7..f80bb4242aa 100644 --- a/tests/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr +++ b/tests/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr @@ -1,18 +1,25 @@ -error[E0277]: `Sender` cannot be shared between threads safely - --> $DIR/issue-70935-complex-spans.rs:13:45 +error[E0277]: `*mut ()` cannot be shared between threads safely + --> $DIR/issue-70935-complex-spans.rs:18:23 | -LL | fn foo(tx: std::sync::mpsc::Sender) -> impl Future + Send { - | ^^^^^^^^^^^^^^^^^^ `Sender` cannot be shared between threads safely +LL | fn foo(x: NotSync) -> impl Future + Send { + | ^^^^^^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely | - = help: the trait `Sync` is not implemented for `Sender` - = note: required for `&Sender` to implement `Send` + = help: within `NotSync`, the trait `Sync` is not implemented for `*mut ()` +note: required because it appears within the type `PhantomData<*mut ()>` + --> $SRC_DIR/core/src/marker.rs:LL:COL +note: required because it appears within the type `NotSync` + --> $DIR/issue-70935-complex-spans.rs:12:8 + | +LL | struct NotSync(PhantomData<*mut ()>); + | ^^^^^^^ + = note: required for `&NotSync` to implement `Send` note: required because it's used within this closure - --> $DIR/issue-70935-complex-spans.rs:17:13 + --> $DIR/issue-70935-complex-spans.rs:22:13 | -LL | baz(|| async{ +LL | baz(|| async { | ^^ note: required because it's used within this `async fn` body - --> $DIR/issue-70935-complex-spans.rs:10:67 + --> $DIR/issue-70935-complex-spans.rs:15:67 | LL | async fn baz(_c: impl FnMut() -> T) where T: Future { | ___________________________________________________________________^ @@ -20,11 +27,11 @@ LL | | } | |_^ = note: required because it captures the following types: `ResumeTy`, `impl Future`, `()` note: required because it's used within this `async` block - --> $DIR/issue-70935-complex-spans.rs:16:5 + --> $DIR/issue-70935-complex-spans.rs:21:5 | LL | / async move { -LL | | baz(|| async{ -LL | | foo(tx.clone()); +LL | | baz(|| async { +LL | | foo(x.clone()); LL | | }).await; LL | | } | |_____^ diff --git a/tests/ui/async-await/issue-70935-complex-spans.drop_tracking_mir.stderr b/tests/ui/async-await/issue-70935-complex-spans.drop_tracking_mir.stderr index c636be15a58..eb9d93e229f 100644 --- a/tests/ui/async-await/issue-70935-complex-spans.drop_tracking_mir.stderr +++ b/tests/ui/async-await/issue-70935-complex-spans.drop_tracking_mir.stderr @@ -1,18 +1,25 @@ -error[E0277]: `Sender` cannot be shared between threads safely - --> $DIR/issue-70935-complex-spans.rs:13:45 +error[E0277]: `*mut ()` cannot be shared between threads safely + --> $DIR/issue-70935-complex-spans.rs:18:23 | -LL | fn foo(tx: std::sync::mpsc::Sender) -> impl Future + Send { - | ^^^^^^^^^^^^^^^^^^ `Sender` cannot be shared between threads safely +LL | fn foo(x: NotSync) -> impl Future + Send { + | ^^^^^^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely | - = help: the trait `Sync` is not implemented for `Sender` - = note: required for `&Sender` to implement `Send` + = help: within `NotSync`, the trait `Sync` is not implemented for `*mut ()` +note: required because it appears within the type `PhantomData<*mut ()>` + --> $SRC_DIR/core/src/marker.rs:LL:COL +note: required because it appears within the type `NotSync` + --> $DIR/issue-70935-complex-spans.rs:12:8 + | +LL | struct NotSync(PhantomData<*mut ()>); + | ^^^^^^^ + = note: required for `&NotSync` to implement `Send` note: required because it's used within this closure - --> $DIR/issue-70935-complex-spans.rs:17:13 + --> $DIR/issue-70935-complex-spans.rs:22:13 | -LL | baz(|| async{ +LL | baz(|| async { | ^^ note: required because it's used within this `async fn` body - --> $DIR/issue-70935-complex-spans.rs:10:67 + --> $DIR/issue-70935-complex-spans.rs:15:67 | LL | async fn baz(_c: impl FnMut() -> T) where T: Future { | ___________________________________________________________________^ @@ -20,11 +27,11 @@ LL | | } | |_^ = note: required because it captures the following types: `impl Future` note: required because it's used within this `async` block - --> $DIR/issue-70935-complex-spans.rs:16:5 + --> $DIR/issue-70935-complex-spans.rs:21:5 | LL | / async move { -LL | | baz(|| async{ -LL | | foo(tx.clone()); +LL | | baz(|| async { +LL | | foo(x.clone()); LL | | }).await; LL | | } | |_____^ diff --git a/tests/ui/async-await/issue-70935-complex-spans.no_drop_tracking.stderr b/tests/ui/async-await/issue-70935-complex-spans.no_drop_tracking.stderr index ef0e182e515..d8ef6a5eedb 100644 --- a/tests/ui/async-await/issue-70935-complex-spans.no_drop_tracking.stderr +++ b/tests/ui/async-await/issue-70935-complex-spans.no_drop_tracking.stderr @@ -1,21 +1,21 @@ error: future cannot be sent between threads safely - --> $DIR/issue-70935-complex-spans.rs:13:45 + --> $DIR/issue-70935-complex-spans.rs:18:23 | -LL | fn foo(tx: std::sync::mpsc::Sender) -> impl Future + Send { - | ^^^^^^^^^^^^^^^^^^ future created by async block is not `Send` +LL | fn foo(x: NotSync) -> impl Future + Send { + | ^^^^^^^^^^^^^^^^^^ future created by async block is not `Send` | - = help: the trait `Sync` is not implemented for `Sender` + = help: within `NotSync`, the trait `Sync` is not implemented for `*mut ()` note: future is not `Send` as this value is used across an await - --> $DIR/issue-70935-complex-spans.rs:19:12 + --> $DIR/issue-70935-complex-spans.rs:24:12 | -LL | baz(|| async{ +LL | baz(|| async { | _____________- -LL | | foo(tx.clone()); +LL | | foo(x.clone()); LL | | }).await; | | - ^^^^^- the value is later dropped here | | | | | |_________| await occurs here, with the value maybe used later - | has type `[closure@$DIR/issue-70935-complex-spans.rs:17:13: 17:15]` which is not `Send` + | has type `[closure@$DIR/issue-70935-complex-spans.rs:22:13: 22:15]` which is not `Send` error: aborting due to previous error diff --git a/tests/ui/async-await/issue-70935-complex-spans.rs b/tests/ui/async-await/issue-70935-complex-spans.rs index 78625bd393d..9ebde1d3977 100644 --- a/tests/ui/async-await/issue-70935-complex-spans.rs +++ b/tests/ui/async-await/issue-70935-complex-spans.rs @@ -6,16 +6,21 @@ // with newlines which lead complex diagnostics. use std::future::Future; +use std::marker::PhantomData; + +#[derive(Clone)] +struct NotSync(PhantomData<*mut ()>); +unsafe impl Send for NotSync {} async fn baz(_c: impl FnMut() -> T) where T: Future { } -fn foo(tx: std::sync::mpsc::Sender) -> impl Future + Send { +fn foo(x: NotSync) -> impl Future + Send { //[no_drop_tracking]~^ ERROR future cannot be sent between threads safely - //[drop_tracking,drop_tracking_mir]~^^ ERROR `Sender` cannot be shared between threads + //[drop_tracking,drop_tracking_mir]~^^ ERROR `*mut ()` cannot be shared between threads async move { - baz(|| async{ - foo(tx.clone()); + baz(|| async { + foo(x.clone()); }).await; } } @@ -24,6 +29,6 @@ fn bar(_s: impl Future + Send) { } fn main() { - let (tx, _rx) = std::sync::mpsc::channel(); - bar(foo(tx)); + let x = NotSync(PhantomData); + bar(foo(x)); } diff --git a/tests/ui/closures/closure-move-sync.rs b/tests/ui/closures/closure-move-sync.rs index ea2d1434c4a..3ee2b35f59f 100644 --- a/tests/ui/closures/closure-move-sync.rs +++ b/tests/ui/closures/closure-move-sync.rs @@ -13,10 +13,4 @@ fn bar() { t.join().unwrap(); } -fn foo() { - let (tx, _rx) = channel(); - thread::spawn(|| tx.send(()).unwrap()); - //~^ ERROR `Sender<()>` cannot be shared between threads safely -} - fn main() {} diff --git a/tests/ui/closures/closure-move-sync.stderr b/tests/ui/closures/closure-move-sync.stderr index 64e3b51ea71..aee903ac950 100644 --- a/tests/ui/closures/closure-move-sync.stderr +++ b/tests/ui/closures/closure-move-sync.stderr @@ -20,24 +20,6 @@ LL | let t = thread::spawn(|| { note: required by a bound in `spawn` --> $SRC_DIR/std/src/thread/mod.rs:LL:COL -error[E0277]: `Sender<()>` cannot be shared between threads safely - --> $DIR/closure-move-sync.rs:18:19 - | -LL | thread::spawn(|| tx.send(()).unwrap()); - | ------------- ^^^^^^^^^^^^^^^^^^^^^^^ `Sender<()>` cannot be shared between threads safely - | | - | required by a bound introduced by this call - | - = help: the trait `Sync` is not implemented for `Sender<()>` - = note: required for `&Sender<()>` to implement `Send` -note: required because it's used within this closure - --> $DIR/closure-move-sync.rs:18:19 - | -LL | thread::spawn(|| tx.send(()).unwrap()); - | ^^ -note: required by a bound in `spawn` - --> $SRC_DIR/std/src/thread/mod.rs:LL:COL - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/stdlib-unit-tests/not-sync.rs b/tests/ui/stdlib-unit-tests/not-sync.rs index f4648994fa9..e9395ae9e81 100644 --- a/tests/ui/stdlib-unit-tests/not-sync.rs +++ b/tests/ui/stdlib-unit-tests/not-sync.rs @@ -17,6 +17,4 @@ fn main() { test::>(); //~^ ERROR `std::sync::mpsc::Receiver` cannot be shared between threads safely [E0277] - test::>(); - //~^ ERROR `Sender` cannot be shared between threads safely [E0277] } diff --git a/tests/ui/stdlib-unit-tests/not-sync.stderr b/tests/ui/stdlib-unit-tests/not-sync.stderr index 4e34e10e377..b9a266e4eb9 100644 --- a/tests/ui/stdlib-unit-tests/not-sync.stderr +++ b/tests/ui/stdlib-unit-tests/not-sync.stderr @@ -65,19 +65,6 @@ note: required by a bound in `test` LL | fn test() {} | ^^^^ required by this bound in `test` -error[E0277]: `Sender` cannot be shared between threads safely - --> $DIR/not-sync.rs:20:12 - | -LL | test::>(); - | ^^^^^^^^^^^ `Sender` cannot be shared between threads safely - | - = help: the trait `Sync` is not implemented for `Sender` -note: required by a bound in `test` - --> $DIR/not-sync.rs:5:12 - | -LL | fn test() {} - | ^^^^ required by this bound in `test` - -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0277`.