Skip to content

Commit 4ad06d1

Browse files
committed
Auto merge of #118847 - eholk:for-await, r=compiler-errors
Add support for `for await` loops This adds support for `for await` loops. This includes parsing, desugaring in AST->HIR lowering, and adding some support functions to the library. Given a loop like: ```rust for await i in iter { ... } ``` this is desugared to something like: ```rust let mut iter = iter.into_async_iter(); while let Some(i) = loop { match core::pin::Pin::new(&mut iter).poll_next(cx) { Poll::Ready(i) => break i, Poll::Pending => yield, } } { ... } ``` This PR also adds a basic `IntoAsyncIterator` trait. This is partly for symmetry with the way `Iterator` and `IntoIterator` work. The other reason is that for async iterators it's helpful to have a place apart from the data structure being iterated over to store state. `IntoAsyncIterator` gives us a good place to do this. I've gated this feature behind `async_for_loop` and opened #118898 as the feature tracking issue. r? `@compiler-errors`
2 parents 0628454 + 212ea03 commit 4ad06d1

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ where
220220
F: FnMut(&ast::Block, Option<&ast::Label>),
221221
{
222222
if let ast::ExprKind::While(_, loop_block, label)
223-
| ast::ExprKind::ForLoop(_, _, loop_block, label)
223+
| ast::ExprKind::ForLoop {
224+
body: loop_block,
225+
label,
226+
..
227+
}
224228
| ast::ExprKind::Loop(loop_block, label, ..) = &expr.kind
225229
{
226230
func(loop_block, label.as_ref());

clippy_lints/src/redundant_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'ast> Visitor<'ast> for BreakVisitor {
111111
ExprKind::If(_, ref then, Some(ref els)) => self.check_block(then) && self.check_expr(els),
112112
ExprKind::If(_, _, None)
113113
// ignore loops for simplicity
114-
| ExprKind::While(..) | ExprKind::ForLoop(..) | ExprKind::Loop(..) => false,
114+
| ExprKind::While(..) | ExprKind::ForLoop { .. } | ExprKind::Loop(..) => false,
115115
_ => {
116116
walk_expr(self, expr);
117117
return;

clippy_lints/src/suspicious_operation_groupings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ fn ident_difference_expr_with_base_location(
554554
| (Closure(_), Closure(_))
555555
| (Match(_, _), Match(_, _))
556556
| (Loop(_, _, _), Loop(_, _, _))
557-
| (ForLoop(_, _, _, _), ForLoop(_, _, _, _))
557+
| (ForLoop { .. }, ForLoop { .. })
558558
| (While(_, _, _), While(_, _, _))
559559
| (If(_, _, _), If(_, _, _))
560560
| (Let(_, _, _, _), Let(_, _, _, _))

clippy_utils/src/ast_utils.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,22 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
169169
(Let(lp, le, _, _), Let(rp, re, _, _)) => eq_pat(lp, rp) && eq_expr(le, re),
170170
(If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
171171
(While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
172-
(ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
173-
eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
174-
},
172+
(
173+
ForLoop {
174+
pat: lp,
175+
iter: li,
176+
body: lt,
177+
label: ll,
178+
kind: lk,
179+
},
180+
ForLoop {
181+
pat: rp,
182+
iter: ri,
183+
body: rt,
184+
label: rl,
185+
kind: rk,
186+
},
187+
) => eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) && lk == rk,
175188
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll, rl) && eq_block(lt, rt),
176189
(Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
177190
(TryBlock(l), TryBlock(r)) => eq_block(l, r),

clippy_utils/src/sugg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a> Sugg<'a> {
197197
| ast::ExprKind::Continue(..)
198198
| ast::ExprKind::Yield(..)
199199
| ast::ExprKind::Field(..)
200-
| ast::ExprKind::ForLoop(..)
200+
| ast::ExprKind::ForLoop { .. }
201201
| ast::ExprKind::Index(..)
202202
| ast::ExprKind::InlineAsm(..)
203203
| ast::ExprKind::OffsetOf(..)

0 commit comments

Comments
 (0)