Plumb awaitness of for loops

This commit is contained in:
Eric Holk 2023-12-08 14:51:50 -08:00
parent ca2472edd7
commit 0315daafee
5 changed files with 23 additions and 13 deletions

View File

@ -448,7 +448,7 @@ fn is_block_closure_forced(context: &RewriteContext<'_>, expr: &ast::Expr) -> bo
fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool { fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
match expr.kind { match expr.kind {
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop(..) => true, ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop { .. } => true,
ast::ExprKind::Loop(..) if version == Version::Two => true, ast::ExprKind::Loop(..) if version == Version::Two => true,
ast::ExprKind::AddrOf(_, _, ref expr) ast::ExprKind::AddrOf(_, _, ref expr)
| ast::ExprKind::Try(ref expr) | ast::ExprKind::Try(ref expr)
@ -473,7 +473,7 @@ fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
| ast::ExprKind::Block(..) | ast::ExprKind::Block(..)
| ast::ExprKind::While(..) | ast::ExprKind::While(..)
| ast::ExprKind::Loop(..) | ast::ExprKind::Loop(..)
| ast::ExprKind::ForLoop(..) | ast::ExprKind::ForLoop { .. }
| ast::ExprKind::TryBlock(..) => false, | ast::ExprKind::TryBlock(..) => false,
_ => true, _ => true,
} }

View File

@ -3,7 +3,7 @@
use itertools::Itertools; use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, ptr, token}; use rustc_ast::{ast, ptr, token, ForLoopKind};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use crate::chains::rewrite_chain; use crate::chains::rewrite_chain;
@ -134,7 +134,7 @@ pub(crate) fn format_expr(
} }
ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr), ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::If(..) ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..) | ast::ExprKind::ForLoop { .. }
| ast::ExprKind::Loop(..) | ast::ExprKind::Loop(..)
| ast::ExprKind::While(..) => to_control_flow(expr, expr_type) | ast::ExprKind::While(..) => to_control_flow(expr, expr_type)
.and_then(|control_flow| control_flow.rewrite(context, shape)), .and_then(|control_flow| control_flow.rewrite(context, shape)),
@ -682,9 +682,15 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<
expr.span, expr.span,
)) ))
} }
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => { ast::ExprKind::ForLoop {
Some(ControlFlow::new_for(pat, cond, block, label, expr.span)) ref pat,
} ref iter,
ref body,
label,
kind,
} => Some(ControlFlow::new_for(
pat, iter, body, label, expr.span, kind,
)),
ast::ExprKind::Loop(ref block, label, _) => { ast::ExprKind::Loop(ref block, label, _) => {
Some(ControlFlow::new_loop(block, label, expr.span)) Some(ControlFlow::new_loop(block, label, expr.span))
} }
@ -771,6 +777,7 @@ fn new_for(
block: &'a ast::Block, block: &'a ast::Block,
label: Option<ast::Label>, label: Option<ast::Label>,
span: Span, span: Span,
kind: ForLoopKind,
) -> ControlFlow<'a> { ) -> ControlFlow<'a> {
ControlFlow { ControlFlow {
cond: Some(cond), cond: Some(cond),
@ -778,7 +785,10 @@ fn new_for(
else_block: None, else_block: None,
label, label,
pat: Some(pat), pat: Some(pat),
keyword: "for", keyword: match kind {
ForLoopKind::For => "for",
ForLoopKind::ForAwait => "for await",
},
matcher: "", matcher: "",
connector: " in", connector: " in",
allow_single_line: false, allow_single_line: false,
@ -1364,7 +1374,7 @@ pub(crate) fn can_be_overflowed_expr(
|| context.config.overflow_delimited_expr() || context.config.overflow_delimited_expr()
} }
ast::ExprKind::If(..) ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..) | ast::ExprKind::ForLoop { .. }
| ast::ExprKind::Loop(..) | ast::ExprKind::Loop(..)
| ast::ExprKind::While(..) => { | ast::ExprKind::While(..) => {
context.config.combine_control_expr() && context.use_block_indent() && args_len == 1 context.config.combine_control_expr() && context.use_block_indent() && args_len == 1

View File

@ -591,7 +591,7 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
ast::ExprKind::If(..) => false, ast::ExprKind::If(..) => false,
// We do not allow collapsing a block around expression with condition // We do not allow collapsing a block around expression with condition
// to avoid it being cluttered with match arm. // to avoid it being cluttered with match arm.
ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) => false, ast::ExprKind::ForLoop { .. } | ast::ExprKind::While(..) => false,
ast::ExprKind::Loop(..) ast::ExprKind::Loop(..)
| ast::ExprKind::Match(..) | ast::ExprKind::Match(..)
| ast::ExprKind::Block(..) | ast::ExprKind::Block(..)

View File

@ -409,7 +409,7 @@ fn rewrite_last_item_with_overflow(
// When overflowing the expressions which consists of a control flow // When overflowing the expressions which consists of a control flow
// expression, avoid condition to use multi line. // expression, avoid condition to use multi line.
ast::ExprKind::If(..) ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..) | ast::ExprKind::ForLoop { .. }
| ast::ExprKind::Loop(..) | ast::ExprKind::Loop(..)
| ast::ExprKind::While(..) | ast::ExprKind::While(..)
| ast::ExprKind::Match(..) => { | ast::ExprKind::Match(..) => {

View File

@ -295,7 +295,7 @@ pub(crate) fn semicolon_for_stmt(
) -> bool { ) -> bool {
match stmt.kind { match stmt.kind {
ast::StmtKind::Semi(ref expr) => match expr.kind { ast::StmtKind::Semi(ref expr) => match expr.kind {
ast::ExprKind::While(..) | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop(..) => { ast::ExprKind::While(..) | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop { .. } => {
false false
} }
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => { ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
@ -476,7 +476,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::ConstBlock(..) | ast::ExprKind::ConstBlock(..)
| ast::ExprKind::Gen(..) | ast::ExprKind::Gen(..)
| ast::ExprKind::Loop(..) | ast::ExprKind::Loop(..)
| ast::ExprKind::ForLoop(..) | ast::ExprKind::ForLoop { .. }
| ast::ExprKind::TryBlock(..) | ast::ExprKind::TryBlock(..)
| ast::ExprKind::Match(..) => repr.contains('\n'), | ast::ExprKind::Match(..) => repr.contains('\n'),
ast::ExprKind::Paren(ref expr) ast::ExprKind::Paren(ref expr)