about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/expr.rs
AgeCommit message (Collapse)AuthorLines
2024-12-08Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-deadMatthias Krüger-32/+26
Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](https://github.com/rust-lang/rust/issues/129967)). This PR is extracted from https://github.com/rust-lang/rust/pull/129996 with minor modifications. cc `@max-niederman`
2024-11-30Eliminate magic numbers from expression precedenceDavid Tolnay-4/+4
2024-11-30Eliminate precedence arithmetic from rustc_parseDavid Tolnay-13/+18
2024-11-28Improve span handling in `parse_expr_bottom`.Nicholas Nethercote-11/+8
`parse_expr_bottom` stores `this.token.span` in `lo`, but then fails to use it in many places where it could. This commit fixes that, and likewise (to a smaller extent) in `parse_ty_common`.
2024-11-24parse guard patternsNadrieril-29/+23
Co-authored-by: Max Niederman <max@maxniederman.com>
2024-11-24refactor pat parser method names/doc-comments to agree with RFC 3637Max Niederman-5/+5
2024-11-21Remove `ErrorGuaranteed` retval from `error_unexpected_after_dot`.Nicholas Nethercote-7/+7
It was added in #130349, but it's not used meaningfully, and causes difficulties for Nonterminal removal in #124141.
2024-11-21Prepare for invisible delimiters.Nicholas Nethercote-2/+10
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-11-16Better account for `else if` macro conditions mising an `if`Esteban Küber-1/+10
If a macro statement has been parsed after `else`, suggest a missing `if`: ``` error: expected `{`, found `falsy` --> $DIR/else-no-if.rs:47:12 | LL | } else falsy! {} { | ---- ^^^^^ | | | expected an `if` or a block after this `else` | help: add an `if` if this is the condition of a chained `else if` statement | LL | } else if falsy! {} { | ++ ```
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-26/+26
2024-09-18Recover more expressions in patternsLieselotte-2/+2
2024-09-14Refactor `Parser::break_up_float`Lieselotte-27/+25
2024-09-14Fix `Parser::break_up_float`'s right spanLieselotte-1/+1
2024-09-14Add `ErrorGuaranteed` to `DestructuredFloat::Error`Lieselotte-7/+7
2024-09-11Also fix if in elseMichael Goulet-7/+5
2024-09-06Add initial support for raw lifetimesMichael Goulet-3/+3
2024-08-21Use bool in favor of Option<()> for diagnosticsMichael Goulet-2/+2
2024-08-18stabilize raw_ref_opRalf Jung-3/+2
2024-08-16Overhaul token collection.Nicholas Nethercote-32/+57
This commit does the following. - Renames `collect_tokens_trailing_token` as `collect_tokens`, because (a) it's annoying long, and (b) the `_trailing_token` bit is less accurate now that its types have changed. - In `collect_tokens`, adds a `Option<CollectPos>` argument and a `UsePreAttrPos` in the return type of `f`. These are used in `parse_expr_force_collect` (for vanilla expressions) and in `parse_stmt_without_recovery` (for two different cases of expression statements). Together these ensure are enough to fix all the problems with token collection and assoc expressions. The changes to the `stringify.rs` test demonstrate some of these. - Adds a new test. The code in this test was causing an assertion failure prior to this commit, due to an invalid `NodeRange`. The extra complexity is annoying, but necessary to fix the existing problems.
2024-08-16Convert a bool to `Trailing`.Nicholas Nethercote-9/+11
This pre-existing type is suitable for use with the return value of the `f` parameter in `collect_tokens_trailing_token`. The more descriptive name will be useful because the next commit will add another boolean value to the return value of `f`.
2024-08-14Use `impl PartialEq<TokenKind> for Token` more.Nicholas Nethercote-35/+34
This lets us compare a `Token` with a `TokenKind`. It's used a lot, but can be used even more, avoiding the need for some `.kind` uses.
2024-08-14Convert a `&mut self` to `&self`.Nicholas Nethercote-1/+1
2024-08-07Use more slice patterns inside the compilerLeón Orell Valerian Liehr-3/+3
2024-08-03Rollup merge of #128483 - nnethercote:still-more-cfg-cleanups, r=petrochenkovMatthias Krüger-46/+29
Still more `cfg` cleanups Found while looking closely at `cfg`/`cfg_attr` processing code. r? `````````@petrochenkov`````````
2024-08-01Fix removed `box_syntax` diagnostic if source isn't availableclubby789-4/+6
2024-08-01Streamline attribute stitching on AST nodes.Nicholas Nethercote-11/+7
It can be done more concisely.
2024-07-31Remove `LhsExpr`.Nicholas Nethercote-35/+22
`parse_expr_assoc_with` has an awkward structure -- sometimes the lhs is already parsed. This commit splits the post-lhs part into a new method `parse_expr_assoc_rest_with`, which makes everything shorter and simpler.
2024-07-29Mark Parser::eat/check methods as must_useMichael Goulet-2/+3
2024-07-29Reformat `use` declarations.Nicholas Nethercote-15/+16
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-22Always pass the visitor as the first argument to walk* functionsOli Scherer-5/+5
2024-07-22Sync `mut_visit` function names with immut `visit` ones (s/noop_visit/walk/)Oli Scherer-6/+6
2024-07-18Remove `TrailingToken`.Nicholas Nethercote-16/+10
It's used in `Parser::collect_tokens_trailing_token` to decide whether to capture a trailing token. But the callers actually know whether to capture a trailing token, so it's simpler for them to just pass in a bool. Also, the `TrailingToken::Gt` case was weird, because it didn't result in a trailing token being captured. It could have been subsumed by the `TrailingToken::MaybeComma` case, and it effectively is in the new code.
2024-07-17Rollup merge of #127806 - nnethercote:parser-improvements, r=spastorinoTrevor Gross-69/+56
Some parser improvements I was looking closely at attribute handling in the parser while debugging some issues relating to #124141, and found a few small improvements. ``@spastorino``
2024-07-16Deny keyword lifetimes pre-expansionMichael Goulet-3/+10
2024-07-16Fix a comment.Nicholas Nethercote-1/+1
2024-07-16Inline and remove `Parser::parse_expr_dot_or_call_with_`.Nicholas Nethercote-53/+49
It only has two call sites, and it extremely similar to `Parser::parse_expr_dot_or_call_with`, in both name and behaviour. The only difference is the latter has an `attrs` argument and an `ensure_sufficient_stack` call. We can pass in an empty `attrs` as necessary, as is already done at some `parse_expr_dot_or_call_with` call sites.
2024-07-16Inline and remove `Parser::parse_and_disallow_postfix_after_cast`.Nicholas Nethercote-13/+4
It has a single call site. Removing it removes the need for an `ExprKind` check. The commit also clarifies the relevant comment.
2024-07-16Reorder `Parser::parse_expr_dot_or_call_with` arguments.Nicholas Nethercote-2/+2
Put `attrs` before `e0` because that matches the order in the source code, where outer attributes appear before expressions.
2024-07-12Make parse error suggestions verbose and fix spansEsteban Küber-5/+6
Go over all structured parser suggestions and make them verbose style. When suggesting to add or remove delimiters, turn them into multiple suggestion parts.
2024-06-29Rollup merge of #127103 - compiler-errors:tighten-trait-bound-parsing, r=fmeaseMatthias Krüger-1/+1
Move binder and polarity parsing into `parse_generic_ty_bound` Let's pull out the parts of #127054 which just: 1. Make the parsing code less confusing 2. Fix `?use<>` (to correctly be denied) 3. Improve `T: for<'a> 'a` diagnostics This should have no user-facing effects on stable parsing. r? fmease
2024-06-28Move binder and polarity parsing into parse_generic_ty_boundMichael Goulet-1/+1
2024-06-27Tighten spans for async blocksMichael Goulet-2/+3
2024-06-25Inline and remove `maybe_whole_expr!`.Nicholas Nethercote-32/+41
And remove the `NtPath` and `NtBlock` cases in `parse_literal_maybe_minus`, because they are unnecessary.
2024-06-20Add blank lines after module-level `//` comments.Nicholas Nethercote-0/+1
Similar to the previous commit.
2024-06-19Auto merge of #126678 - nnethercote:fix-duplicated-attrs-on-nt-expr, ↵bors-81/+74
r=petrochenkov Fix duplicated attributes on nonterminal expressions This PR fixes a long-standing bug (#86055) whereby expression attributes can be duplicated when expanded through declarative macros. First, consider how items are parsed in declarative macros: ``` Items: - parse_nonterminal - parse_item(ForceCollect::Yes) - parse_item_ - attrs = parse_outer_attributes - parse_item_common(attrs) - maybe_whole! - collect_tokens_trailing_token ``` The important thing is that the parsing of outer attributes is outside token collection, so the item's tokens don't include the attributes. This is how it's supposed to be. Now consider how expression are parsed in declarative macros: ``` Exprs: - parse_nonterminal - parse_expr_force_collect - collect_tokens_no_attrs - collect_tokens_trailing_token - parse_expr - parse_expr_res(None) - parse_expr_assoc_with - parse_expr_prefix - parse_or_use_outer_attributes - parse_expr_dot_or_call ``` The important thing is that the parsing of outer attributes is inside token collection, so the the expr's tokens do include the attributes, i.e. in `AttributesData::tokens`. This PR fixes the bug by rearranging expression parsing to that outer attribute parsing happens outside of token collection. This requires a number of small refactorings because expression parsing is somewhat complicated. While doing so the PR makes the code a bit cleaner and simpler, by eliminating `parse_or_use_outer_attributes` and `Option<AttrWrapper>` arguments (in favour of the simpler `parse_outer_attributes` and `AttrWrapper` arguments), and simplifying `LhsExpr`. r? `@petrochenkov`
2024-06-19Change how `parse_expr_force_collect` works.Nicholas Nethercote-2/+5
It now parses outer attributes before collecting tokens. This avoids the problem where the outer attribute tokens were being stored twice -- for the attribute tokesn, and also for the expression tokens. Fixes #86055.
2024-06-19Refactor `parse_expr_res`.Nicholas Nethercote-11/+19
This removes the final `Option<AttrWrapper>` argument.
2024-06-19Simplify `LhsExpr::Unparsed`.Nicholas Nethercote-10/+11
By making the `AttrWrapper` non-optional.
2024-06-19Move `parse_or_use_outer_attributes` out of `parse_expr_prefix_range`.Nicholas Nethercote-9/+10
This eliminates another `Option<AttrWrapper>` argument and changes one obscure error message.
2024-06-19Move `parse_or_use_outer_attributes` out of `parse_expr_prefix`.Nicholas Nethercote-5/+8
This eliminates one `Option<AttrWrapper>` argument.