about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/nonterminal.rs
AgeCommit message (Collapse)AuthorLines
2025-08-09remove `P`Deadbeef-9/+7
2025-04-21Remove `token::{Open,Close}Delim`.Nicholas Nethercote-8/+6
By replacing them with `{Open,Close}{Param,Brace,Bracket,Invisible}`. PR #137902 made `ast::TokenKind` more like `lexer::TokenKind` by replacing the compound `BinOp{,Eq}(BinOpToken)` variants with fieldless variants `Plus`, `Minus`, `Star`, etc. This commit does a similar thing with delimiters. It also makes `ast::TokenKind` more similar to `parser::TokenType`. This requires a few new methods: - `TokenKind::is_{,open_,close_}delim()` replace various kinds of pattern matches. - `Delimiter::as_{open,close}_token_kind` are used to convert `Delimiter` values to `TokenKind`. Despite these additions, it's a net reduction in lines of code. This is because e.g. `token::OpenParen` is so much shorter than `token::OpenDelim(Delimiter::Parenthesis)` that many multi-line forms reduce to single line forms. And many places where the number of lines doesn't change are still easier to read, just because the names are shorter, e.g.: ``` - } else if self.token != token::CloseDelim(Delimiter::Brace) { + } else if self.token != token::CloseBrace { ```
2025-04-02Impl `Copy` for `Token` and `TokenKind`.Nicholas Nethercote-2/+2
2025-04-02Remove `NtBlock`, `Nonterminal`, and `TokenKind::Interpolated`.Nicholas Nethercote-82/+38
`NtBlock` is the last remaining variant of `Nonterminal`, so once it is gone then `Nonterminal` can be removed as well.
2025-04-02Remove `NtExpr` and `NtLiteral`.Nicholas Nethercote-8/+8
Notes about tests: - tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.rs: some messages are now duplicated due to repeated parsing. - tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs: ditto. - `tests/ui/proc-macro/macro-rules-derive-cfg.rs`: the diff looks large but the only difference is the insertion of a single invisible-delimited group around a metavar. - `tests/ui/attributes/nonterminal-expansion.rs`: a slight span degradation, somehow related to the recent massive attr parsing rewrite (#135726). I couldn't work out exactly what is going wrong, but I don't think it's worth holding things up for a single slightly suboptimal error message.
2025-03-07Remove `NtItem` and `NtStmt`.Nicholas Nethercote-7/+5
This involves replacing `nt_pretty_printing_compatibility_hack` with `stream_pretty_printing_compatibility_hack`. The handling of statements in `transcribe` is slightly different to other nonterminal kinds, due to the lack of `from_ast` implementation for empty statements. Notable test changes: - `tests/ui/proc-macro/expand-to-derive.rs`: the diff looks large but the only difference is the insertion of a single invisible-delimited group around a metavar.
2025-02-28Remove `NtPath`.Nicholas Nethercote-3/+5
2025-02-28Remove `NtMeta`.Nicholas Nethercote-5/+6
Note: there was an existing code path involving `Interpolated` in `MetaItem::from_tokens` that was dead. This commit transfers that to the new form, but puts an `unreachable!` call inside it.
2025-02-28Remove `NtPat`.Nicholas Nethercote-11/+13
The one notable test change is `tests/ui/macros/trace_faulty_macros.rs`. This commit removes the complicated `Interpolated` handling in `expected_expression_found` that results in a longer error message. But I think the new, shorter message is actually an improvement. The original complaint was in #71039, when the error message started with "error: expected expression, found `1 + 1`". That was confusing because `1 + 1` is an expression. Other than that, the reporter said "the whole error message is not too bad if you ignore the first line". Subsequently, extra complexity and wording was added to the error message. But I don't think the extra wording actually helps all that much. In particular, it still says of the `1+1` that "this is expected to be expression". This repeats the problem from the original complaint! This commit removes the extra complexity, reverting to a simpler error message. This is primarily because the traversal is a pain without `Interpolated` tokens. Nonetheless, I think the error message is *improved*. It now starts with "expected expression, found `pat` metavariable", which is much clearer and the real problem. It also doesn't say anything specific about `1+1`, which is good, because the `1+1` isn't really relevant to the error -- it's the `$e:pat` that's important.
2025-02-21Avoid snapshotting the parser in `parse_path_inner`.Nicholas Nethercote-2/+2
2025-02-21Remove `NtTy`.Nicholas Nethercote-3/+4
Notes about tests: - tests/ui/parser/macro/trait-object-macro-matcher.rs: the syntax error is duplicated, because it occurs now when parsing the decl macro input, and also when parsing the expanded decl macro. But this won't show up for normal users due to error de-duplication. - tests/ui/associated-consts/issue-93835.rs: similar, plus there are some additional errors about this very broken code. - The changes to metavariable descriptions in #132629 are now visible in error message for several tests.
2025-02-21Remove `NtVis`.Nicholas Nethercote-7/+6
We now use invisible delimiters for expanded `vis` fragments, instead of `Token::Interpolated`.
2025-02-03tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`Askar Safin-2/+3
2024-12-18Re-export more `rustc_span::symbol` things from `rustc_span`.Nicholas Nethercote-1/+1
`rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason. This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers in `compiler/` to `rustc_span::`. This is a 200+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one.
2024-11-24refactor pat parser method names/doc-comments to agree with RFC 3637Max Niederman-1/+1
2024-11-21Prepare for invisible delimiters.Nicholas Nethercote-4/+46
Current places where `Interpolated` is used are going to change to instead use invisible delimiters. This prepares for that. - It adds invisible delimiter cases to the `can_begin_*`/`may_be_*` methods and the `failed_to_match_macro` that are equivalent to the existing `Interpolated` cases. - It adds panics/asserts in some places where invisible delimiters should never occur. - In `Parser::parse_struct_fields` it excludes an ident + invisible delimiter from special consideration in an error message, because that's quite different to an ident + paren/brace/bracket.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-06Add initial support for raw lifetimesMichael Goulet-3/+3
2024-08-26Don't make pattern nonterminals match statement nonterminalsMichael Goulet-19/+1
2024-07-31Rollup merge of #126697 - vincenzopalazzo:macros/find_the_expression_tok, ↵Matthias Krüger-1/+10
r=eholk,compiler-errors [RFC] mbe: consider the `_` in 2024 an expression This commit is adding the possibility to parse the `_` as an expression inside the esition 2024. Link: https://rust-lang.zulipchat.com/#narrow/stream/404510-wg-macros/topic/supporting.20.60_.60.20expressions Issue https://github.com/rust-lang/rust/issues/123742 r? `@eholk`
2024-07-31tweak comment on `NonterminalKind::Expr`Michael Goulet-1/+1
Co-authored-by: Eric Holk <eric@theincredibleholk.org>
2024-07-31rustc_parser: consider the in 2024 an expressionVincenzo Palazzo-1/+10
This commit is adding the possibility to parse the `_` as an expression inside the esition 2024. Link: https://rust-lang.zulipchat.com/#narrow/stream/404510-wg-macros/topic/supporting.20.60_.60.20expressions Co-authored-by: Eric Holk <eric@theincredibleholk.org> Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-07-29Reformat `use` declarations.Nicholas Nethercote-3/+4
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-19Use `ForceCollect` in `parse_attr_item`.Nicholas Nethercote-1/+1
Instead of a `bool`. Because `ForceCollect` is used in this way everywhere else.
2024-07-16Deny keyword lifetimes pre-expansionMichael Goulet-2/+5
2024-06-23Rework pattern and expression nonterminal kinds.Nicholas Nethercote-16/+12
Merge `PatParam`/`PatWithOr`, and `Expr`/`Expr2021`, for a few reasons. - It's conceptually nice, because the two pattern kinds and the two expression kinds are very similar. - With expressions in particular, there are several places where both expression kinds get the same treatment. - It removes one unreachable match arm. - Most importantly, for #124141 I will need to introduce a new type `MetaVarKind` that is very similar to `NonterminalKind`, but records a couple of extra fields for expression metavars. It's nicer to have a single `MetaVarKind::Expr` expression variant to hold those extra fields instead of duplicating them across two variants `MetaVarKind::{Expr,Expr2021}`. And then it makes sense for patterns to be treated the same way, and for `NonterminalKind` to also be treated the same way. I also clarified the comments, because I have long found them a little hard to understand.
2024-06-19Allow naming expr_2021 in all editionsMichael Goulet-3/+2
2024-06-02Avoid checking the edition as much as possibleVincenzo Palazzo-1/+1
Inside #123865, we are adding support for the new semantics for expr2024, but we have noted a performance issue. We realized there is a redundant check for each token regarding an edition. This commit moves the edition check to the end, avoiding some extra checks that can slow down compilation time. Link: https://github.com/rust-lang/rust/pull/123865 Co-Developed-by: @eholk Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-05-17Auto merge of #123865 - eholk:expr_2021, r=fmeasebors-2/+10
Update `expr` matcher for Edition 2024 and add `expr_2021` nonterminal This commit adds a new nonterminal `expr_2021` in macro patterns, and `expr_fragment_specifier_2024` feature flag. This change also updates `expr` so that on Edition 2024 it will also match `const { ... }` blocks, while `expr_2021` preserves the current behavior of `expr`, matching expressions without `const` blocks. Joint work with `@vincenzopalazzo.` Issue #123742
2024-05-17Update compiler/rustc_parse/src/parser/nonterminal.rsEric Holk-2/+1
Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
2024-05-14Remove `NtIdent` and `NtLifetime`.Nicholas Nethercote-26/+27
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-13Macros: match const { ... } with expr nonterminal in edition 2024Vincenzo Palazzo-1/+8
Co-authored-by: Eric Holk <eric@theincredibleholk.org> Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-05-13Add expr_2021 nonterminal and feature flagEric Holk-2/+4
This commit adds a new nonterminal `expr_2021` in macro patterns, and `expr_fragment_specifier_2024` feature flag. For now, `expr` and `expr_2021` are treated the same, but in future PRs we will update `expr` to match to new grammar. Co-authored-by: Vincezo Palazzo <vincenzopalazzodev@gmail.com>
2024-05-13Remove a `Span` from `TokenKind::Interpolated`.Nicholas Nethercote-10/+8
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-04-04Rename ModSep to PathSepLeón Orell Valerian Liehr-2/+2
2024-03-21Streamline `NamedMatch`.Nicholas Nethercote-2/+5
This commit combines `MatchedTokenTree` and `MatchedNonterminal`, which are often considered together, into a single `MatchedSingle`. It shares a representation with the newly-parameterized `ParseNtResult`. This will also make things much simpler if/when variants from `Interpolated` start being moved to `ParseNtResult`.
2024-03-21Fix some formatting.Nicholas Nethercote-5/+3
2024-02-20Add newtype for raw identsclubby789-1/+1
2023-12-18Use `.into_diagnostic()` less.Nicholas Nethercote-12/+10
This commit replaces this pattern: ``` err.into_diagnostic(dcx) ``` with this pattern: ``` dcx.create_err(err) ``` in a lot of places. It's a little shorter, makes the error level explicit, avoids some `IntoDiagnostic` imports, and is a necessary prerequisite for the next commit which will add a `level` arg to `into_diagnostic`. This requires adding `track_caller` on `create_err` to avoid mucking up the output of `tests/ui/track-diagnostics/track4.rs`. It probably should have been there already.
2023-12-18Rename `Parser::span_diagnostic` as `Parser::dcx`.Nicholas Nethercote-5/+6
2023-12-02Use `Session::diagnostic` in more places.Nicholas Nethercote-4/+4
2023-11-16More detail when expecting expression but encountering bad macro argumentEsteban Küber-7/+7
Partially address #71039.
2023-10-13Format all the let chains in compilerMichael Goulet-19/+18
2023-08-18Rename `NtOrTt` as `ParseNtResult`.Nicholas Nethercote-4/+4
It's more descriptive, and future-proofs it if/when additional variants get added.
2023-08-17Simplify a `match`.Nicholas Nethercote-4/+1
`may_be_ident` is true for `NtPath` and `NtMeta`, so we don't need to check for them separately.
2023-08-17Make some `match`es exhaustive in `nonterminal.rs`.Nicholas Nethercote-5/+20
For ones matching more than one or two variants, this is easier to think about.
2023-08-17Use `Nonterminal::*` in `nonterminal.rs`.Nicholas Nethercote-26/+17
It makes the code more readable.
2023-08-17Remove unnecessary braces on `PatWithOr` patterns.Nicholas Nethercote-4/+4
2023-08-17Rename `parse_no_question_mark_recover`.Nicholas Nethercote-1/+1
Adding a `ty_` makes its purpose much clearer, and consistent with other `parse_ty_*` functions.
2023-08-17Remove outdated comment.Nicholas Nethercote-2/+0
All nonterminals collect and store tokens now. (Unless they are very simple, e.g. single-token, and can precisely recover them without collecting.)