Remove async AST node variants for with, for, and def (#6369)

## Summary

Per the suggestion in
https://github.com/astral-sh/ruff/discussions/6183, this PR removes
`AsyncWith`, `AsyncFor`, and `AsyncFunctionDef`, replacing them with an
`is_async` field on the non-async variants of those structs. Unlike an
interpreter, we _generally_ have identical handling for these nodes, so
separating them into distinct variants adds complexity from which we
don't really benefit. This can be seen below, where we get to remove a
_ton_ of code related to adding generic `Any*` wrappers, and a ton of
duplicate branches for these cases.

## Test Plan

`cargo test` is unchanged, apart from parser snapshots.
This commit is contained in:
Charlie Marsh 2023-08-07 12:36:02 -04:00 committed by GitHub
parent c895252aae
commit daefa74e9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
91 changed files with 375 additions and 1478 deletions

View file

@ -23,7 +23,6 @@ mod precedence {
pub(crate) const YIELD_FROM: u8 = 7;
pub(crate) const IF: u8 = 9;
pub(crate) const FOR: u8 = 9;
pub(crate) const ASYNC_FOR: u8 = 9;
pub(crate) const WHILE: u8 = 9;
pub(crate) const RETURN: u8 = 11;
pub(crate) const SLICE: u8 = 13;
@ -204,6 +203,7 @@ impl<'a> Generator<'a> {
match ast {
Stmt::FunctionDef(ast::StmtFunctionDef {
is_async,
name,
parameters,
body,
@ -220,6 +220,9 @@ impl<'a> Generator<'a> {
});
}
statement!({
if *is_async {
self.p("async ");
}
self.p("def ");
self.p_id(name);
if let Some(type_params) = type_params {
@ -239,42 +242,6 @@ impl<'a> Generator<'a> {
self.newlines(2);
}
}
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
name,
parameters,
body,
returns,
decorator_list,
type_params,
..
}) => {
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(&decorator.expression, precedence::MAX);
});
}
statement!({
self.p("async def ");
self.p_id(name);
if let Some(type_params) = type_params {
self.unparse_type_params(type_params);
}
self.p("(");
self.unparse_parameters(parameters);
self.p(")");
if let Some(returns) = returns {
self.p(" -> ");
self.unparse_expr(returns, precedence::MAX);
}
self.p(":");
});
self.body(body);
if self.indent_depth == 0 {
self.newlines(2);
}
}
Stmt::ClassDef(ast::StmtClassDef {
name,
arguments,
@ -400,6 +367,7 @@ impl<'a> Generator<'a> {
});
}
Stmt::For(ast::StmtFor {
is_async,
target,
iter,
body,
@ -407,6 +375,9 @@ impl<'a> Generator<'a> {
..
}) => {
statement!({
if *is_async {
self.p("async ");
}
self.p("for ");
self.unparse_expr(target, precedence::FOR);
self.p(" in ");
@ -421,28 +392,6 @@ impl<'a> Generator<'a> {
self.body(orelse);
}
}
Stmt::AsyncFor(ast::StmtAsyncFor {
target,
iter,
body,
orelse,
..
}) => {
statement!({
self.p("async for ");
self.unparse_expr(target, precedence::ASYNC_FOR);
self.p(" in ");
self.unparse_expr(iter, precedence::MAX);
self.p(":");
});
self.body(body);
if !orelse.is_empty() {
statement!({
self.p("else:");
});
self.body(orelse);
}
}
Stmt::While(ast::StmtWhile {
test,
body,
@ -490,21 +439,17 @@ impl<'a> Generator<'a> {
self.body(&clause.body);
}
}
Stmt::With(ast::StmtWith { items, body, .. }) => {
Stmt::With(ast::StmtWith {
is_async,
items,
body,
..
}) => {
statement!({
self.p("with ");
let mut first = true;
for item in items {
self.p_delim(&mut first, ", ");
self.unparse_with_item(item);
if *is_async {
self.p("async ");
}
self.p(":");
});
self.body(body);
}
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
statement!({
self.p("async with ");
self.p("with ");
let mut first = true;
for item in items {
self.p_delim(&mut first, ", ");