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

@ -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()
},
};

View file

@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: f99d8cb29227bfbe1fa07719f655304a9a93fd4715726687ef40c091adbdbad5
// sha3: d713a7771107f8c20353ce5e890fba004b3c5491f513d28e9348a49cd510c59b
use num_bigint::BigInt;
use ruff_text_size::TextSize;
use ruff_python_ast::{self as ast, Ranged, MagicKind};
@ -33081,11 +33081,7 @@ fn __action147<
.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() })
}
}
@ -33306,11 +33302,7 @@ fn __action155<
{
{
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()
}
}
@ -33407,11 +33399,7 @@ fn __action161<
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()
}
}

View file

@ -6,6 +6,7 @@ expression: parse_ast
For(
StmtFor {
range: 0..24,
is_async: false,
target: Name(
ExprName {
range: 4..5,

View file

@ -6,6 +6,7 @@ expression: parse_ast
With(
StmtWith {
range: 0..17,
is_async: false,
items: [
WithItem {
range: 5..11,

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..23,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..29,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..13,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..13,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..32,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..38,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..42,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..52,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..20,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..26,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -7,6 +7,7 @@ Ok(
FunctionDef(
StmtFunctionDef {
range: 0..20,
is_async: false,
decorator_list: [],
name: Identifier {
id: "f",

View file

@ -6,6 +6,7 @@ expression: parse_ast
FunctionDef(
StmtFunctionDef {
range: 0..34,
is_async: false,
decorator_list: [
Decorator {
range: 0..13,

View file

@ -125,6 +125,7 @@ Module(
FunctionDef(
StmtFunctionDef {
range: 566..626,
is_async: false,
decorator_list: [],
name: Identifier {
id: "foo",
@ -199,6 +200,7 @@ Module(
For(
StmtFor {
range: 701..727,
is_async: false,
target: Name(
ExprName {
range: 705..706,

View file

@ -38,6 +38,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 18..44,
is_async: false,
decorator_list: [],
name: Identifier {
id: "__init__",
@ -78,6 +79,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 46..98,
is_async: false,
decorator_list: [],
name: Identifier {
id: "method_with_default",

View file

@ -6,6 +6,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 0..20,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -53,6 +54,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 22..53,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -132,6 +134,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 55..91,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -219,6 +222,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 93..138,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -321,6 +325,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 140..171,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -393,6 +398,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 173..230,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",
@ -496,6 +502,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
FunctionDef(
StmtFunctionDef {
range: 232..273,
is_async: false,
decorator_list: [],
name: Identifier {
id: "func",

View file

@ -6,6 +6,7 @@ expression: parse_ast
FunctionDef(
StmtFunctionDef {
range: 1..49,
is_async: false,
decorator_list: [],
name: Identifier {
id: "args_to_tuple",

View file

@ -1,11 +1,12 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
expression: "parse_suite(source, \"<test>\").unwrap()"
---
[
With(
StmtWith {
range: 0..12,
is_async: false,
items: [
WithItem {
range: 5..6,
@ -33,6 +34,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 13..30,
is_async: false,
items: [
WithItem {
range: 18..24,
@ -68,6 +70,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 31..46,
is_async: false,
items: [
WithItem {
range: 36..37,
@ -108,6 +111,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 47..72,
is_async: false,
items: [
WithItem {
range: 52..58,
@ -164,6 +168,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 73..97,
is_async: false,
items: [
WithItem {
range: 78..91,
@ -214,6 +219,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 98..127,
is_async: false,
items: [
WithItem {
range: 103..121,
@ -272,6 +278,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 128..141,
is_async: false,
items: [
WithItem {
range: 133..135,
@ -297,6 +304,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 142..160,
is_async: false,
items: [
WithItem {
range: 147..154,
@ -330,6 +338,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 161..175,
is_async: false,
items: [
WithItem {
range: 167..168,
@ -357,6 +366,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 176..195,
is_async: false,
items: [
WithItem {
range: 181..189,
@ -392,6 +402,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 196..211,
is_async: false,
items: [
WithItem {
range: 202..203,
@ -419,6 +430,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 212..232,
is_async: false,
items: [
WithItem {
range: 217..226,
@ -462,6 +474,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 233..250,
is_async: false,
items: [
WithItem {
range: 239..243,
@ -502,6 +515,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 251..273,
is_async: false,
items: [
WithItem {
range: 256..267,
@ -554,6 +568,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 274..290,
is_async: false,
items: [
WithItem {
range: 279..284,
@ -593,6 +608,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 291..312,
is_async: false,
items: [
WithItem {
range: 296..306,
@ -640,6 +656,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 313..331,
is_async: false,
items: [
WithItem {
range: 318..325,
@ -688,6 +705,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 332..355,
is_async: false,
items: [
WithItem {
range: 337..349,
@ -744,6 +762,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 356..375,
is_async: false,
items: [
WithItem {
range: 361..369,
@ -783,6 +802,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 376..400,
is_async: false,
items: [
WithItem {
range: 381..394,
@ -830,6 +850,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 401..428,
is_async: false,
items: [
WithItem {
range: 406..422,
@ -898,6 +919,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 429..461,
is_async: false,
items: [
WithItem {
range: 434..455,
@ -974,6 +996,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 462..481,
is_async: false,
items: [
WithItem {
range: 468..474,
@ -1009,6 +1032,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 482..502,
is_async: false,
items: [
WithItem {
range: 488..494,
@ -1044,6 +1068,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 503..530,
is_async: false,
items: [
WithItem {
range: 509..515,
@ -1100,6 +1125,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
With(
StmtWith {
range: 531..559,
is_async: false,
items: [
WithItem {
range: 537..543,