Skip to content

Commit 397f4a1

Browse files
committed
Add additional tests and update existing tests
1 parent 97df0d3 commit 397f4a1

File tree

7 files changed

+111
-2
lines changed

7 files changed

+111
-2
lines changed

compiler/rustc_parse/src/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2684,7 +2684,8 @@ impl<'a> Parser<'a> {
26842684

26852685
/// Parses `for await? <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
26862686
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
2687-
let is_await = self.eat_keyword(kw::Await);
2687+
let is_await =
2688+
self.token.uninterpolated_span().at_least_rust_2018() && self.eat_keyword(kw::Await);
26882689

26892690
if is_await {
26902691
self.sess.gated_spans.gate(sym::async_for_loop, self.prev_token.span);

tests/ui/async-await/feature-async-for-loop.rs

+9
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ fn f() {
1111
};
1212
}
1313

14+
#[cfg(FALSE)]
15+
fn g() {
16+
let _ = async {
17+
for await _i in core::async_iter::from_iter(0..3) {
18+
//~^ ERROR `for await` loops are experimental
19+
}
20+
};
21+
}
22+
1423
fn main() {}

tests/ui/async-await/feature-async-for-loop.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ error[E0658]: `for await` loops are experimental
44
LL | for await _i in core::async_iter::from_iter(0..3) {
55
| ^^^^^
66
|
7+
= note: see issue #118898 <https://github.com/rust-lang/rust/issues/118898> for more information
78
= help: add `#![feature(async_for_loop)]` to the crate attributes to enable
89

9-
error: aborting due to 1 previous error
10+
error[E0658]: `for await` loops are experimental
11+
--> $DIR/feature-async-for-loop.rs:17:13
12+
|
13+
LL | for await _i in core::async_iter::from_iter(0..3) {
14+
| ^^^^^
15+
|
16+
= note: see issue #118898 <https://github.com/rust-lang/rust/issues/118898> for more information
17+
= help: add `#![feature(async_for_loop)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
1020

1121
For more information about this error, try `rustc --explain E0658`.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check-pass
2+
3+
#![feature(async_for_loop)]
4+
5+
// Make sure we don't break `for await` loops in the 2015 edition, where `await` was allowed as an
6+
// identifier.
7+
8+
fn main() {
9+
for await in 0..3 {}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// edition: 2021
2+
#![feature(async_iterator, async_iter_from_iter, const_waker, async_for_loop, noop_waker)]
3+
4+
use std::future::Future;
5+
6+
// a test to make sure `for await` consumes the iterator
7+
8+
async fn real_main() {
9+
let iter = core::async_iter::from_iter(0..3);
10+
let mut count = 0;
11+
for await i in iter {
12+
}
13+
// make sure iter has been moved and we can't iterate over it again.
14+
for await i in iter {
15+
//~^ ERROR: use of moved value: `iter`
16+
}
17+
}
18+
19+
fn main() {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0382]: use of moved value: `iter`
2+
--> $DIR/for-await-consumes-iter.rs:14:20
3+
|
4+
LL | let iter = core::async_iter::from_iter(0..3);
5+
| ---- move occurs because `iter` has type `FromIter<std::ops::Range<i32>>`, which does not implement the `Copy` trait
6+
LL | let mut count = 0;
7+
LL | for await i in iter {
8+
| -------------------
9+
| | |
10+
| | value moved here
11+
| inside of this loop
12+
...
13+
LL | for await i in iter {
14+
| ^^^^ value used here after move
15+
|
16+
help: consider cloning the value if the performance cost is acceptable
17+
|
18+
LL | for await i in iter.clone() {
19+
| ++++++++
20+
help: borrow this binding in the pattern to avoid moving the value
21+
|
22+
LL | for await i in ref iter {
23+
| +++
24+
25+
error: aborting due to 1 previous error
26+
27+
For more information about this error, try `rustc --explain E0382`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// run-pass
2+
// edition: 2024
3+
// compile-flags: -Zunstable-options
4+
#![feature(async_iterator, async_iter_from_iter, const_waker, async_for_loop, noop_waker,
5+
gen_blocks)]
6+
7+
use std::future::Future;
8+
9+
async gen fn async_iter() -> i32 {
10+
let iter = core::async_iter::from_iter(0..3);
11+
for await i in iter {
12+
yield i + 1;
13+
}
14+
}
15+
16+
// make sure a simple for await loop works
17+
async fn real_main() {
18+
let mut count = 1;
19+
for await i in async_iter() {
20+
assert_eq!(i, count);
21+
count += 1;
22+
}
23+
assert_eq!(count, 4);
24+
}
25+
26+
fn main() {
27+
let future = real_main();
28+
let waker = std::task::Waker::noop();
29+
let mut cx = &mut core::task::Context::from_waker(&waker);
30+
let mut future = core::pin::pin!(future);
31+
while let core::task::Poll::Pending = future.as_mut().poll(&mut cx) {}
32+
}

0 commit comments

Comments
 (0)