internal: Store token trees in the proc macro server in the representation of the tt crate - #23152
Open
ChayimFriedman2 wants to merge 1 commit into
Open
internal: Store token trees in the proc macro server in the representation of the tt crate#23152ChayimFriedman2 wants to merge 1 commit into
ChayimFriedman2 wants to merge 1 commit into
Conversation
ChayimFriedman2
force-pushed
the
proc-macro-srv-lossy
branch
2 times, most recently
from
August 16, 2026 00:39
03fe2e8 to
ec4f27c
Compare
Member
|
This will severely degrade proc-macro performance. We moved over to the bridge intentionally as concat is a super common operation in proc-macro's which our representation does not lend itself well too #21097 |
Contributor
Author
|
No this will not. We do not use the contiguous representation, only the same leaf types. |
This comment has been minimized.
This comment has been minimized.
…he tt crate And not in the representation of the proc macro bridge. We still do not use the contiguous representation the rest of r-a uses, and prefer a tree with `Rc`s (this changed from `Arc` because we don't need thread safety for the bridge) for cheap cloning, but the types are the same. The reason for that is that conversion to the bridge's types is a lossy conversion, in rustc as well: the internal token tree representation contains details that are lost in this conversion. This is a prerequisite to fix rust-lang#23088 - the only way to fix it properly is to have an additional kind of token tree for doc comments, like rustc does, as evidenced by the fact that if a proc macro would create this macro (that has a doc comment in its matcher), the doc comment would *not* be ignored for matching, but if the macro's matcher would have been passed to the proc macro and it wouldn't have touch it (meaning, not even listing the `TokenTree`s then giving them back), the comment would still be ignored. It is also required for supporting invisible groups properly for the same reason (there're actually several kinds of invisible delimiters, and the proc macro bridge lossily converts them all into one). While we're at, I've also tried to untangle the mess of Cargo features of the proc macro server, and reduce the code it contains: There are now *two* somewhat-orthogonal axes: the `in-rust-tree` feature, and the `in-ra` vs. `in-proc-macro-srv` feature. The former only decides whether we should import rustc crates from crates.io or from the sysroot. The latter decides what code to enable - the proc macro server doesn't need all the code in tt and proc-macro-api. I also moved the proc macro server's `TokenStream` from proc-macro-srv into proc-macro-api and inverted their dependency, as was required for this work. You cannot enable `in-proc-macro-srv` and disable `in-rust-tree` (in practice this just disables all proc macro server code since such combination can arise when compiling r-a, also proc-macro-api supports this mode for tests), and you are not supposed to enable both `in-ra` and `in-proc-macro-srv`, except for tests. Enabling `in-rust-tree` and disabling `in-proc-macro-srv` is possible, though, and done when compiling r-a in-tree. In the future, this mode may use sysroot crates not just for the proc macro server, like was done in the past.
ChayimFriedman2
force-pushed
the
proc-macro-srv-lossy
branch
from
August 21, 2026 00:34
ec4f27c to
1d1a8b8
Compare
Collaborator
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
And not in the representation of the proc macro bridge. We still do not use the contiguous representation the rest of r-a uses, and prefer a tree with
Rcs (this changed fromArcbecause we don't need thread safety for the bridge) for cheap cloning, but the types are the same.The reason for that is that conversion to the bridge's types is a lossy conversion, in rustc as well: the internal token tree representation contains details that are lost in this conversion. This is a prerequisite to fix #23088 - the only way to fix it properly is to have an additional kind of token tree for doc comments, like rustc does, as evidenced by the fact that if a proc macro would create this macro (that has a doc comment in its matcher), the doc comment would not be ignored for matching, but if the macro's matcher would have been passed to the proc macro and it wouldn't have touch it (meaning, not even listing the
TokenTrees then giving them back), the comment would still be ignored.It is also required for supporting invisible groups properly for the same reason (there're actually several kinds of invisible delimiters, and the proc macro bridge lossily converts them all into one).
While we're at, I've also tried to untangle the mess of Cargo features of the proc macro server, and reduce the code it contains:
There are now two somewhat-orthogonal axes: the
in-rust-treefeature, and thein-ravs.in-proc-macro-srvfeature. The former only decides whether we should import rustc crates from crates.io or from the sysroot. The latter decides what code to enable - the proc macro server doesn't need all the code in tt and proc-macro-api. I also moved the proc macro server'sTokenStreamfrom proc-macro-srv into proc-macro-api and inverted their dependency, as was required for this work.You cannot enable
in-proc-macro-srvand disablein-rust-tree(in practice this just disables all proc macro server code since such combination can arise when compiling r-a, also proc-macro-api supports this mode for tests), and you are not supposed to enable bothin-raandin-proc-macro-srv, except for tests. Enablingin-rust-treeand disablingin-proc-macro-srvis possible, though, and done when compiling r-a in-tree. In the future, this mode may use sysroot crates not just for the proc macro server, like was done in the past.This depends on #23079 to not create merge conflicts for myself.