Remove AstNode and AnyNode (#15479)

While looking into potential AST optimizations, I noticed the `AstNode`
trait and `AnyNode` type aren't used anywhere in Ruff or Red Knot. It
looks like they might be historical artifacts of previous ways of
consuming AST nodes?

- `AstNode::cast`, `AstNode::cast_ref`, and `AstNode::can_cast` are not
used anywhere.
- Since `cast_ref` isn't needed anymore, the `Ref` associated type isn't
either.

This is a pure refactoring, with no intended behavior changes.
This commit is contained in:
Douglas Creager 2025-01-17 17:11:00 -05:00 committed by GitHub
parent 8e3633f55a
commit 98ef564170
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 52 additions and 5349 deletions

View file

@ -28,11 +28,11 @@ nodes_file = (
.read_text()
)
node_lines = (
nodes_file.split("pub enum AnyNode {")[1].split("}")[0].strip().splitlines()
nodes_file.split("pub enum AnyNodeRef<'a> {")[1].split("}")[0].strip().splitlines()
)
nodes = []
for node_line in node_lines:
node = node_line.split("(")[1].split(")")[0].split("::")[-1].split("<")[0]
node = node_line.split("(")[1].split(")")[0].split("::")[-1].removeprefix("&'a ")
# `FString` has a custom implementation while the formatting for
# `FStringLiteralElement`, `FStringFormatSpec` and `FStringExpressionElement` are handled by the `FString`
# implementation.

View file

@ -184,7 +184,7 @@ mod tests {
use insta::assert_debug_snapshot;
use ruff_formatter::SourceCode;
use ruff_python_ast::AnyNode;
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{StmtBreak, StmtContinue};
use ruff_python_trivia::{CommentLinePosition, CommentRanges};
use ruff_text_size::{TextRange, TextSize};
@ -194,13 +194,13 @@ mod tests {
#[test]
fn debug() {
let continue_statement = AnyNode::from(StmtContinue {
let continue_statement = StmtContinue {
range: TextRange::new(TextSize::new(18), TextSize::new(26)),
});
};
let break_statement = AnyNode::from(StmtBreak {
let break_statement = StmtBreak {
range: TextRange::new(TextSize::new(55), TextSize::new(60)),
});
};
let source = r"# leading comment
continue; # trailing
@ -213,7 +213,7 @@ break;
let mut comments_map: CommentsMap = MultiMap::new();
comments_map.push_leading(
continue_statement.as_ref().into(),
AnyNodeRef::from(&continue_statement).into(),
SourceComment::new(
source_code.slice(TextRange::at(TextSize::new(0), TextSize::new(17))),
CommentLinePosition::OwnLine,
@ -221,7 +221,7 @@ break;
);
comments_map.push_trailing(
continue_statement.as_ref().into(),
AnyNodeRef::from(&continue_statement).into(),
SourceComment::new(
source_code.slice(TextRange::at(TextSize::new(28), TextSize::new(10))),
CommentLinePosition::EndOfLine,
@ -229,7 +229,7 @@ break;
);
comments_map.push_leading(
break_statement.as_ref().into(),
AnyNodeRef::from(&break_statement).into(),
SourceComment::new(
source_code.slice(TextRange::at(TextSize::new(39), TextSize::new(15))),
CommentLinePosition::OwnLine,

View file

@ -1,7 +1,7 @@
use std::borrow::Cow;
use ruff_formatter::{format_args, write, FormatError, FormatOptions, SourceCode};
use ruff_python_ast::{AnyNodeRef, AstNode, NodeKind, PySourceType};
use ruff_python_ast::{AnyNodeRef, NodeKind, PySourceType};
use ruff_python_trivia::{
is_pragma_comment, lines_after, lines_after_ignoring_trivia, lines_before, CommentLinePosition,
};
@ -13,11 +13,11 @@ use crate::prelude::*;
use crate::statement::suite::should_insert_blank_line_after_class_in_stub_file;
/// Formats the leading comments of a node.
pub(crate) fn leading_node_comments<T>(node: &T) -> FormatLeadingComments
pub(crate) fn leading_node_comments<'a, T>(node: T) -> FormatLeadingComments<'a>
where
T: AstNode,
T: Into<AnyNodeRef<'a>>,
{
FormatLeadingComments::Node(node.as_any_node_ref())
FormatLeadingComments::Node(node.into())
}
/// Formats the passed comments as leading comments
@ -192,11 +192,11 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
}
/// Formats the dangling comments of `node`.
pub(crate) fn dangling_node_comments<T>(node: &T) -> FormatDanglingComments
pub(crate) fn dangling_node_comments<'a, T>(node: T) -> FormatDanglingComments<'a>
where
T: AstNode,
T: Into<AnyNodeRef<'a>>,
{
FormatDanglingComments::Node(node.as_any_node_ref())
FormatDanglingComments::Node(node.into())
}
pub(crate) fn dangling_comments(comments: &[SourceComment]) -> FormatDanglingComments {

View file

@ -1,5 +1,5 @@
use ruff_formatter::{write, FormatError};
use ruff_python_ast::{AnyNodeRef, AstNode};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprSlice, ExprUnaryOp, UnaryOp};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
@ -36,7 +36,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
// to handle newlines and spacing, or the node is None and we insert the corresponding
// slice of dangling comments
let comments = f.context().comments().clone();
let slice_dangling_comments = comments.dangling(item.as_any_node_ref());
let slice_dangling_comments = comments.dangling(item);
// Put the dangling comments (where the nodes are missing) into buckets
let first_colon_partition_index =
slice_dangling_comments.partition_point(|x| x.start() < first_colon.start());

View file

@ -1,5 +1,5 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_python_ast::{AnyNodeRef, AstNode};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprSubscript};
use crate::expression::expr_tuple::TupleParentheses;
@ -35,7 +35,7 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
let call_chain_layout = self.call_chain_layout.apply_in_node(item, f);
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item.as_any_node_ref());
let dangling_comments = comments.dangling(item);
debug_assert!(
dangling_comments.len() <= 1,
"A subscript expression can only have a single dangling comment, the one after the bracket"

View file

@ -4,10 +4,10 @@ use tracing::Level;
pub use range::format_range;
use ruff_formatter::prelude::*;
use ruff_formatter::{format, write, FormatError, Formatted, PrintError, Printed, SourceCode};
use ruff_python_ast::AstNode;
use ruff_python_ast::Mod;
use ruff_python_ast::{AnyNodeRef, Mod};
use ruff_python_parser::{parse, AsMode, ParseError, Parsed};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::Ranged;
use crate::comments::{
has_skip_comment, leading_comments, trailing_comments, Comments, SourceComment,
@ -43,23 +43,23 @@ mod verbatim;
/// 'ast is the lifetime of the source code (input), 'buf is the lifetime of the buffer (output)
pub(crate) type PyFormatter<'ast, 'buf> = Formatter<'buf, PyFormatContext<'ast>>;
/// Rule for formatting a Python [`AstNode`].
/// Rule for formatting a Python AST node.
pub(crate) trait FormatNodeRule<N>
where
N: AstNode,
N: Ranged,
for<'a> AnyNodeRef<'a>: From<&'a N>,
{
fn fmt(&self, node: &N, f: &mut PyFormatter) -> FormatResult<()> {
let comments = f.context().comments().clone();
let node_comments = comments.leading_dangling_trailing(node.as_any_node_ref());
let node_ref = AnyNodeRef::from(node);
let node_comments = comments.leading_dangling_trailing(node_ref);
if self.is_suppressed(node_comments.trailing, f.context()) {
suppressed_node(node.as_any_node_ref()).fmt(f)
suppressed_node(node_ref).fmt(f)
} else {
leading_comments(node_comments.leading).fmt(f)?;
let node_ref = node.as_any_node_ref();
// Emit source map information for nodes that are valid "narrowing" targets
// in range formatting. Never emit source map information if they're disabled
// for performance reasons.

View file

@ -1,6 +1,5 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::Parameters;
use ruff_python_ast::{AnyNodeRef, AstNode};
use ruff_python_ast::{AnyNodeRef, Parameters};
use ruff_python_trivia::{CommentLinePosition, SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
@ -165,7 +164,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
token("*"),
vararg.format()
]);
last_node = Some(vararg.as_any_node_ref());
last_node = Some(vararg.as_ref().into());
} else if !kwonlyargs.is_empty() {
// Given very strange comment placement, comments here may not actually have been
// marked as `StarLeading`/`StarTrailing`, but that's fine since we still produce
@ -201,7 +200,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
token("**"),
kwarg.format()
]);
last_node = Some(kwarg.as_any_node_ref());
last_node = Some(kwarg.as_ref().into());
}
joiner.finish()?;

View file

@ -1,5 +1,4 @@
use ruff_formatter::write;
use ruff_python_ast::AstNode;
use ruff_python_ast::{Pattern, PatternArguments};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
@ -63,7 +62,7 @@ impl FormatNodeRule<PatternArguments> for FormatPatternArguments {
// )
// ```
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item.as_any_node_ref());
let dangling_comments = comments.dangling(item);
write!(
f,

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtGlobal;
use crate::comments::SourceComment;
@ -14,7 +13,7 @@ impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
// Join the `global` names, breaking across continuation lines if necessary, unless the
// `global` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `global` statement.
if f.context().comments().has_trailing(item.as_any_node_ref()) {
if f.context().comments().has_trailing(item) {
let joined = format_with(|f| {
f.join_with(format_args![token(","), space()])
.entries(item.names.iter().formatted())

View file

@ -1,5 +1,4 @@
use ruff_formatter::write;
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtImportFrom;
use ruff_text_size::Ranged;
@ -62,7 +61,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
// )
// ```
let comments = f.context().comments().clone();
let parenthesized_comments = comments.dangling(item.as_any_node_ref());
let parenthesized_comments = comments.dangling(item);
if parenthesized_comments.is_empty() {
parenthesize_if_expands(&names).fmt(f)

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtNonlocal;
use crate::comments::SourceComment;
@ -14,7 +13,7 @@ impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
// Join the `nonlocal` names, breaking across continuation lines if necessary, unless the
// `nonlocal` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `nonlocal` statement.
if f.context().comments().has_trailing(item.as_any_node_ref()) {
if f.context().comments().has_trailing(item) {
let joined = format_with(|f| {
f.join_with(format_args![token(","), space()])
.entries(item.names.iter().formatted())

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::{Stmt, StmtWhile};
use ruff_text_size::Ranged;
@ -22,7 +21,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
} = item;
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item.as_any_node_ref());
let dangling_comments = comments.dangling(item);
let body_start = body.first().map_or(test.end(), Stmt::start);
let or_else_comments_start =

View file

@ -1,6 +1,5 @@
use ruff_formatter::{format_args, write, FormatContext, FormatError};
use ruff_python_ast::StmtWith;
use ruff_python_ast::{AstNode, WithItem};
use ruff_python_ast::{StmtWith, WithItem};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
@ -36,7 +35,7 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
// ...
// ```
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(with_stmt.as_any_node_ref());
let dangling_comments = comments.dangling(with_stmt);
let partition_point = dangling_comments.partition_point(|comment| {
with_stmt
.items

View file

@ -1,5 +1,4 @@
use ruff_formatter::FormatResult;
use ruff_python_ast::AstNode;
use ruff_python_ast::TypeParams;
use ruff_text_size::Ranged;
@ -21,7 +20,7 @@ impl FormatNodeRule<TypeParams> for FormatTypeParams {
// c,
// ] = ...
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item.as_any_node_ref());
let dangling_comments = comments.dangling(item);
let items = format_with(|f| {
f.join_comma_separated(item.end())