mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
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:
parent
c895252aae
commit
daefa74e9a
91 changed files with 375 additions and 1478 deletions
|
@ -45,8 +45,6 @@ impl From<ModExpression> for Mod {
|
|||
pub enum Stmt {
|
||||
#[is(name = "function_def_stmt")]
|
||||
FunctionDef(StmtFunctionDef),
|
||||
#[is(name = "async_function_def_stmt")]
|
||||
AsyncFunctionDef(StmtAsyncFunctionDef),
|
||||
#[is(name = "class_def_stmt")]
|
||||
ClassDef(StmtClassDef),
|
||||
#[is(name = "return_stmt")]
|
||||
|
@ -63,16 +61,12 @@ pub enum Stmt {
|
|||
TypeAlias(StmtTypeAlias),
|
||||
#[is(name = "for_stmt")]
|
||||
For(StmtFor),
|
||||
#[is(name = "async_for_stmt")]
|
||||
AsyncFor(StmtAsyncFor),
|
||||
#[is(name = "while_stmt")]
|
||||
While(StmtWhile),
|
||||
#[is(name = "if_stmt")]
|
||||
If(StmtIf),
|
||||
#[is(name = "with_stmt")]
|
||||
With(StmtWith),
|
||||
#[is(name = "async_with_stmt")]
|
||||
AsyncWith(StmtAsyncWith),
|
||||
#[is(name = "match_stmt")]
|
||||
Match(StmtMatch),
|
||||
#[is(name = "raise_stmt")]
|
||||
|
@ -118,10 +112,15 @@ impl From<StmtLineMagic> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef)
|
||||
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef) and
|
||||
/// [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtFunctionDef {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub decorator_list: Vec<Decorator>,
|
||||
pub name: Identifier,
|
||||
pub type_params: Option<TypeParams>,
|
||||
|
@ -136,24 +135,6 @@ impl From<StmtFunctionDef> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncFunctionDef {
|
||||
pub range: TextRange,
|
||||
pub decorator_list: Vec<Decorator>,
|
||||
pub name: Identifier,
|
||||
pub type_params: Option<TypeParams>,
|
||||
pub parameters: Box<Parameters>,
|
||||
pub returns: Option<Box<Expr>>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncFunctionDef> for Stmt {
|
||||
fn from(payload: StmtAsyncFunctionDef) -> Self {
|
||||
Stmt::AsyncFunctionDef(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtClassDef {
|
||||
|
@ -275,10 +256,15 @@ impl From<StmtAnnAssign> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For)
|
||||
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For) and
|
||||
/// [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtFor {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub target: Box<Expr>,
|
||||
pub iter: Box<Expr>,
|
||||
pub body: Vec<Stmt>,
|
||||
|
@ -291,23 +277,8 @@ impl From<StmtFor> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncFor {
|
||||
pub range: TextRange,
|
||||
pub target: Box<Expr>,
|
||||
pub iter: Box<Expr>,
|
||||
pub body: Vec<Stmt>,
|
||||
pub orelse: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncFor> for Stmt {
|
||||
fn from(payload: StmtAsyncFor) -> Self {
|
||||
Stmt::AsyncFor(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While)
|
||||
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While) and
|
||||
/// [AsyncWhile](https://docs.python.org/3/library/ast.html#ast.AsyncWhile).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtWhile {
|
||||
pub range: TextRange,
|
||||
|
@ -344,10 +315,15 @@ pub struct ElifElseClause {
|
|||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With)
|
||||
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With) and
|
||||
/// [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtWith {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub items: Vec<WithItem>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
@ -358,20 +334,6 @@ impl From<StmtWith> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncWith {
|
||||
pub range: TextRange,
|
||||
pub items: Vec<WithItem>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncWith> for Stmt {
|
||||
fn from(payload: StmtAsyncWith) -> Self {
|
||||
Stmt::AsyncWith(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtMatch {
|
||||
|
@ -2599,11 +2561,6 @@ impl Ranged for crate::nodes::StmtFunctionDef {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncFunctionDef {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtClassDef {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2644,11 +2601,6 @@ impl Ranged for crate::nodes::StmtFor {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncFor {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtWhile {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2669,11 +2621,6 @@ impl Ranged for crate::nodes::StmtWith {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncWith {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtMatch {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2748,7 +2695,6 @@ impl Ranged for crate::Stmt {
|
|||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
Self::FunctionDef(node) => node.range(),
|
||||
Self::AsyncFunctionDef(node) => node.range(),
|
||||
Self::ClassDef(node) => node.range(),
|
||||
Self::Return(node) => node.range(),
|
||||
Self::Delete(node) => node.range(),
|
||||
|
@ -2757,11 +2703,9 @@ impl Ranged for crate::Stmt {
|
|||
Self::AugAssign(node) => node.range(),
|
||||
Self::AnnAssign(node) => node.range(),
|
||||
Self::For(node) => node.range(),
|
||||
Self::AsyncFor(node) => node.range(),
|
||||
Self::While(node) => node.range(),
|
||||
Self::If(node) => node.range(),
|
||||
Self::With(node) => node.range(),
|
||||
Self::AsyncWith(node) => node.range(),
|
||||
Self::Match(node) => node.range(),
|
||||
Self::Raise(node) => node.range(),
|
||||
Self::Try(node) => node.range(),
|
||||
|
@ -3110,8 +3054,7 @@ mod size_assertions {
|
|||
use static_assertions::assert_eq_size;
|
||||
|
||||
assert_eq_size!(Stmt, [u8; 144]);
|
||||
assert_eq_size!(StmtFunctionDef, [u8; 136]);
|
||||
assert_eq_size!(StmtAsyncFunctionDef, [u8; 136]);
|
||||
assert_eq_size!(StmtFunctionDef, [u8; 144]);
|
||||
assert_eq_size!(StmtClassDef, [u8; 104]);
|
||||
assert_eq_size!(StmtTry, [u8; 104]);
|
||||
assert_eq_size!(Expr, [u8; 80]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue