summary refs log tree commit diff
path: root/compiler/rustc_ast/src/tokenstream.rs
AgeCommit message (Collapse)AuthorLines
2024-12-18Overhaul `TokenTreeCursor`.Nicholas Nethercote-37/+4
- Move it to `rustc_parse`, which is the only crate that uses it. This lets us remove all the `pub` markers from it. - Change `next_ref` and `look_ahead` to `get` and `bump`, which work better for the `rustc_parse` uses. - This requires adding a `TokenStream::get` method, which is simple. - In `TokenCursor`, we currently duplicate the `DelimSpan`/`DelimSpacing`/`Delimiter` from the surrounding `TokenTree::Delimited` in the stack. This isn't necessary so long as we don't prematurely move past the `Delimited`, and is a small perf win on a very hot code path. - In `parse_token_tree`, we clone the relevant `TokenTree::Delimited` instead of constructing an identical one from pieces.
2024-12-18Remove `Peekable<TokenStreamIter>` uses.Nicholas Nethercote-1/+4
Currently there are two ways to peek at a `TokenStreamIter`. - Wrap it in a `Peekable` and use that traits `peek` method. - Use `TokenStreamIter`'s inherent `peek` method. Some code uses one, some use the other. This commit converts all places to the inherent method. This eliminates mixing of `TokenStreamIter` and `Peekable<TokenStreamIter>` and some use of `impl Iterator` and `dyn Iterator`.
2024-12-18Rename `RefTokenTreeCursor`.Nicholas Nethercote-18/+16
Because `TokenStreamIter` is a much better name for a `TokenStream` iterator. Also rename the `TokenStream::trees` method as `TokenStream::iter`, and some local variables.
2024-12-18Simplify `RefTokenTreeCursor::look_ahead`.Nicholas Nethercote-2/+2
It's only ever used with a lookahead of 0, so this commit removes the lookahead and renames it `peek`.
2024-11-21Introduce `InvisibleOrigin` on invisible delimiters.Nicholas Nethercote-3/+3
It's not used meaningfully yet, but will be needed to get rid of interpolated tokens.
2024-11-12Delete the `cfg(not(parallel))` serial compilerNoratrieb-1/+0
Since it's inception a long time ago, the parallel compiler and its cfgs have been a maintenance burden. This was a necessary evil the allow iteration while not degrading performance because of synchronization overhead. But this time is over. Thanks to the amazing work by the parallel working group (and the dyn sync crimes), the parallel compiler has now been fast enough to be shipped by default in nightly for quite a while now. Stable and beta have still been on the serial compiler, because they can't use `-Zthreads` anyways. But this is quite suboptimal: - the maintenance burden still sucks - we're not testing the serial compiler in nightly Because of these reasons, it's time to end it. The serial compiler has served us well in the years since it was split from the parallel one, but it's over now. Let the knight slay one head of the two-headed dragon!
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-09-06Add initial support for raw lifetimesMichael Goulet-2/+2
2024-07-29Reformat `use` declarations.Nicholas Nethercote-7/+8
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-10Add some comments.Nicholas Nethercote-1/+4
Explaining things that took me some time to work out.
2024-07-10Factor out `AttrsTarget` flattening code.Nicholas Nethercote-64/+68
This commit does the following. - Pulls the code out of `AttrTokenStream::to_token_trees` into a new function `attrs_and_tokens_to_token_trees`. - Simplifies `TokenStream::from_ast` by calling the new function. This is nicer than the old way, which created a temporary `AttrTokenStream` containing a single `AttrsTarget` (which required some cloning) just to call `to_token_trees` on it. (It is good to remove this use of `AttrsTarget` which isn't related to `cfg_attr` expansion.)
2024-07-10Rework `Attribute::get_tokens`.Nicholas Nethercote-4/+5
Returning `Vec<TokenTree>` works better for the call sites than returning `TokenStream`.
2024-07-07Add an size assertion.Nicholas Nethercote-0/+1
`Option<LazyAttrTokenStream>` is the type that's actually used in all the aST nodes.
2024-07-07Rename some attribute types for consistency.Nicholas Nethercote-11/+11
- `AttributesData` -> `AttrsTarget` - `AttrTokenTree::Attributes` -> `AttrTokenTree::AttrsTarget` - `FlatToken::AttrTarget` -> `FlatToken::AttrsTarget`
2024-07-07Remove `HasSpan` trait.Nicholas Nethercote-3/+3
The only place it is meaningfully used is in a panic message in `TokenStream::from_ast`. But `node.span()` doesn't need to be printed because `node` is also printed and it must contain the span.
2024-07-07Rename `Attribute::tokens` (the inherent method).Nicholas Nethercote-2/+2
To distinguish it from the `HasTokens` method.
2024-07-02Just `push` in `AttrTokenStream::to_token_trees`.Nicholas Nethercote-16/+12
Currently it uses a mixture of functional style (`flat_map`) and imperative style (`push`), which is a bit hard to read. This commit converts it to fully imperative, which is more concise and avoids the need for `smallvec`.
2024-07-02Rename `TokenStream::new` argument.Nicholas Nethercote-2/+2
`tts` is a better name than `streams` for a `Vec<TokenTree>`.
2024-07-02Change `AttrTokenStream::to_tokenstream` to `to_token_trees`.Nicholas Nethercote-16/+7
I.e. change the return type from `TokenStream` to `Vec<TokenTree>`. Most of the callsites require a `TokenStream`, but the recursive call used to create `target_tokens` requires a `Vec<TokenTree>`. It's easy to convert a `Vec<TokenTree>` to a `TokenStream` (just call `TokenStream::new`) but it's harder to convert a `TokenStream` to a `Vec<TokenTree>` (either iterate/clone/collect, or use `Lrc::into_inner` if appropriate). So this commit changes the return value to simplify that `target_tokens` call site.
2024-06-24Fix a typo in a comment.Nicholas Nethercote-1/+1
2024-05-23Use `JointHidden` in a couple of suitable places.Nicholas Nethercote-2/+2
This has no notable effect, but it's appropriate because the relevant tokens are followed by delimiters.
2024-05-14Remove `NtIdent` and `NtLifetime`.Nicholas Nethercote-11/+14
The extra span is now recorded in the new `TokenKind::NtIdent` and `TokenKind::NtLifetime`. These both consist of a single token, and so there's no operator precedence problems with inserting them directly into the token stream. The other way to do this would be to wrap the ident/lifetime in invisible delimiters, but there's a lot of code that assumes an interpolated ident/lifetime fits in a single token, and changing all that code to work with invisible delimiters would have been a pain. (Maybe it could be done in a follow-up.) This change might not seem like much of a win, but it's a first step toward the much bigger and long-desired removal of `Nonterminal` and `TokenKind::Interpolated`. That change is big and complex enough that it's worth doing this piece separately. (Indeed, this commit is based on part of a late commit in #114647, a prior attempt at that big and complex change.)
2024-05-13Remove a `Span` from `TokenKind::Interpolated`.Nicholas Nethercote-3/+3
This span records the declaration of the metavariable in the LHS of the macro. It's used in a couple of error messages. Unfortunately, it gets in the way of the long-term goal of removing `TokenKind::Interpolated`. So this commit removes it, which degrades a couple of (obscure) error messages but makes things simpler and enables the next commit.
2024-05-07compiler: derive Debug in parserJubilee Young-1/+1
It's annoying to debug the parser if you have to stop every five seconds to add a Debug impl.
2024-04-29Remove `extern crate rustc_macros` from numerous crates.Nicholas Nethercote-1/+1
2024-04-24Make `LazyAttrTokenStream::encode` panic.Nicholas Nethercote-3/+2
It's unreachable, because AST JSON printing support was removed some time ago.
2024-04-18Simplify `static_assert_size`s.Nicholas Nethercote-1/+1
We want to run them on all 64-bit platforms.
2024-04-03Check `x86_64` size assertions on `aarch64`, tooZalathar-1/+1
This makes it easier for contributors on aarch64 workstations (e.g. Macs) to notice when these assertions have been violated.
2024-03-21Shrink the comment on `TokenTree`.Nicholas Nethercote-12/+1
It uses very old language that is more confusing today than helpful, including references to `SubstNt` that no longer exists. The comment above `TokenStream` is better, and suffices for a basic understanding of these types.
2024-02-20Add newtype for raw identsclubby789-1/+1
2024-01-06Auto merge of #119478 - bjorn3:no_serialize_specialization, r=wesleywiserbors-4/+4
Avoid specialization in the metadata serialization code With the exception of a perf-only specialization for byte slices and byte vectors. This uses the same trick of introducing a new trait and having the Encodable and Decodable derives add a bound to it as used for TyEncoder/TyDecoder. The new code is clearer about which encoder/decoder uses which impl and it reduces the dependency of rustc on specialization, making it easier to remove support for specialization entirely or turn it into a construct that is only allowed for perf optimizations if we decide to do this.
2024-01-04macro_rules: Less hacky heuristic for using `tt` metavariable spansVadim Petrochenkov-22/+1
2023-12-31Avoid specialization for the Span Encodable and Decodable implsbjorn3-4/+4
2023-12-11Add spacing information to delimiters.Nicholas Nethercote-20/+35
This is an extension of the previous commit. It means the output of something like this: ``` stringify!(let a: Vec<u32> = vec![];) ``` goes from this: ``` let a: Vec<u32> = vec![] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![]; ```
2023-12-11Improve `print_tts` by changing `tokenstream::Spacing`.Nicholas Nethercote-18/+64
`tokenstream::Spacing` appears on all `TokenTree::Token` instances, both punct and non-punct. Its current usage: - `Joint` means "can join with the next token *and* that token is a punct". - `Alone` means "cannot join with the next token *or* can join with the next token but that token is not a punct". The fact that `Alone` is used for two different cases is awkward. This commit augments `tokenstream::Spacing` with a new variant `JointHidden`, resulting in: - `Joint` means "can join with the next token *and* that token is a punct". - `JointHidden` means "can join with the next token *and* that token is a not a punct". - `Alone` means "cannot join with the next token". This *drastically* improves the output of `print_tts`. For example, this: ``` stringify!(let a: Vec<u32> = vec![];) ``` currently produces this string: ``` let a : Vec < u32 > = vec! [] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![] ; ``` (The space after the `]` is because `TokenTree::Delimited` currently doesn't have spacing information. The subsequent commit fixes this.) The new `print_tts` doesn't replicate original code perfectly. E.g. multiple space characters will be condensed into a single space character. But it's much improved. `print_tts` still produces the old, uglier output for code produced by proc macros. Because we have to translate the generated code from `proc_macro::Spacing` to the more expressive `token::Spacing`, which results in too much `proc_macro::Along` usage and no `proc_macro::JointHidden` usage. So `space_between` still exists and is used by `print_tts` in conjunction with the `Spacing` field. This change will also help with the removal of `Token::Interpolated`. Currently interpolated tokens are pretty-printed nicely via AST pretty printing. `Token::Interpolated` removal will mean they get printed with `print_tts`. Without this change, that would result in much uglier output for code produced by decl macro expansions. With this change, AST pretty printing and `print_tts` produce similar results. The commit also tweaks the comments on `proc_macro::Spacing`. In particular, it refers to "compound tokens" rather than "multi-char operators" because lifetimes aren't operators.
2023-11-16More detail when expecting expression but encountering bad macro argumentEsteban Küber-2/+2
Partially address #71039.
2023-10-13Format all the let chains in compilerMichael Goulet-1/+3
2023-09-04improve `AttrTokenStream`mojave2-11/+7
2023-07-31Remove `TokenTreeCursor::replace_prev_and_rewind`.Nicholas Nethercote-9/+0
It's no longer used.
2023-07-31Move doc comment desugaring out of `TokenCursor`.Nicholas Nethercote-3/+89
`TokenCursor` currently does doc comment desugaring on the fly, if the `desugar_doc_comment` field is set. This requires also modifying the token stream on the fly with `replace_prev_and_rewind`. This commit moves the doc comment desugaring out of `TokenCursor`, by introducing a new `TokenStream::desugar_doc_comment` method. This separation of desugaring and iterating makes the code nicer.
2023-07-27Remove `Iterator` impl for `TokenTreeCursor`.Nicholas Nethercote-13/+8
This is surprising, but the new comment explains why. It's a logical conclusion in the drive to avoid `TokenTree` clones. `TokenTreeCursor` is now only used within `Parser`. It's still needed due to `replace_prev_and_rewind`.
2023-07-27Make `TokenTree::uninterpolate` take `&self` and return a `Cow`.Nicholas Nethercote-5/+7
Making it similar to `Token::uninterpolate`. This avoids some more token tree cloning.
2023-07-03Use Lrc::make_mut instead of Lrc::get_mutThe 8472-10/+4
2023-07-03perform TokenStream replacement in-place when possible in expand_macroThe 8472-3/+18
2023-05-13refactor: add chunks method to TokenStream to obviate rustdoc clonesCaleb Cartwright-0/+4
2023-05-06correct literals for dyn thread safeSparrowLii-1/+1
2023-05-06introduce `DynSend` and `DynSync` auto traitSparrowLii-6/+7
2023-02-03Rename `Cursor`/`CursorRef` as `TokenTreeCursor`/`RefTokenTreeCursor`.Nicholas Nethercote-14/+16
This makes it clear they return token trees, and makes for a nice comparison against `TokenCursor` which returns tokens.
2023-02-03Make clear that `TokenTree::Token` shouldn't contain a delimiter.Nicholas Nethercote-1/+2
2023-02-03Improve doc comment desugaring.Nicholas Nethercote-0/+9
Sometimes the parser needs to desugar a doc comment into `#[doc = r"foo"]`. Currently it does this in a hacky way: by pushing a "fake" new frame (one without a delimiter) onto the `TokenCursor` stack. This commit changes things so that the token stream itself is modified in place. The nice thing about this is that it means `TokenCursorFrame::delim_sp` is now only `None` for the outermost frame.