Commit Graph

368 Commits

Author SHA1 Message Date
Nicholas Nethercote
ac3d8ce1c6 Clarify comments about doc comments in macros. 2022-03-30 10:42:47 +11:00
Nicholas Nethercote
2b60cc081b Simplify and rename count_names. 2022-03-30 10:42:34 +11:00
Nicholas Nethercote
df6ead557d Add a useful assertion. 2022-03-29 08:00:26 +11:00
Dylan DPC
1c8b7412d4
Rollup merge of #95390 - nnethercote:allow-doc-comments-in-macros, 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-28 16:08:11 +02:00
Dylan DPC
ae037a86f9
Rollup merge of #95301 - nnethercote:rm-NtTT, r=petrochenkov
Remove `Nonterminal::NtTT`.

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.

r? `@petrochenkov`
2022-03-28 16:08:07 +02:00
Nicholas Nethercote
9967594346 Ignore doc comments in a declarative macro matcher.
Fixes #95267. Reverts to the old behaviour before #95159 introduced a
regression.
2022-03-28 10:45:24 +11:00
Nicholas Nethercote
364b908d57 Remove Nonterminal::NtTT.
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-28 10:03:02 +11:00
Dylan DPC
979c8e885e
Rollup merge of #95335 - Badel2:resolve-path, r=Dylan-DPC
Move resolve_path to rustc_builtin_macros and make it private

Fixing a FIXME introduced by `@jyn514` in #85457
2022-03-27 05:36:09 +02:00
Badel2
ea26d72710 Move resolve_path to rustc_builtin_macros and make it private 2022-03-26 16:47:13 +01:00
Vadim Petrochenkov
baa3ad4dc8 proc-macro: Stop wrapping ident matchers into groups 2022-03-26 12:38:46 +03:00
bors
c74925438c Auto merge of #95149 - cjgillot:once-diag, r=estebank
Remove `Session::one_time_diagnostic`

This is untracked mutable state, which modified the behaviour of queries.
It was used for 2 things: some full-blown errors, but mostly for lint declaration notes ("the lint level is defined here" notes).

It is replaced by the diagnostic deduplication infra which already exists in the diagnostic emitter.
A new diagnostic level `OnceNote` is introduced specifically for lint notes, to deduplicate subdiagnostics.

As a drive-by, diagnostic emission takes a `&mut` to allow dropping the `SubDiagnostic`s.
2022-03-26 00:54:54 +00:00
Nicholas Nethercote
fdec26ddad Shrink MatcherPosRepetition.
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-25 12:35:56 +11:00
Nicholas Nethercote
cad5f1e774 Shrink NamedMatchVec to one inline element.
This counters the `NamedMatchVec` size increase from the previous
commit, leaving `NamedMatchVec` smaller than before.
2022-03-25 12:35:56 +11:00
Nicholas Nethercote
6817442ec7 Split NamedMatch::MatchNonterminal in two.
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-25 12:35:52 +11:00
Nicholas Nethercote
904e70a7b0 Add a size assertion for NamedMatchVec. 2022-03-23 13:54:34 +11:00
bors
a4a5e79814 Auto merge of #95159 - nnethercote:TtParser, r=petrochenkov
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-22 21:46:57 +00:00
Nicholas Nethercote
31df680789 Eliminate TokenTreeOrTokenTreeSlice.
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-23 07:13:31 +11:00
Caio
74e7313e0e Fix generated tokens hygiene 2022-03-21 19:45:55 -03:00
Nicholas Nethercote
754dc8e66f Move items into TtParser as Vecs.
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-21 10:09:24 +11:00
Nicholas Nethercote
cedb787f6e Remove MatcherPosHandle.
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-21 10:08:29 +11:00
Camille GILLOT
056951d628 Take &mut Diagnostic in emit_diagnostic.
Taking a Diagnostic by move would break the usual pattern
`diag.label(..).emit()`.
2022-03-20 20:36:08 +01:00
Nicholas Nethercote
10644e0789 Remove an impossible code path.
Doc comments cannot appear in a matcher.
2022-03-19 09:44:47 +11:00
Nicholas Nethercote
39810a85da Add TtParser::macro_name.
Instead of passing it into `parse_tt`.
2022-03-19 09:44:44 +11:00
Nicholas Nethercote
354bd1071c Rename bb_items_ambiguity_error as ambiguity_error.
Because it involves `next_items` as well as `bb_items`.
2022-03-19 08:04:06 +11:00
Nicholas Nethercote
d21b4f30c1 Introduce TtParser.
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-19 07:47:22 +11:00
bors
a8adf7685a Auto merge of #95067 - nnethercote:parse_tt-more-refactoring, r=petrochenkov
Still more refactoring of `parse_tt`

r? `@petrochenkov`
2022-03-18 12:34:05 +00:00
Nicholas Nethercote
440a685575 Rename TtSeq as TtSlice.
It's a better name because (a) it holds a slice, and (b) "sequence" has
other meanings in this file.
2022-03-18 17:47:08 +11:00
Nicholas Nethercote
f43028d06f Tweak a bunch of comments.
I've been staring at these enough lately that they're annoying me, let's
make them better.
2022-03-18 17:22:34 +11:00
Nicholas Nethercote
14875a5564 Reorder cases in parse_tt_inner.
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-18 14:21:13 +11:00
Nicholas Nethercote
83044714a1 Only modify eof_items if token == Eof.
Because that's the condition under which `eof_items` is used.
2022-03-18 14:11:01 +11:00
Nicholas Nethercote
8bd1bcad58 Factor out some code into MatcherPos::repetition.
Also move `create_matches` within `impl MatcherPos`, because it's only
used within that impl block.
2022-03-18 14:09:02 +11:00
Nicholas Nethercote
5bbbee5ba7 Add two useful assertions. 2022-03-18 13:57:11 +11:00
mark
bb8d4307eb rustc_error: make ErrorReported impossible to construct
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-16 10:35:24 -05:00
bors
95561b336c Auto merge of #94584 - pnkfelix:inject-use-suggestion-sites, r=ekuber
More robust fallback for `use` suggestion

Our old way to suggest where to add `use`s would first look for pre-existing `use`s in the relevant crate/module, and if there are *no* uses, it would fallback on trying to use another item as the basis for the suggestion.

But this was fragile, as illustrated in issue #87613

This PR instead identifies span of the first token after any inner attributes, and uses *that* as the fallback for the `use` suggestion.

Fix #87613
2022-03-15 03:56:33 +00:00
Caio
5d333c155e Fix remaining meta-variable expression TODOs 2022-03-14 08:29:20 -03:00
Matthias Krüger
8c87132103
Rollup merge of #94833 - c410-f3r:meta-take-2, r=petrochenkov
[2/2] Implement macro meta-variable expression

Final part of https://github.com/rust-lang/rust/pull/93545#issuecomment-1050963295

r? `@petrochenkov`
2022-03-12 09:35:45 +01:00
Caio
d0eca08bc4 Implement macro meta-variable expression 2022-03-11 17:48:51 -03:00
Nicholas Nethercote
95d13fa37d Move a parse_tt error case into a separate function. 2022-03-11 14:10:21 +11:00
Nicholas Nethercote
235a87fbd3 Make next_items a SmallVec.
For consistency, and to make the code slightly nicer.
2022-03-11 14:10:21 +11:00
Nicholas Nethercote
c13ca42d67 Move eof_items handling entirely within inner_parse_loop.
Also rename `inner_parse_loop` as `parse_tt_inner`, because it's no
longer just a loop.
2022-03-11 14:10:21 +11:00
Nicholas Nethercote
9f0798b2eb Add a useful assertion. 2022-03-11 14:10:21 +11:00
Nicholas Nethercote
4d4baf7c9a Disallow TokenTree::{MetaVar,MetaVarExpr} in matchers.
They should only appear in transcribers.
2022-03-11 14:10:19 +11:00
Nicholas Nethercote
09c3e82050 Refactor the second half of parse_tt.
The current structure makes it hard to tell that there are just four
distinct code paths, depending on how many items there are in `bb_items`
and `next_items`. This commit introduces a `match` that clarifies
things.
2022-03-11 13:56:54 +11:00
Caio
8073a88f35 Implement macro meta-variable expressions 2022-03-09 16:46:23 -03:00
bors
65f6d33b77 Auto merge of #94096 - cjgillot:ensure-stability, r=lcnr
Ensure stability directives are checked in all cases

Split off  #93017

Stability and deprecation were not checked in all cases, for instance if a type error happened.
This PR moves the check earlier in the pipeline to ensure the errors are emitted in all cases.

r? `@lcnr`
2022-03-04 05:49:14 +00:00
Felix S. Klock II
b82795244e Associate multiple with a crate too. 2022-03-03 18:45:25 -05:00
Felix S. Klock II
e9035f7bef refactor: prepare to associate multiple spans with a module. 2022-03-03 14:38:50 -05:00
Matthias Krüger
939c1585a4
Rollup merge of #94555 - cuishuang:master, r=oli-obk
all: fix some typos

Signed-off-by: cuishuang <imcusg@gmail.com>
2022-03-03 20:01:48 +01:00
Matthias Krüger
26cbf9158d
Rollup merge of #94554 - Urgau:stmt-node-id-ice, r=petrochenkov
Fix invalid lint_node_id being put on a removed stmt

This pull-request remove a invalid `assign_id!` being put on an stmt node.

The problem is that this node is being removed away by a cfg making it unreachable when triggering a buffered lint.
The comment in the other match arm already tell to not assign a id because it could have a `#[cfg()]` so this is just respecting the comment.

Fixes https://github.com/rust-lang/rust/issues/94523
r? ```````@petrochenkov```````
2022-03-03 20:01:48 +01:00
Matthias Krüger
16c6594f34
Rollup merge of #94547 - nnethercote:parse_tt-cleanups, r=petrochenkov
`parse_tt` cleanups

I've been looking closely at this code, and saw some opportunities to improve its readability.

r? ```````@petrochenkov```````
2022-03-03 20:01:45 +01:00