-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async/await: improve not-send errors, part 2 #65345
Merged
bors
merged 3 commits into
rust-lang:master
from
davidtwco:issue-64130-async-send-sync-error-improvements
Dec 11, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,88 @@ | ||
error[E0277]: `std::rc::Rc<()>` cannot be sent between threads safely | ||
error: future cannot be sent between threads safely | ||
--> $DIR/async-fn-nonsend.rs:50:5 | ||
| | ||
LL | fn assert_send(_: impl Send) {} | ||
| ----------- ---- required by this bound in `assert_send` | ||
... | ||
LL | assert_send(local_dropped_before_await()); | ||
| ^^^^^^^^^^^ `std::rc::Rc<()>` cannot be sent between threads safely | ||
| ^^^^^^^^^^^ future returned by `local_dropped_before_await` is not `Send` | ||
| | ||
= help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<()>` | ||
= note: required because it appears within the type `impl std::fmt::Debug` | ||
= note: required because it appears within the type `{impl std::fmt::Debug, impl std::future::Future, impl std::future::Future, ()}` | ||
= note: required because it appears within the type `[static generator@$DIR/async-fn-nonsend.rs:21:39: 26:2 {impl std::fmt::Debug, impl std::future::Future, impl std::future::Future, ()}]` | ||
= note: required because it appears within the type `std::future::GenFuture<[static generator@$DIR/async-fn-nonsend.rs:21:39: 26:2 {impl std::fmt::Debug, impl std::future::Future, impl std::future::Future, ()}]>` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
note: future is not `Send` as this value is used across an await | ||
--> $DIR/async-fn-nonsend.rs:25:5 | ||
| | ||
LL | let x = non_send(); | ||
| - has type `impl std::fmt::Debug` | ||
LL | drop(x); | ||
LL | fut().await; | ||
| ^^^^^^^^^^^ await occurs here, with `x` maybe used later | ||
LL | } | ||
| - `x` is later dropped here | ||
|
||
error[E0277]: `std::rc::Rc<()>` cannot be sent between threads safely | ||
error: future cannot be sent between threads safely | ||
--> $DIR/async-fn-nonsend.rs:52:5 | ||
| | ||
LL | fn assert_send(_: impl Send) {} | ||
| ----------- ---- required by this bound in `assert_send` | ||
... | ||
LL | assert_send(non_send_temporary_in_match()); | ||
| ^^^^^^^^^^^ `std::rc::Rc<()>` cannot be sent between threads safely | ||
| ^^^^^^^^^^^ future returned by `non_send_temporary_in_match` is not `Send` | ||
| | ||
= help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<()>` | ||
= note: required because it appears within the type `impl std::fmt::Debug` | ||
= note: required because it appears within the type `{impl std::fmt::Debug, std::option::Option<impl std::fmt::Debug>, impl std::future::Future, impl std::future::Future, ()}` | ||
= note: required because it appears within the type `[static generator@$DIR/async-fn-nonsend.rs:28:40: 37:2 {impl std::fmt::Debug, std::option::Option<impl std::fmt::Debug>, impl std::future::Future, impl std::future::Future, ()}]` | ||
= note: required because it appears within the type `std::future::GenFuture<[static generator@$DIR/async-fn-nonsend.rs:28:40: 37:2 {impl std::fmt::Debug, std::option::Option<impl std::fmt::Debug>, impl std::future::Future, impl std::future::Future, ()}]>` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
note: future is not `Send` as this value is used across an await | ||
--> $DIR/async-fn-nonsend.rs:34:20 | ||
| | ||
LL | match Some(non_send()) { | ||
| ---------- has type `impl std::fmt::Debug` | ||
LL | Some(_) => fut().await, | ||
| ^^^^^^^^^^^ await occurs here, with `non_send()` maybe used later | ||
... | ||
LL | } | ||
| - `non_send()` is later dropped here | ||
|
||
error[E0277]: `dyn std::fmt::Write` cannot be sent between threads safely | ||
error: future cannot be sent between threads safely | ||
--> $DIR/async-fn-nonsend.rs:54:5 | ||
| | ||
LL | fn assert_send(_: impl Send) {} | ||
| ----------- ---- required by this bound in `assert_send` | ||
... | ||
LL | assert_send(non_sync_with_method_call()); | ||
| ^^^^^^^^^^^ `dyn std::fmt::Write` cannot be sent between threads safely | ||
| ^^^^^^^^^^^ future returned by `non_sync_with_method_call` is not `Send` | ||
| | ||
= help: the trait `std::marker::Send` is not implemented for `dyn std::fmt::Write` | ||
= note: required because of the requirements on the impl of `std::marker::Send` for `&mut dyn std::fmt::Write` | ||
= note: required because it appears within the type `std::fmt::Formatter<'_>` | ||
= note: required because of the requirements on the impl of `std::marker::Send` for `&mut std::fmt::Formatter<'_>` | ||
= note: required because it appears within the type `for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}` | ||
= note: required because it appears within the type `[static generator@$DIR/async-fn-nonsend.rs:39:38: 45:2 for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}]` | ||
= note: required because it appears within the type `std::future::GenFuture<[static generator@$DIR/async-fn-nonsend.rs:39:38: 45:2 for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}]>` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
note: future is not `Send` as this value is used across an await | ||
--> $DIR/async-fn-nonsend.rs:43:9 | ||
| | ||
LL | let f: &mut std::fmt::Formatter = panic!(); | ||
| - has type `&mut std::fmt::Formatter<'_>` | ||
LL | if non_sync().fmt(f).unwrap() == () { | ||
LL | fut().await; | ||
| ^^^^^^^^^^^ await occurs here, with `f` maybe used later | ||
LL | } | ||
LL | } | ||
| - `f` is later dropped here | ||
|
||
error[E0277]: `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely | ||
error: future cannot be sent between threads safely | ||
--> $DIR/async-fn-nonsend.rs:54:5 | ||
| | ||
LL | fn assert_send(_: impl Send) {} | ||
| ----------- ---- required by this bound in `assert_send` | ||
... | ||
LL | assert_send(non_sync_with_method_call()); | ||
| ^^^^^^^^^^^ `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely | ||
| ^^^^^^^^^^^ future returned by `non_sync_with_method_call` is not `Send` | ||
| | ||
= help: within `std::fmt::ArgumentV1<'_>`, the trait `std::marker::Sync` is not implemented for `*mut (dyn std::ops::Fn() + 'static)` | ||
= note: required because it appears within the type `std::marker::PhantomData<*mut (dyn std::ops::Fn() + 'static)>` | ||
= note: required because it appears within the type `core::fmt::Void` | ||
= note: required because it appears within the type `&core::fmt::Void` | ||
= note: required because it appears within the type `std::fmt::ArgumentV1<'_>` | ||
= note: required because of the requirements on the impl of `std::marker::Send` for `std::slice::Iter<'_, std::fmt::ArgumentV1<'_>>` | ||
= note: required because it appears within the type `std::fmt::Formatter<'_>` | ||
= note: required because of the requirements on the impl of `std::marker::Send` for `&mut std::fmt::Formatter<'_>` | ||
= note: required because it appears within the type `for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}` | ||
= note: required because it appears within the type `[static generator@$DIR/async-fn-nonsend.rs:39:38: 45:2 for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}]` | ||
= note: required because it appears within the type `std::future::GenFuture<[static generator@$DIR/async-fn-nonsend.rs:39:38: 45:2 for<'r, 's> {&'r mut std::fmt::Formatter<'s>, bool, bool, impl std::future::Future, impl std::future::Future, ()}]>` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
note: future is not `Send` as this value is used across an await | ||
--> $DIR/async-fn-nonsend.rs:43:9 | ||
| | ||
LL | let f: &mut std::fmt::Formatter = panic!(); | ||
| - has type `&mut std::fmt::Formatter<'_>` | ||
LL | if non_sync().fmt(f).unwrap() == () { | ||
LL | fut().await; | ||
| ^^^^^^^^^^^ await occurs here, with `f` maybe used later | ||
LL | } | ||
LL | } | ||
| - `f` is later dropped here | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(optin_builtin_traits)] | ||
// edition:2018 | ||
|
||
// This tests the the specialized async-await-specific error when futures don't implement an | ||
// auto trait (which is specifically Sync) due to some type that was captured. | ||
|
||
struct Foo; | ||
|
||
impl !Sync for Foo {} | ||
|
||
fn is_sync<T: Sync>(t: T) { } | ||
|
||
async fn bar() { | ||
let x = Foo; | ||
baz().await; | ||
} | ||
|
||
async fn baz() { } | ||
|
||
fn main() { | ||
is_sync(bar()); | ||
//~^ ERROR future cannot be shared between threads safely | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: future cannot be shared between threads safely | ||
--> $DIR/issue-64130-1-sync.rs:21:5 | ||
| | ||
LL | fn is_sync<T: Sync>(t: T) { } | ||
| ------- ---- required by this bound in `is_sync` | ||
... | ||
LL | is_sync(bar()); | ||
| ^^^^^^^ future returned by `bar` is not `Sync` | ||
| | ||
= help: within `impl std::future::Future`, the trait `std::marker::Sync` is not implemented for `Foo` | ||
note: future is not `Sync` as this value is used across an await | ||
--> $DIR/issue-64130-1-sync.rs:15:5 | ||
| | ||
LL | let x = Foo; | ||
| - has type `Foo` | ||
LL | baz().await; | ||
| ^^^^^^^^^^^ await occurs here, with `x` maybe used later | ||
LL | } | ||
| - `x` is later dropped here | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(optin_builtin_traits)] | ||
// edition:2018 | ||
|
||
// This tests the the specialized async-await-specific error when futures don't implement an | ||
// auto trait (which is specifically Send) due to some type that was captured. | ||
|
||
struct Foo; | ||
|
||
impl !Send for Foo {} | ||
|
||
fn is_send<T: Send>(t: T) { } | ||
|
||
async fn bar() { | ||
let x = Foo; | ||
baz().await; | ||
} | ||
|
||
async fn baz() { } | ||
|
||
fn main() { | ||
is_send(bar()); | ||
//~^ ERROR future cannot be sent between threads safely | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: future cannot be sent between threads safely | ||
--> $DIR/issue-64130-2-send.rs:21:5 | ||
| | ||
LL | fn is_send<T: Send>(t: T) { } | ||
| ------- ---- required by this bound in `is_send` | ||
... | ||
LL | is_send(bar()); | ||
| ^^^^^^^ future returned by `bar` is not `Send` | ||
| | ||
= help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `Foo` | ||
note: future is not `Send` as this value is used across an await | ||
--> $DIR/issue-64130-2-send.rs:15:5 | ||
| | ||
LL | let x = Foo; | ||
| - has type `Foo` | ||
LL | baz().await; | ||
| ^^^^^^^^^^^ await occurs here, with `x` maybe used later | ||
LL | } | ||
| - `x` is later dropped here | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![feature(optin_builtin_traits)] | ||
// edition:2018 | ||
|
||
// This tests the the unspecialized async-await-specific error when futures don't implement an | ||
// auto trait (which is not Send or Sync) due to some type that was captured. | ||
|
||
auto trait Qux { } | ||
|
||
struct Foo; | ||
|
||
impl !Qux for Foo {} | ||
|
||
fn is_qux<T: Qux>(t: T) { } | ||
|
||
async fn bar() { | ||
let x = Foo; | ||
baz().await; | ||
} | ||
|
||
async fn baz() { } | ||
|
||
fn main() { | ||
is_qux(bar()); | ||
//~^ ERROR the trait bound `Foo: Qux` is not satisfied in `impl std::future::Future` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0277]: the trait bound `Foo: Qux` is not satisfied in `impl std::future::Future` | ||
--> $DIR/issue-64130-3-other.rs:23:5 | ||
| | ||
LL | fn is_qux<T: Qux>(t: T) { } | ||
| ------ --- required by this bound in `is_qux` | ||
... | ||
LL | is_qux(bar()); | ||
| ^^^^^^ within `impl std::future::Future`, the trait `Qux` is not implemented for `Foo` | ||
| | ||
= help: the following implementations were found: | ||
<Foo as Qux> | ||
note: future does not implement `Qux` as this value is used across an await | ||
--> $DIR/issue-64130-3-other.rs:17:5 | ||
| | ||
LL | let x = Foo; | ||
| - has type `Foo` | ||
LL | baz().await; | ||
| ^^^^^^^^^^^ await occurs here, with `x` maybe used later | ||
LL | } | ||
| - `x` is later dropped here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// edition:2018 | ||
use std::any::Any; | ||
use std::future::Future; | ||
|
||
struct Client(Box<dyn Any + Send>); | ||
|
||
impl Client { | ||
fn status(&self) -> u16 { | ||
200 | ||
} | ||
} | ||
|
||
async fn get() { } | ||
|
||
pub fn foo() -> impl Future + Send { | ||
//~^ ERROR future cannot be sent between threads safely | ||
let client = Client(Box::new(true)); | ||
async move { | ||
match client.status() { | ||
200 => { | ||
let _x = get().await; | ||
}, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diagnostic is misleading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, because of the
drop(x)
that comes before? I suppose that's true, though it'd ordinarily be quite helpful. Perhaps we can just weaken the language to "x may be dropped here"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think we could adjust the wording to better explain why the
drop
call isn’t enough to avoid the type being held over the await point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider this a bit of a bug, also -- it'd be nice if we were more precise around particularly this case)