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:
@@ -858,11 +858,7 @@ ForStatement: ast::Stmt = {
|
||||
.end();
|
||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||
let iter = Box::new(iter);
|
||||
if is_async.is_some() {
|
||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
} else {
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
}
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, is_async: is_async.is_some(), range: (location..end_location).into() })
|
||||
},
|
||||
};
|
||||
|
||||
@@ -975,11 +971,7 @@ ExceptClause: ast::ExceptHandler = {
|
||||
WithStatement: ast::Stmt = {
|
||||
<location:@L> <is_async:"async"?> "with" <items:WithItems> ":" <body:Suite> => {
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncWith { items, body, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtWith { items, body, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtWith { items, body, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1014,11 +1006,7 @@ FuncDef: ast::Stmt = {
|
||||
let args = Box::new(args);
|
||||
let returns = r.map(Box::new);
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user