sway macros for visiting the typed tree #7505
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces some
proc-macrosto help create visitors to the typed tree. Today, we have four main visitors that were developed using traits, each for its own purpose.Their main issue is that they mutate their trees only sometimes. We do not have a way to know a priori if the visit will mutate anything or not. So we clone everything beforehand and pass a
&mut. Needless to say that this is a huge waste. CloningTyFunctionDeclis one of the biggest wastes, and we can improve up to 10% of compilation performance by avoiding these clones.Another issue is that being trait-based, once we change the trait signature for whatever reason, we need to change dozens and dozens of files, just to make the project compile and check if the change works or not. That completely breaks the development loop.
Depending on the change, bugs can be introduced and are very, very hard to find.
To try to alleviate this, this PR introduces the concept of purpose-free visit. The macro will generate a general enough visit that will traverse the tree and allow a "visitor" to do whatever it wants.
We only need to start with the derive macro.
The generated code is tailored for mutation. For occasional mutation.
And it is also intentionally "non-infectious". This means that decorating one struct with "Visit" does not mandate others to also be derived, like happens with Debug, for example. This means that simply deriving "Visit" in one struct will not traverse the whole tree of objects.
The generated code will be something like:
Each field behaviour can be configured with "call_visit", "do_not_call_visit", "call_visitor", "do_not_call_visitor", "iter_visit", "iter_visitor", "skip", "optional".
For example:
The other missing piece is the visitor which is built in two steps. We first generate one generate Visitor trait. Which will be used by all visitors:
And each specific visitor can be built like:
Checklist
Breaking*orNew Featurelabels where relevant.