about summary refs log tree commit diff
path: root/compiler/rustc_expand/src/mbe
AgeCommit message (Collapse)AuthorLines
2022-04-05span: move `MultiSpan`David Wood-1/+2
`MultiSpan` contains labels, which are more complicated with the introduction of diagnostic translation and will use types from `rustc_errors` - however, `rustc_errors` depends on `rustc_span` so `rustc_span` cannot use types like `DiagnosticMessage` without dependency cycles. Introduce a new `rustc_error_messages` crate that can contain `DiagnosticMessage` and `MultiSpan`. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05errors: introduce `DiagnosticMessage`David Wood-4/+5
Introduce a `DiagnosticMessage` type that will enable diagnostic messages to be simple strings or Fluent identifiers. `DiagnosticMessage` is now used in the implementation of the standard `DiagnosticBuilder` APIs. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-04Reorder match arms in `parse_tt_inner`.Nicholas Nethercote-31/+31
To match the order the variants are declared in.
2022-04-04A new matcher representation for use in `parse_tt`.Nicholas Nethercote-332/+289
`parse_tt` currently traverses a `&[TokenTree]` to do matching. But this is a bad representation for the traversal. - `TokenTree` is nested, and there's a bunch of expensive and fiddly state required to handle entering and exiting nested submatchers. - There are three positions (sequence separators, sequence Kleene ops, and end of the matcher) that are represented by an index that exceeds the end of the `&[TokenTree]`, which is clumsy and error-prone. This commit introduces a new representation called `MatcherLoc` that is designed specifically for matching. It fixes all the above problems, making the code much easier to read. A `&[TokenTree]` is converted to a `&[MatcherLoc]` before matching begins. Despite the cost of the conversion, it's still a net performance win, because various pieces of traversal state are computed once up-front, rather than having to be recomputed repeatedly during the macro matching. Some improvements worth noting. - `parse_tt_inner` is *much* easier to read. No more having to compare `idx` against `len` and read comments to understand what the result means. - The handling of `Delimited` in `parse_tt_inner` is now trivial. - The three end-of-sequence cases in `parse_tt_inner` are now handled in three separate match arms, and the control flow is much simpler. - `nameize` is no longer recursive. - There were two places that issued "missing fragment specifier" errors: one in `parse_tt_inner()`, and one in `nameize()`. Presumably the latter was never executed. There's now a single place issuing these errors, in `compute_locs()`. - The number of heap allocations done for a `check full` build of `async-std-1.10.0` (an extreme example of heavy macro use) drops from 11.8M to 2.6M, and most of these occur outside of macro matching. - The size of `MatcherPos` drops from 64 bytes to 16 bytes. Small enough that it no longer needs boxing, which partly accounts for the reduction in allocations. - The rest of the drop in allocations is due to the removal of `MatcherKind`, because we no longer need to record anything for the parent matcher when entering a submatcher. - Overall it reduces code size by 45 lines.
2022-04-02Create 2024 editionJacob Pratt-1/+1
2022-04-02Auto merge of #95509 - nnethercote:simplify-MatcherPos-some-more, r=petrochenkovbors-95/+119
Simplify `MatcherPos` some more A few more improvements. r? `@petrochenkov`
2022-03-31expand: Do not count metavar declarations on RHS of `macro_rules`Vadim Petrochenkov-15/+10
They are 0 by definition there.
2022-03-31Make `MatcherPos` not derive `Clone`.Nicholas Nethercote-8/+24
It's only used in one place, and there we clone and then make a bunch of modifications. It's clearer if we duplicate more explicitly, and there's a symmetry now between `sequence()` and `empty_sequence()`.
2022-03-31Remove `MatcherPos::stack`.Nicholas Nethercote-62/+73
`parse_tt` needs a way to get from within submatchers make to the enclosing submatchers. Currently it has two distinct mechanisms for this: - `Delimited` submatchers use `MatcherPos::stack` to record stuff about the parent (and further back ancestors). - `Sequence` submatchers use `MatcherPosSequence::parent` to point to the parent matcher position. Having two mechanisms is really confusing, and it took me a long time to understand all this. This commit eliminates `MatcherPos::stack`, and changes `Delimited` submatchers to use the same mechanism as sequence submatchers. That mechanism is also changed a bit: instead of storing the entire parent `MatcherPos`, we now only store the necessary parts from the parent `MatcherPos`. Overall this is a small performance win, with the positives outweighing the negatives, but it's mostly for clarity.
2022-03-31Rollup merge of #95497 - nyurik:compiler-spell-comments, r=compiler-errorsDylan DPC-2/+2
Spellchecking compiler comments This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-31Clarify `idx` handling in sequences.Nicholas Nethercote-9/+18
By adding comments, and improving an assertion. I finally fully understand this part!
2022-03-31Remove `MatcherPos::match_lo`.Nicholas Nethercote-10/+4
It's redundant w.r.t. other fields.
2022-03-31Simplify exit of `Delimited` submatchers.Nicholas Nethercote-18/+12
Currently, we detect an exit from a `Delimited` submatcher when `idx` exceeds the bounds of the current submatcher *and* there is a `stack` entry. This commit changes it to something simpler: just look for a `CloseDelim` token.
2022-03-30Spellchecking compiler commentsYuri Astrakhan-2/+2
This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-30Pre-allocate an empty `Lrc<NamedMatchVec>`.Nicholas Nethercote-8/+26
This avoids some allocations.
2022-03-30Overhaul how matches are recorded.Nicholas Nethercote-48/+55
Currently, matches within a sequence are recorded in a new empty `matches` vector. Then when the sequence finishes the matches are merged into the `matches` vector of the parent. This commit changes things so that a sequence mp inherits the matches made so far. This means that additional matches from the sequence don't need to be merged into the parent. `push_match` becomes more complicated, and the current sequence depth needs to be tracked. But it's a sizeable performance win because it avoids one or more `push_match` calls on every iteration of a sequence. The commit also removes `match_hi`, which is no longer necessary.
2022-03-30Improve comments and rename many things for consistency.Nicholas Nethercote-198/+181
In particular: - Replace use of "item" with "matcher position/"mp". - Replace use of "repetition" with "sequence". - Replace `ms` with `matcher`.
2022-03-30Clarify comments about doc comments in macros.Nicholas Nethercote-1/+6
2022-03-30Simplify and rename `count_names`.Nicholas Nethercote-14/+14
2022-03-29Add a useful assertion.Nicholas Nethercote-2/+7
2022-03-28Rollup merge of #95390 - nnethercote:allow-doc-comments-in-macros, ↵Dylan DPC-7/+8
r=petrochenkov Ignore doc comments in a declarative macro matcher. Fixes #95267. Reverts to the old behaviour before #95159 introduced a regression. r? `@petrochenkov`
2022-03-28Ignore doc comments in a declarative macro matcher.Nicholas Nethercote-7/+8
Fixes #95267. Reverts to the old behaviour before #95159 introduced a regression.
2022-03-28Remove `Nonterminal::NtTT`.Nicholas Nethercote-31/+27
It's only needed for macro expansion, not as a general element in the AST. This commit removes it, adds `NtOrTt` for the parser and macro expansion cases, and renames the variants in `NamedMatch` to better match the new type.
2022-03-25Shrink `MatcherPosRepetition`.Nicholas Nethercote-15/+13
Currently it copies a `KleeneOp` and a `Token` out of a `SequenceRepetition`. It's better to store a reference to the `SequenceRepetition`, which is now possible due to #95159 having changed the lifetimes.
2022-03-25Shrink `NamedMatchVec` to one inline element.Nicholas Nethercote-2/+5
This counters the `NamedMatchVec` size increase from the previous commit, leaving `NamedMatchVec` smaller than before.
2022-03-25Split `NamedMatch::MatchNonterminal` in two.Nicholas Nethercote-71/+81
The `Lrc` is only relevant within `transcribe()`. There, the `Lrc` is helpful for the non-`NtTT` cases, because the entire nonterminal is cloned. But for the `NtTT` cases the inner token tree is cloned (a full clone) and so the `Lrc` is of no help. This commit splits the `NtTT` and non-`NtTT` cases, avoiding the useless `Lrc` in the former case, for the following effect on macro-heavy crates. - It reduces the total number of allocations a lot. - It increases the size of some of the remaining allocations. - It doesn't affect *peak* memory usage, because the larger allocations are short-lived. This overall gives a speed win.
2022-03-23Add a size assertion for `NamedMatchVec`.Nicholas Nethercote-0/+4
2022-03-22Auto merge of #95159 - nnethercote:TtParser, r=petrochenkovbors-464/+379
Introduce `TtParser` These commits make a number of changes to declarative macro expansion, resulting in code that is shorter, simpler, and faster. Best reviewed one commit at a time. r? `@petrochenkov`
2022-03-23Eliminate `TokenTreeOrTokenTreeSlice`.Nicholas Nethercote-126/+105
As its name suggests, `TokenTreeOrTokenTreeSlice` is either a single `TokenTree` or a slice of them. It has methods `len` and `get_tt` that let it be treated much like an ordinary slice. The reason it isn't an ordinary slice is that for `TokenTree::Delimited` the open and close delimiters are represented implicitly, and when they are needed they are constructed on the fly with `Delimited::{open,close}_tt`, rather than being present in memory. This commit changes `Delimited` so the open and close delimiters are represented explicitly. As a result, `TokenTreeOrTokenTreeSlice` is no longer needed and `MatcherPos` and `MatcherTtFrame` can just use an ordinary slice. `TokenTree::{len,get_tt}` are also removed, because they were only needed to support `TokenTreeOrTokenTreeSlice`. The change makes the code shorter and a little bit faster on benchmarks that use macro expansion heavily, partly because `MatcherPos` is a lot smaller (less data to `memcpy`) and partly because ordinary slice operations are faster than `TokenTreeOrTokenTreeSlice::{len,get_tt}`.
2022-03-21Fix generated tokens hygieneCaio-4/+10
2022-03-21Move items into `TtParser` as `Vec`s.Nicholas Nethercote-58/+43
By putting them in `TtParser`, we can reuse them for every rule in a macro. With that done, they can be `SmallVec` instead of `Vec`, and this is a performance win because these vectors are hot and `SmallVec` operations are a bit slower due to always needing an "inline or heap?" check.
2022-03-21Remove `MatcherPosHandle`.Nicholas Nethercote-88/+22
This type was a small performance win for `html5ever`, which uses a macro with hundreds of very simple rules that don't contain any metavariables. But this type is complicated (extra lifetimes) and perf-neutral for macros that do have metavariables. This commit removes `MatcherPosHandle`, simplifying things a lot. This increases the allocation rate for `html5ever` and similar cases a bit, but makes things easier for follow-up changes that will improve performance more than what we lost here.
2022-03-19Remove an impossible code path.Nicholas Nethercote-5/+6
Doc comments cannot appear in a matcher.
2022-03-19Add `TtParser::macro_name`.Nicholas Nethercote-38/+35
Instead of passing it into `parse_tt`.
2022-03-19Rename `bb_items_ambiguity_error` as `ambiguity_error`.Nicholas Nethercote-2/+2
Because it involves `next_items` as well as `bb_items`.
2022-03-19Introduce `TtParser`.Nicholas Nethercote-304/+323
It currently has no state, just the three methods `parse_tt`, `parse_tt_inner`, and `bb_items_ambiguity_error`. This commit is large but trivial, and mostly consists of changes to the indentation of those methods. Subsequent commits will do more.
2022-03-18Auto merge of #95067 - nnethercote:parse_tt-more-refactoring, r=petrochenkovbors-142/+124
Still more refactoring of `parse_tt` r? `@petrochenkov`
2022-03-18Rename `TtSeq` as `TtSlice`.Nicholas Nethercote-10/+10
It's a better name because (a) it holds a slice, and (b) "sequence" has other meanings in this file.
2022-03-18Tweak a bunch of comments.Nicholas Nethercote-62/+36
I've been staring at these enough lately that they're annoying me, let's make them better.
2022-03-18Reorder cases in `parse_tt_inner`.Nicholas Nethercote-61/+55
I find the new order easier to read: within a matcher; past the end of a repetition; at end of input. It also reduces the indentation level by one for
2022-03-18Only modify `eof_items` if `token == Eof`.Nicholas Nethercote-4/+7
Because that's the condition under which `eof_items` is used.
2022-03-18Factor out some code into `MatcherPos::repetition`.Nicholas Nethercote-28/+37
Also move `create_matches` within `impl MatcherPos`, because it's only used within that impl block.
2022-03-18Add two useful assertions.Nicholas Nethercote-0/+2
2022-03-16rustc_error: make ErrorReported impossible to constructmark-3/+5
There are a few places were we have to construct it, though, and a few places that are more invasive to change. To do this, we create a constructor with a long obvious name.
2022-03-14Fix remaining meta-variable expression TODOsCaio-9/+16
2022-03-12Rollup merge of #94833 - c410-f3r:meta-take-2, r=petrochenkovMatthias Krüger-10/+147
[2/2] Implement macro meta-variable expression Final part of https://github.com/rust-lang/rust/pull/93545#issuecomment-1050963295 r? `@petrochenkov`
2022-03-11Implement macro meta-variable expressionCaio-10/+147
2022-03-11Move a `parse_tt` error case into a separate function.Nicholas Nethercote-24/+35
2022-03-11Make next_items a `SmallVec`.Nicholas Nethercote-3/+2
For consistency, and to make the code slightly nicer.
2022-03-11Move `eof_items` handling entirely within `inner_parse_loop`.Nicholas Nethercote-50/+40
Also rename `inner_parse_loop` as `parse_tt_inner`, because it's no longer just a loop.