-
Notifications
You must be signed in to change notification settings - Fork 13.4k
adjust desugaring for async fn to correct drop order #64525
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
Merged
bors
merged 7 commits into
rust-lang:master
from
nikomatsakis:issue-64512-drop-order-tail-temp
Sep 17, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab1a3f0
add test for drop order of temporary in tail return expression
nikomatsakis 9ae1a66
add regression test for issue-64391
nikomatsakis 00d1590
adjust desugaring for async fn to correct drop order
nikomatsakis 123f129
apply nits from centril
nikomatsakis 0a11e80
introduce `lower_block_expr` convenience function, and use it
nikomatsakis 716b2bc
use drop-temps { .. } pseudo-notation
nikomatsakis 2d8b10f
adjust larger comment to include the body
nikomatsakis 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 hidden or 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 hidden or 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
src/test/ui/async-await/drop-order/drop-order-for-temporary-in-tail-return-expr.rs
This file contains hidden or 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,95 @@ | ||
// aux-build:arc_wake.rs | ||
// edition:2018 | ||
// run-pass | ||
|
||
#![allow(unused_variables)] | ||
|
||
// Test the drop order for parameters relative to local variables and | ||
// temporaries created in the tail return expression of the function | ||
// body. In particular, check that this drop order is the same between | ||
// a `async fn` and an ordinary `fn`. See #64512. | ||
|
||
extern crate arc_wake; | ||
|
||
use arc_wake::ArcWake; | ||
use std::cell::RefCell; | ||
use std::future::Future; | ||
use std::sync::Arc; | ||
use std::rc::Rc; | ||
use std::task::Context; | ||
|
||
struct EmptyWaker; | ||
|
||
impl ArcWake for EmptyWaker { | ||
fn wake(self: Arc<Self>) {} | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
enum DropOrder { | ||
Function, | ||
Val(&'static str), | ||
} | ||
|
||
type DropOrderListPtr = Rc<RefCell<Vec<DropOrder>>>; | ||
|
||
struct D(&'static str, DropOrderListPtr); | ||
|
||
impl Drop for D { | ||
fn drop(&mut self) { | ||
self.1.borrow_mut().push(DropOrder::Val(self.0)); | ||
} | ||
} | ||
|
||
/// Check drop order of temporary "temp" as compared to `x`, `y`, and `z`. | ||
/// | ||
/// Expected order: | ||
/// - `z` | ||
/// - temp | ||
/// - `y` | ||
/// - `x` | ||
async fn foo_async(x: D, _y: D) { | ||
let l = x.1.clone(); | ||
let z = D("z", l.clone()); | ||
l.borrow_mut().push(DropOrder::Function); | ||
helper_async(&D("temp", l)).await | ||
} | ||
|
||
async fn helper_async(v: &D) { } | ||
|
||
fn foo_sync(x: D, _y: D) { | ||
let l = x.1.clone(); | ||
let z = D("z", l.clone()); | ||
l.borrow_mut().push(DropOrder::Function); | ||
helper_sync(&D("temp", l)) | ||
} | ||
|
||
fn helper_sync(v: &D) { } | ||
|
||
fn assert_drop_order_after_poll<Fut: Future<Output = ()>>( | ||
f: impl FnOnce(DropOrderListPtr) -> Fut, | ||
g: impl FnOnce(DropOrderListPtr), | ||
) { | ||
let empty = Arc::new(EmptyWaker); | ||
let waker = ArcWake::into_waker(empty); | ||
let mut cx = Context::from_waker(&waker); | ||
|
||
let actual_order = Rc::new(RefCell::new(Vec::new())); | ||
let mut fut = Box::pin(f(actual_order.clone())); | ||
let r = fut.as_mut().poll(&mut cx); | ||
|
||
assert!(match r { | ||
std::task::Poll::Ready(()) => true, | ||
_ => false, | ||
}); | ||
|
||
let expected_order = Rc::new(RefCell::new(Vec::new())); | ||
g(expected_order.clone()); | ||
|
||
assert_eq!(*actual_order.borrow(), *expected_order.borrow()); | ||
} | ||
|
||
fn main() { | ||
// Free functions (see doc comment on function for what it tests). | ||
assert_drop_order_after_poll(|l| foo_async(D("x", l.clone()), D("_y", l.clone())), | ||
|l| foo_sync(D("x", l.clone()), D("_y", l.clone()))); | ||
} |
This file contains hidden or 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,14 @@ | ||
// Regression test for Issue #64391. The goal here is that this | ||
// function compiles. In the past, due to incorrect drop order for | ||
// temporaries in the tail expression, we failed to compile this | ||
// example. The drop order itself is directly tested in | ||
// `drop-order/drop-order-for-temporary-in-tail-return-expr.rs`. | ||
// | ||
// check-pass | ||
// edition:2018 | ||
|
||
async fn add(x: u32, y: u32) -> u32 { | ||
async { x + y }.await | ||
} | ||
|
||
fn main() { } |
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.
Uh oh!
There was an error while loading. Please reload this page.