-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Improve async-await/generator obligation errors in some cases #70679
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b129b32
Use inner/outer generator naming instead of first/last
tmandry a40ec13
Add test for #68112 (existing output)
tmandry db0a5a1
Improve span label
tmandry 7127ff3
Don't annotate type when type is opaque
tmandry 6edfd66
Use "generator" instead of "future" when appropriate
tmandry aed7c30
Use clearer message when obligation is caused by await expr
tmandry e340375
Don't double-annotate the same Span
tmandry 8ce334d
rustfmt
tmandry df0a16b
Include type info when available for awaited expr
tmandry 00795a9
Fix style nits
tmandry df64c5d
Incorporate feedback into diagnostics
tmandry 4326d95
Update test after rebase
tmandry 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
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.
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.
seems good to me