Skip to content

Conversation

Glyphack
Copy link
Contributor

@Glyphack Glyphack commented Mar 11, 2025

Summary

Part of #15655

Replaced statement nodes with autogenerated ones. Reused the stuff we introduced in #16285. Nothing except for copying the nodes to new format.

Test Plan

Tests run without any changes. Also moved the test that checks size of AST nodes to generated.rs since all of the structs that it tests are now there.

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Mar 12, 2025
@Glyphack Glyphack force-pushed the ast-autogen-stmt branch 2 times, most recently from 8580b75 to 9b2b18b Compare March 12, 2025 07:42
Copy link
Contributor

github-actions bot commented Mar 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

{ name = "decorator_list", type = "Decorator*" },
{ name = "name", type = "Identifier" },
# check remove
{ name = "type_params", type = "Box<crate::TypeParams>?" },
Copy link
Contributor Author

@Glyphack Glyphack Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this is boxed here to reduce the size of statement. But it's not the same in StmtTypeAlias was is tested there and the benefit was small?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try, but I suspect that StmtFunctionDef is simply the largest statement node and reducing the size of any other node doesn't help to reduce the size of Stmt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I forgot about the enum size thing. Thanks.

@Glyphack Glyphack marked this pull request as ready for review March 12, 2025 21:52
@MichaReiser MichaReiser requested a review from dcreager March 13, 2025 08:51
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

{ name = "decorator_list", type = "Decorator*" },
{ name = "name", type = "Identifier" },
# check remove
{ name = "type_params", type = "Box<crate::TypeParams>?" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try, but I suspect that StmtFunctionDef is simply the largest statement node and reducing the size of any other node doesn't help to reduce the size of Stmt.

# Node structs


def write_size_test(out: list[str]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer to keep this code outside generate.rs. It doesn't benefit from the codegen and being able to change code directly in rs file has better ergonomics (I don't have to go searching where the code is coming from and I've an IDE that can help me figure out the code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, it's also easier to just hover over the type and find the size there.

While you're at it, can you also remove the size assertions for Pattern and ExprFString specifically for Rustc < 1.76 as our MSRV is 1.83 now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've already addressed this, but just calling out that I agree with @MichaReiser on this — the generate script should only produce code that actually depends on the AST definitions in ast.toml. Anything that is not parameterized like that should go in a hand-edited .rs file like you have now.

# Node structs


def write_size_test(out: list[str]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've already addressed this, but just calling out that I agree with @MichaReiser on this — the generate script should only produce code that actually depends on the AST definitions in ast.toml. Anything that is not parameterized like that should go in a hand-edited .rs file like you have now.

@dcreager dcreager changed the title Auto generate statement nodes [red-knot] Auto generate statement nodes Mar 13, 2025
@dcreager dcreager added the ty Multi-file analysis & type inference label Mar 13, 2025
@MichaReiser MichaReiser merged commit 360ba09 into astral-sh:main Mar 13, 2025
21 checks passed
@Glyphack Glyphack deleted the ast-autogen-stmt branch March 13, 2025 15:16
dcreager added a commit that referenced this pull request Mar 14, 2025
* main: (53 commits)
  [syntax-errors] Tuple unpacking in `for` statement iterator clause before Python 3.9 (#16558)
  Ruff v0.10 Release (#16708)
  Add new `noqa` specification to the docs (#16703)
  describe requires-python fallback in docs (#16704)
  [red-knot] handle cycles in MRO/bases resolution (#16693)
  [red-knot] Auto generate statement nodes (#16645)
  [`pylint`] Better inference for `str.strip` (`PLE310`) (#16671)
  [`pylint`] Improve `repeated-equality-comparison` fix to use a `set` when all elements are hashable (`PLR1714`) (#16685)
  [`pylint`/`pep8-naming`] Check `__new__` argument name in `bad-staticmethod-argument` and not `invalid-first-argument-name-for-class-method` (`PLW0211`/`N804`) (#16676)
  [`flake8-pyi`] Stabilize fix for `unused-private-type-var` (`PYI018`) (#16682)
  [`flake8-bandit`] Deprecate `suspicious-xmle-tree-usage` (`S320`) (#16680)
  [`flake8-simplify`] Avoid double negation in fixes (`SIM103`) (#16684)
  [`pyupgrade`]: Improve diagnostic range for `redundant-open-mode` (`UP015`) (#16672)
  Consider all `TYPE_CHECKING` symbols for type-checking blocks (#16669)
  [`pep8-naming`]: Ignore methods decorated with `@typing.override` (`invalid-argument-name`) (#16667)
  Stabilize FURB169 preview behavior (#16666)
  [`pylint`] Detect invalid default value type for `os.environ.get` (`PLW1508`) (#16674)
  [`flake8-pytest-style`] Allow for loops with empty bodies (`PT012`, `PT031`) (#16678)
  [`pyupgrade`]: Deprecate `non-pep604-isinstance` (`UP038`) (#16681)
  [`flake8-type-checking`] Stabilize `runtime-cast-value` (`TC006`) (#16637)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants