about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/nonterminal.rs
AgeCommit message (Collapse)AuthorLines
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.)
2023-08-03Avoid too many expected symbols and reduce `None`sr0cky-1/+1
2023-08-03Keep the suggestion for wrong arbitrary self typesMu001999-2/+2
2023-04-16use matches! macro in more placesMatthias Krüger-6/+4
2023-02-01rustc_parse: migrate more to diagnostic structsXiretza-8/+14
2022-11-22`rustc_parse`: remove `ref` patternsMaybe Waffle-8/+8
2022-09-15slight vertical formattingRageking8-12/+12
2022-05-11ast: Introduce some traits to get AST node properties genericallyVadim Petrochenkov-1/+1
And use them to avoid constructing some artificial `Nonterminal` tokens during expansion
2022-04-28rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter`Vadim Petrochenkov-4/+4
2022-04-20Inline `Parser::nonterminal_may_begin_with`.Nicholas Nethercote-2/+4
2022-04-20Inline `Parser::parse_nonterminal`.Nicholas Nethercote-1/+3
2022-04-07Shrink `Nonterminal`.Nicholas Nethercote-3/+3
By heap allocating the argument within `NtPath`, `NtVis`, and `NtStmt`. This slightly reduces cumulative and peak allocation amounts, most notably on `deep-vector`.
2022-03-28Remove `Nonterminal::NtTT`.Nicholas Nethercote-12/+17
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-04Do not recover from `Ty?` in macro parsingEsteban Kuber-1/+1
Follow up to #92746. Address #94510.
2022-02-28Tweak diagnosticsEsteban Kuber-2/+2
* Recover from invalid `'label: ` before block. * Make suggestion to enclose statements in a block multipart. * Point at `match`, `while`, `loop` and `unsafe` keywords when failing to parse their expression. * Do not suggest `{ ; }`. * Do not suggest `|` when very unlikely to be what was wanted (in `let` statements).
2021-08-25Use if-let guards in the codebaseLéo Lanteri Thauvin-8/+9
2021-07-14Suggest a path separator if a stray colon is found in a match armFabian Wolff-2/+2
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
2021-06-06parser: Ensure that all nonterminals have tokens after parsingVadim Petrochenkov-2/+15
2021-04-27remove pat2021mark-4/+4
2021-04-15rename pat2015 to pat_parammark-4/+6
2021-04-11Implement token-based handling of attributes during expansionAaron Hill-3/+1
This PR modifies the macro expansion infrastructure to handle attributes in a fully token-based manner. As a result: * Derives macros no longer lose spans when their input is modified by eager cfg-expansion. This is accomplished by performing eager cfg-expansion on the token stream that we pass to the derive proc-macro * Inner attributes now preserve spans in all cases, including when we have multiple inner attributes in a row. This is accomplished through the following changes: * New structs `AttrAnnotatedTokenStream` and `AttrAnnotatedTokenTree` are introduced. These are very similar to a normal `TokenTree`, but they also track the position of attributes and attribute targets within the stream. They are built when we collect tokens during parsing. An `AttrAnnotatedTokenStream` is converted to a regular `TokenStream` when we invoke a macro. * Token capturing and `LazyTokenStream` are modified to work with `AttrAnnotatedTokenStream`. A new `ReplaceRange` type is introduced, which is created during the parsing of a nested AST node to make the 'outer' AST node aware of the attributes and attribute target stored deeper in the token stream. * When we need to perform eager cfg-expansion (either due to `#[derive]` or `#[cfg_eval]`), we tokenize and reparse our target, capturing additional information about the locations of `#[cfg]` and `#[cfg_attr]` attributes at any depth within the target. This is a performance optimization, allowing us to perform less work in the typical case where captured tokens never have eager cfg-expansion run.
2021-03-25Avoid double-collection for expression nonterminalsAaron Hill-16/+1
2021-03-23Rollup merge of #83384 - mark-i-m:rename-pat2018, r=joshtriplettYuki Okushi-3/+3
rename :pat2018 -> :pat2015 as requested by T-lang on zulip: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/or.20patterns/near/231133873 No functional changes here... just renaming. r? `@nikomatsakis`
2021-03-22rename :pat2018 -> :pat215mark-3/+3
2021-03-19stabilize or_patternsmark-2/+2
2021-02-15Simplify pattern grammar by allowing nested leading vertmark-2/+2
Along the way, we also implement a handful of diagnostics improvements and fixes, particularly with respect to the special handling of `||` in place of `|` and when there are leading verts in function params, which don't allow top-level or-patterns anyway.
2021-02-13Address review commentsAaron Hill-1/+1
2021-02-13Require passing an `AttrWrapper` to `collect_tokens_trailing_token`Aaron Hill-10/+34
This is a pure refactoring split out from #80689. It represents the most invasive part of that PR, requiring changes in every caller of `parse_outer_attributes` In order to eagerly expand `#[cfg]` attributes while preserving the original `TokenStream`, we need to know the range of tokens that corresponds to every attribute target. This is accomplished by making `parse_outer_attributes` return an opaque `AttrWrapper` struct. An `AttrWrapper` must be converted to a plain `AttrVec` by passing it to `collect_tokens_trailing_token`. This makes it difficult to accidentally construct an AST node with attributes without calling `collect_tokens_trailing_token`, since AST nodes store an `AttrVec`, not an `AttrWrapper`. As a result, we now call `collect_tokens_trailing_token` for attribute targets which only support inert attributes, such as generic arguments and struct fields. Currently, the constructed `LazyTokenStream` is simply discarded. Future PRs will record the token range corresponding to the attribute target, allowing those tokens to be removed from an enclosing `collect_tokens_trailing_token` call if necessary.
2021-02-02Bump rustfmt versionMark Rousskov-7/+10
Also switches on formatting of the mir build module
2021-01-20Force token collection to run when parsing nonterminalsAaron Hill-3/+3
Fixes #81007 Previously, we would fail to collect tokens in the proper place when only builtin attributes were present. As a result, we would end up with attribute tokens in the collected `TokenStream`, leading to duplication when we attempted to prepend the attributes from the AST node. We now explicitly track when token collection must be performed due to nomterminal parsing.
2021-01-13Set tokens on AST node in `collect_tokens`Aaron Hill-81/+18
A new `HasTokens` trait is introduced, which is used to move logic from the callers of `collect_tokens` into the body of `collect_tokens`. In addition to reducing duplication, this paves the way for PR #80689, which needs to perform additional logic during token collection.
2020-12-30Implement edition-based macro pat featuremark-17/+10
2020-12-19implement edition-specific :pat behavior for 2015/18mark-3/+20
2020-12-17Don't allow `const` to begin a nonterminalCamelid-0/+2
Thanks to Vadim Petrochenkov who [told me what the fix was][z]! [z]: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/finding.20which.20macro.20rule.20to.20use/near/220240422
2020-11-26Properly handle attributes on statementsAaron Hill-2/+2
We now collect tokens for the underlying node wrapped by `StmtKind` instead of storing tokens directly in `Stmt`. `LazyTokenStream` now supports capturing a trailing semicolon after it is initially constructed. This allows us to avoid refactoring statement parsing to wrap the parsing of the semicolon in `parse_tokens`. Attributes on item statements (e.g. `fn foo() { #[bar] struct MyStruct; }`) are now treated as item attributes, not statement attributes, which is consistent with how we handle attributes on other kinds of statements. The feature-gating code is adjusted so that proc-macro attributes are still allowed on item statements on stable. Two built-in macros (`#[global_allocator]` and `#[test]`) needed to be adjusted to support being passed `Annotatable::Stmt`.
2020-10-30Fix even more clippy warningsJoshua Nelson-14/+8
2020-10-22Don't create an empty `LazyTokenStream`Aaron Hill-10/+10
2020-10-21Unconditionally capture tokens for attributes.Aaron Hill-1/+1
This allows us to avoid synthesizing tokens in `prepend_attr`, since we have the original tokens available. We still need to synthesize tokens when expanding `cfg_attr`, but this is an unavoidable consequence of the syntax of `cfg_attr` - the user does not supply the `#` and `[]` tokens that a `cfg_attr` expands to.
2020-09-10Attach tokens to `ast::Stmt`Aaron Hill-4/+14
We currently only attach tokens when parsing a `:stmt` matcher for a `macro_rules!` macro. Proc-macro attributes on statements are still unstable, and need additional work.