forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix capture analysis for by-move closure bodies
- Loading branch information
1 parent
88c2f4f
commit a1a1f41
Showing
5 changed files
with
239 additions
and
16 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,89 @@ | ||
#![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,80 @@ | ||
//@ aux-build:block-on.rs | ||
//@ edition:2021 | ||
//@ run-pass | ||
//@ check-run-results | ||
|
||
#![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) |