Skip to content

Commit 1671954

Browse files
authored
Rollup merge of rust-lang#63331 - gorup:conditionalinit, r=cramertj
Test conditional initialization validation in async fns r? @cramertj Per [paper doc](https://paper.dropbox.com/doc/async.await-Call-for-Tests--AiWF2Nt8tgDiA70qFI~oiLOOAg-nMyZGrra7dz9KcFRMLKJy) calling for async/.await tests, tests are desired for conditionally initialized local variables. This PR hopes to provide tests for that. rust-lang#63294 seems to be tracking the items from the paper doc that this PR is related to rust-lang#62121 is an open issue asking for more async/.await tests that this relates to --- :+1: executed 2 new tests :+1: tidy
2 parents bcf5287 + 811c304 commit 1671954

3 files changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
// edition:2018
3+
// compile-flags: --crate-type lib
4+
5+
#![feature(async_await)]
6+
7+
async fn conditional_and_guaranteed_initialization(x: usize) -> usize {
8+
let y;
9+
if x > 5 {
10+
y = echo(10).await;
11+
} else {
12+
y = get_something().await;
13+
}
14+
y
15+
}
16+
17+
async fn echo(x: usize) -> usize { x }
18+
async fn get_something() -> usize { 10 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-fail
2+
// edition:2018
3+
// compile-flags: --crate-type lib
4+
5+
#![feature(async_await)]
6+
7+
async fn no_non_guaranteed_initialization(x: usize) -> usize {
8+
let y;
9+
if x > 5 {
10+
y = echo(10).await;
11+
}
12+
y
13+
//~^ use of possibly uninitialized variable: `y`
14+
}
15+
16+
async fn echo(x: usize) -> usize { x + 1 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0381]: use of possibly uninitialized variable: `y`
2+
--> $DIR/no-non-guaranteed-initialization.rs:12:5
3+
|
4+
LL | y
5+
| ^ use of possibly uninitialized `y`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)