-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 #123349 - compiler-errors:async-closure-captures, r=o…
…li-obk Fix capture analysis for by-move closure bodies The check we were doing to figure out if a coroutine was borrowing from its parent coroutine-closure was flat-out wrong -- a misunderstanding of mine of the way that `tcx.closure_captures` represents its captures. Fixes #123251 (the miri/ui test I added should more than cover that issue) r? `@oli-obk` -- I recognize that this PR may be underdocumented, so please ask me what I should explain further.
- Loading branch information
Showing
5 changed files
with
311 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Same as rustc's `tests/ui/async-await/async-closures/captures.rs`, keep in sync | ||
|
||
#![feature(async_closure, noop_waker)] | ||
|
||
use std::future::Future; | ||
use std::pin::pin; | ||
use std::task::*; | ||
|
||
pub fn block_on<T>(fut: impl Future<Output = T>) -> T { | ||
let mut fut = pin!(fut); | ||
let ctx = &mut Context::from_waker(Waker::noop()); | ||
|
||
loop { | ||
match fut.as_mut().poll(ctx) { | ||
Poll::Pending => {} | ||
Poll::Ready(t) => break t, | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
block_on(async_main()); | ||
} | ||
|
||
async fn call<T>(f: &impl async Fn() -> T) -> T { | ||
f().await | ||
} | ||
|
||
async fn call_once<T>(f: impl async FnOnce() -> T) -> T { | ||
f().await | ||
} | ||
|
||
#[derive(Debug)] | ||
#[allow(unused)] | ||
struct Hello(i32); | ||
|
||
async fn async_main() { | ||
// Capture something by-ref | ||
{ | ||
let x = Hello(0); | ||
let c = async || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
|
||
let x = &Hello(1); | ||
let c = async || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something and consume it (force to `AsyncFnOnce`) | ||
{ | ||
let x = Hello(2); | ||
let c = async || { | ||
println!("{x:?}"); | ||
drop(x); | ||
}; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something with `move`, don't consume it | ||
{ | ||
let x = Hello(3); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
|
||
let x = &Hello(4); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something with `move`, also consume it (so `AsyncFnOnce`) | ||
{ | ||
let x = Hello(5); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
drop(x); | ||
}; | ||
call_once(c).await; | ||
} | ||
} |
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,10 @@ | ||
Hello(0) | ||
Hello(0) | ||
Hello(1) | ||
Hello(1) | ||
Hello(2) | ||
Hello(3) | ||
Hello(3) | ||
Hello(4) | ||
Hello(4) | ||
Hello(5) |
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,82 @@ | ||
//@ aux-build:block-on.rs | ||
//@ edition:2021 | ||
//@ run-pass | ||
//@ check-run-results | ||
|
||
// Same as miri's `tests/pass/async-closure-captures.rs`, keep in sync | ||
|
||
#![feature(async_closure)] | ||
|
||
extern crate block_on; | ||
|
||
fn main() { | ||
block_on::block_on(async_main()); | ||
} | ||
|
||
async fn call<T>(f: &impl async Fn() -> T) -> T { | ||
f().await | ||
} | ||
|
||
async fn call_once<T>(f: impl async FnOnce() -> T) -> T { | ||
f().await | ||
} | ||
|
||
#[derive(Debug)] | ||
#[allow(unused)] | ||
struct Hello(i32); | ||
|
||
async fn async_main() { | ||
// Capture something by-ref | ||
{ | ||
let x = Hello(0); | ||
let c = async || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
|
||
let x = &Hello(1); | ||
let c = async || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something and consume it (force to `AsyncFnOnce`) | ||
{ | ||
let x = Hello(2); | ||
let c = async || { | ||
println!("{x:?}"); | ||
drop(x); | ||
}; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something with `move`, don't consume it | ||
{ | ||
let x = Hello(3); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
|
||
let x = &Hello(4); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
}; | ||
call(&c).await; | ||
call_once(c).await; | ||
} | ||
|
||
// Capture something with `move`, also consume it (so `AsyncFnOnce`) | ||
{ | ||
let x = Hello(5); | ||
let c = async move || { | ||
println!("{x:?}"); | ||
drop(x); | ||
}; | ||
call_once(c).await; | ||
} | ||
} |
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,10 @@ | ||
Hello(0) | ||
Hello(0) | ||
Hello(1) | ||
Hello(1) | ||
Hello(2) | ||
Hello(3) | ||
Hello(3) | ||
Hello(4) | ||
Hello(4) | ||
Hello(5) |