forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#70679 - tmandry:issue-68112, r=nikomatsakis
Improve async-await/generator obligation errors in some cases Fixes rust-lang#68112. This change is best read one commit at a time (I add a test at the beginning and update it in each change after). The `test2` function is a case I found while writing the test that we don't handle with this code yet. I don't attempt to fix it in this PR, but it's a good candidate for future work. r? @davidtwco, @nikomatsakis
- Loading branch information
Showing
18 changed files
with
411 additions
and
106 deletions.
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
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
244 changes: 162 additions & 82 deletions
244
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// edition:2018 | ||
|
||
use std::{ | ||
future::Future, | ||
cell::RefCell, | ||
sync::Arc, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
fn require_send(_: impl Send) {} | ||
|
||
struct Ready<T>(Option<T>); | ||
impl<T> Future for Ready<T> { | ||
type Output = T; | ||
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> { | ||
Poll::Ready(self.0.take().unwrap()) | ||
} | ||
} | ||
fn ready<T>(t: T) -> Ready<T> { | ||
Ready(Some(t)) | ||
} | ||
|
||
fn make_non_send_future1() -> impl Future<Output = Arc<RefCell<i32>>> { | ||
ready(Arc::new(RefCell::new(0))) | ||
} | ||
|
||
fn test1() { | ||
let send_fut = async { | ||
let non_send_fut = make_non_send_future1(); | ||
let _ = non_send_fut.await; | ||
ready(0).await; | ||
}; | ||
require_send(send_fut); | ||
//~^ ERROR future cannot be sent between threads | ||
} | ||
|
||
fn test1_no_let() { | ||
let send_fut = async { | ||
let _ = make_non_send_future1().await; | ||
ready(0).await; | ||
}; | ||
require_send(send_fut); | ||
//~^ ERROR future cannot be sent between threads | ||
} | ||
|
||
async fn ready2<T>(t: T) -> T { t } | ||
fn make_non_send_future2() -> impl Future<Output = Arc<RefCell<i32>>> { | ||
ready2(Arc::new(RefCell::new(0))) | ||
} | ||
|
||
// Ideally this test would have diagnostics similar to the test above, but right | ||
// now it doesn't. | ||
fn test2() { | ||
let send_fut = async { | ||
let non_send_fut = make_non_send_future2(); | ||
let _ = non_send_fut.await; | ||
ready(0).await; | ||
}; | ||
require_send(send_fut); | ||
//~^ ERROR `std::cell::RefCell<i32>` cannot be shared between threads safely | ||
} | ||
|
||
fn main() {} |
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,56 @@ | ||
error: future cannot be sent between threads safely | ||
--> $DIR/issue-68112.rs:34:5 | ||
| | ||
LL | fn require_send(_: impl Send) {} | ||
| ------------ ---- required by this bound in `require_send` | ||
... | ||
LL | require_send(send_fut); | ||
| ^^^^^^^^^^^^ future created by async block is not `Send` | ||
| | ||
= help: the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>` | ||
note: future is not `Send` as it awaits another future which is not `Send` | ||
--> $DIR/issue-68112.rs:31:17 | ||
| | ||
LL | let _ = non_send_fut.await; | ||
| ^^^^^^^^^^^^ await occurs here on type `impl std::future::Future`, which is not `Send` | ||
|
||
error: future cannot be sent between threads safely | ||
--> $DIR/issue-68112.rs:43:5 | ||
| | ||
LL | fn require_send(_: impl Send) {} | ||
| ------------ ---- required by this bound in `require_send` | ||
... | ||
LL | require_send(send_fut); | ||
| ^^^^^^^^^^^^ future created by async block is not `Send` | ||
| | ||
= help: the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>` | ||
note: future is not `Send` as it awaits another future which is not `Send` | ||
--> $DIR/issue-68112.rs:40:17 | ||
| | ||
LL | let _ = make_non_send_future1().await; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `impl std::future::Future`, which is not `Send` | ||
|
||
error[E0277]: `std::cell::RefCell<i32>` cannot be shared between threads safely | ||
--> $DIR/issue-68112.rs:60:5 | ||
| | ||
LL | fn require_send(_: impl Send) {} | ||
| ------------ ---- required by this bound in `require_send` | ||
... | ||
LL | require_send(send_fut); | ||
| ^^^^^^^^^^^^ `std::cell::RefCell<i32>` cannot be shared between threads safely | ||
| | ||
= help: the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>` | ||
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::cell::RefCell<i32>>` | ||
= note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:47:31: 47:36 t:std::sync::Arc<std::cell::RefCell<i32>> {}]` | ||
= note: required because it appears within the type `std::future::from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:47:31: 47:36 t:std::sync::Arc<std::cell::RefCell<i32>> {}]>` | ||
= 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: required because it appears within the type `impl std::future::Future` | ||
= note: required because it appears within the type `{std::future::ResumeTy, impl std::future::Future, (), i32, Ready<i32>}` | ||
= note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:55:26: 59:6 {std::future::ResumeTy, impl std::future::Future, (), i32, Ready<i32>}]` | ||
= note: required because it appears within the type `std::future::from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:55:26: 59:6 {std::future::ResumeTy, impl std::future::Future, (), i32, Ready<i32>}]>` | ||
= note: required because it appears within the type `impl std::future::Future` | ||
|
||
error: aborting due to 3 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
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,56 @@ | ||
#![feature(generators, generator_trait)] | ||
|
||
use std::{ | ||
cell::RefCell, | ||
sync::Arc, | ||
pin::Pin, | ||
ops::{Generator, GeneratorState}, | ||
}; | ||
|
||
pub struct Ready<T>(Option<T>); | ||
impl<T> Generator<()> for Ready<T> { | ||
type Return = T; | ||
type Yield = (); | ||
fn resume(mut self: Pin<&mut Self>, _args: ()) -> GeneratorState<(), T> { | ||
GeneratorState::Complete(self.0.take().unwrap()) | ||
} | ||
} | ||
pub fn make_gen1<T>(t: T) -> Ready<T> { | ||
Ready(Some(t)) | ||
} | ||
|
||
fn require_send(_: impl Send) {} | ||
|
||
fn make_non_send_generator() -> impl Generator<Return = Arc<RefCell<i32>>> { | ||
make_gen1(Arc::new(RefCell::new(0))) | ||
} | ||
|
||
fn test1() { | ||
let send_gen = || { | ||
let _non_send_gen = make_non_send_generator(); | ||
yield; | ||
}; | ||
require_send(send_gen); | ||
//~^ ERROR generator cannot be sent between threads | ||
} | ||
|
||
pub fn make_gen2<T>(t: T) -> impl Generator<Return = T> { | ||
|| { | ||
yield; | ||
t | ||
} | ||
} | ||
fn make_non_send_generator2() -> impl Generator<Return = Arc<RefCell<i32>>> { | ||
make_gen2(Arc::new(RefCell::new(0))) | ||
} | ||
|
||
fn test2() { | ||
let send_gen = || { | ||
let _non_send_gen = make_non_send_generator2(); | ||
yield; | ||
}; | ||
require_send(send_gen); | ||
//~^ ERROR `std::cell::RefCell<i32>` cannot be shared between threads safely | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.