Eagerly check for ambiguity in macro parsing#158976
Conversation
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @nnethercote (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
This comment has been minimized.
This comment has been minimized.
1db582c to
392aa10
Compare
This comment has been minimized.
This comment has been minimized.
The code that calls `parser.bump()` or parses a non-terminal is moved from `parse_tt()` to `parse_tt_inner()`. This makes it possible to move the non-terminal parsing code even further, to `match_one()`. I marked the comment in the moved code as a `FIXME` because it reads like one. This makes `parse_tt()` look quite redundant, but once the full switch to DFS is made, I think it will handle some back-tracking logic. `tests/ui/macros` pass.
This will be used in the next commit to reference `MatcherLoc`s by index so that they can be compared and hashed efficiently.
Right now, if an ambiguity error occurs, `TtParser::{next,bb}_mps` are
relied on to extract information for diagnostics. This hinders movement
to depth-first traversal. This commit allows the `Tracker` to compute
`{next,bb}_mps` information without relying on `TtParser` to work in a
breadth-first way.
This information is recorded in a `HashSet`, mixing data from `next_mps`
and `bb_mps`. It *could* have been a `Vec` that gets cleared after a
`Parser::bump()` / non-terminal parse, but that be incompatible with
depth-first traversal in `TtParser`.
`CollectTrackerAndEmitter` can compute this data from `Tracker::matched_one()`. The collected data coalesces non-terminal `MatcherLoc`s with others, so `partition()` is used to separate them. The data is retrieved from a `HashSet`, but because the results are sorted, everything is deterministic. This removes some pressure on `TtParser` to use breadth-first traversal. `tests/ui/macros` pass.
When a meta-variable `MatcherLoc` is discovered, it would be nice to parse it immediately; but right now, it gets added to `bb_mps` and has to wait until everything in `cur_mps` is processed so `TtParser` can check for ambiguity. This commit adds an eager ambiguity checking step when a meta-variable `MatcherLoc` is being processed. This is a subtle but important change in the semantics of `match_one()`; it can now modify `cur_mps`, and effectively work from `parse_tt_inner()`. This is the first major step towards depth-first traversal.
The same infrastructure for checking for ambiguity for `MetaVarDecl` can also be used for processing `Eof`! This has several benefits: - `eof_mps` is removed and no longer needs to be passed around. - The two calls to `Tracker::failure()` are unified; specifically "reached EOF but no successful parses" and "ran out of mps". - The two calls to `TtParser::ambiguity_error()` are unified: specifically "multiple successful parses at EOF" and "multiple `MatcherLoc`s at a non-terminal parsing point". These unifications further motivate the original unification of the errors in `Tracker`, and show that the different kinds of merged errors *did* deserve to be grouped together semantically. `tests/ui/macros` pass.
If `parse_tt_inner()` -> `match_one()` -> `check_for_ambiguity()` detects ambiguity, it will clear `cur_mps`, which guarantees that the next thing to happen will be the check for ambiguity. This commit brings the ambiguity detection closer to the building of the ambiguity error by moving that code directly into `check_for_ambiguity()` (which now returns a `ParseResult`).
392aa10 to
a1b7471
Compare
|
This PR was rebased onto a different main 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. |
|
can we get a perf run? |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Eagerly check for ambiguity in macro parsing
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (d51d067): 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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -0.7%, secondary 0.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -1.9%, secondary -4.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 490.231s -> 487.546s (-0.55%) |
|
The perf results seem quite reasonable (small but widespread improvements in instruction count, limited but nice improvements in cycle count) ... shall I set +perf-regression-triaged? |
|
Perf improvements outweigh regressions. @rustbot label: +perf-regression-triaged |
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing da80ed0 (parent) -> 2f5253f (this PR) Test differencesShow 4 test diffs4 doctest diffs were found. These are ignored, as they are noisy. Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 2f5253f0087ee60faab7dbe7c9002163c605ac7e --output-dir test-dashboardAnd then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
|
Finished benchmarking commit (2f5253f): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Our benchmarks found a performance regression caused by this PR. Next Steps:
@rustbot label: +perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 3.2%, secondary 11.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.2%, secondary 3.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 491.202s -> 490.821s (-0.08%) |
|
@rustbot label: +perf-regression-triaged |
|
Perhaps I can add some context: it seems |
|
Makes sense, thanks for the explanation. I seem to remember |
View all comments
This PR implements an important step leading up to the BFS->DFS change: it makes ambiguity detection (as occurs when parsing meta-variables and reaching EOF) eager. Rather than accumulating the
bb_mpsandeof_mpslists, then checking that a single valid parse exists, this PR introduces acheck_for_ambiguity()method that gets called as soon as the relevantMatcherLocis observed.check_for_ambiguity()immediately drainscur_mpsinstead of waiting for the outer loop to do so, and evaluates the mps within a "checking for ambiguity" context.This change was complicated by the fact that
bb_mpswas relied on for error messages; it was passed along toTracker::ambiguity(). The same is true fornext_mps. The first few commits of this PR (after pending rebases) add state toTrackerso that it can compute the relevant diagnostic information independently ofbb_mps.This PR complicates the control flow around
parse_tt_inner()in subtle ways. I'm not super happy about it, but I think it will become easier to understand again once a DFS approach is implemented.This PR contains #158894 and #158974. I'll rebase on top ofmainonce they're both merged.Best reviewed commit-by-commit.
r? @nnethercote