Skip to content

Clean up AST visitor some more - #162315

Draft
nnethercote wants to merge 9 commits into
rust-lang:mainfrom
nnethercote:clean-up-AST-visitor-some-more
Draft

Clean up AST visitor some more#162315
nnethercote wants to merge 9 commits into
rust-lang:mainfrom
nnethercote:clean-up-AST-visitor-some-more

Conversation

@nnethercote

@nnethercote nnethercote commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

A sequel to #162226. Details in individual commits.

r? @fee1-dead

`visit_test_binder_constraints` is the only `visit_foo`
method in `impl_visitable_calling_walkable!` without a corresponding
`walk_foo` method in `define_named_walk!`.

I confirmed with the original author (khyperia) that this is an
unintentional oversight.
There is currently a big list of `visit_*` methods in one macro call and
another list of `walk_*` method in another macro call. The previous
commit showed these can unintentionally get out of sync.

This commit introduces a higher-order macro that puts all the
information in a single place. This will make it impossible for the
`visit_*` and `walk_*` methods to get out of sync (as we saw in the
previous commit). It will also facilitate another cleanup in
the next commit.
Currently various `Visitable` impls are defined within functions even
though `Visitable` is defined at the top level, which is weird and
requires `allow(non_local_definitions)`.

This commit uses `for_each_ast_visit_hook` to move them out. It also
renames a couple of the existing macros to give them simpler names.
Use `$ty` and `$extra` and `$extra_ty` consistently in macros, rather
than `$Ty` and `$ExtraTy` and `$ParamTy`.

Also fix some spacing.
They fit the pattern.

Also remove `#[inline]` from `impl_visitable_list` for consistency. The
visitor is generic so the methods are monomorphized into the calling
crate and probably inlined anyway.
The AST visitor code uses `($($extra_ty)?)` which expands to `()` in
some cases and `(T)` in others, which requires `allow(unused_parens)` to
avoid warnings.

This commit splits the two `impl_visitable!` macros into two rules: one
for the "no-extra" case and one for the "with-extra" case. There's a
small amount of duplication between the two rules but there are multiple
advantages.

- The `allow(unused_parens)` is removed.

- `impl_visitable!` callers can omit the extra param if it's `()`.

- When the extra argument might or might not be present, a more standard
  `$(, $extra: $extra_ty)?` is now used.

- No weird `let ($($extra)?) = extra;` destructuring.

The `Visitable` derive also gets a tweak to avoid unnecessary parens
around extra args.
These macros aren't needed outside the crate.
We can just hardwire `'a` and `Visitor`/`MutVisitor`. This simplifies
the inputs for these macros from "leading thing + repeating elements" to
just "repeating elements".
A lot of these lists are already almost in alphabetical order.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 4, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

LLM disclosure: most of these clean-ups were suggested by an LLM. I made all the code and text changes myself.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Shouldn't affect perf, but just in case:

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 4, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 4, 2026
@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 5, 2026
@rust-bors

rust-bors Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

💥 Test timed out after 21600s

@nnethercote

Copy link
Copy Markdown
Contributor Author

💥 Test timed out after 21600s

Not sure what happened. Let's try again:

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 5, 2026
@rust-bors

rust-bors Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: d804d79 (d804d7975d767ccd03c93b9f34541ae4bc41c8ee)
Base parent: 0ed41eb (0ed41eb4142dda2df61eb1145a312c1a9d62eb56)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d804d79): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.3%] 7
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 2
All ❌✅ (primary) 0.2% [0.2%, 0.3%] 7

Max RSS (memory usage)

Results (primary 2.2%, secondary 6.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
7.7% [7.7%, 7.7%] 1
Regressions ❌
(secondary)
6.8% [6.8%, 6.8%] 1
Improvements ✅
(primary)
-3.3% [-3.3%, -3.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.2% [-3.3%, 7.7%] 2

Cycles

Results (secondary -2.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.718s -> 480.953s (0.89%)
Artifact size: 403.29 MiB -> 403.40 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 5, 2026
@nnethercote
nnethercote marked this pull request as draft September 5, 2026 13:16
@nnethercote

Copy link
Copy Markdown
Contributor Author

Slight regressions on html5ever. I'll investigate on Monday.

@rust-bors

rust-bors Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #162310) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants