Repository navigation
Parser: Refactor & better document should_continue_as_assoc_expr & can_continue_expr_unambiguously - #163665
Conversation
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use Why was this reviewer chosen?The reviewer was selected based on:
|
81705e0 to
e75eedf
Compare
|
|
||
| impl<'a> Parser<'a> { | ||
| /// Recover from a binary operator after complete statement expression as in `{ 4 } / 2`. | ||
| pub(super) fn recover_from_bin_op_after_complete_stmt_expr(&self, lhs: &Expr) -> bool { |
There was a problem hiding this comment.
Instead of a non-descriptive bool I could return a non-descriptive ControlFlow<()> since in this case the return value controls whether to continue parsing as an expression or "break" (i.e., yield to the caller / parent subparser)
0d636ba to
c562a8a
Compare
| // based on the assumption that double-refs are rarely intentional closures are distinct | ||
| // enough that they don't get mixed up with their return value. |
There was a problem hiding this comment.
| // based on the assumption that double-refs are rarely intentional closures are distinct | |
| // enough that they don't get mixed up with their return value. | |
| // based on the assumption that double-refs are rarely intentional, and closures are distinct | |
| // enough that they don't get mixed up with their return value. |
c562a8a to
49e1df1
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@bors r=fee1-dead force |
…`can_continue_expr_unambiguously`
* `can_continue_expr_unambiguously`
* exhaustively match on `AssocOp`
* don't needlessly provide code snippets in comments for ops that are
unproblematic, that's not interesting
* instead, provide an example / explainer for each op that is ambiguous
which is far more relevant
* move it into `rustc_parse` since it's only used there & it's only
tailored towards parse error recovery
* `should_continue_as_assoc_expr`
* it used to match on a `(bool, _)` tuple & execute `AssocOp::from_token`
unconditionally even though all but one branch cared about the bool
being false
* instead, first check `expr_is_complete` and bail out early if it's
false; no need to execute `AssocOp::from_token` unnecessarily or
"bother" the rest of the code with the impossibility of
`expr_is_complete` not holding
* extract the recovery logic into a new
`recover_from_bin_op_after_complete_stmt_expr` that resides in
`expr/diagnostics.rs` to clearly separate what is recovery and what
is actual parsing
* in it check `can_continue_expr_unambiguously` *first* before
special-casing some ops (the `Mul | Sub | …` branch) because it's
more "important"; in any case it's a superset; renders the control
flow & the logic a lot more comprehensible
* rewrite the comments from scratch to make it clear that all of that
"bin op after complete stmt expr" is recovery code only! when I
first came across this function one year ago I was super confused &
briefly worried that typeck was potentially making belated parsing
decisions
* inline it because it's become a single expression
…uwer Rollup of 9 pull requests Successful merges: - #161491 (Rip out old solver coherence) - #163533 (Run cg_gcc tests with the correct compiler) - #163223 (regression test for async handler normalization ICE) - #163367 (simplify rustc_log a bit) - #163622 (x86: c-variadic functions don't use registers with `-Zregparm`) - #163653 (callconv: mips64: Match GCC for alignment of 16-byte scalars) - #163665 (Parser: Refactor & better document `should_continue_as_assoc_expr` & `can_continue_expr_unambiguously`) - #163742 (Add the `movdir64b` and `movdiri` x86 target features) - #163752 (Revert "implement PartialEq<VecDeque<U>> for Vec<T>, &[T], &mut [T], [T; N], &[T; N] and &mut [T; N]")
Rollup merge of #163665 - fmease:refactor-should-cont-as-assoc-expr, r=fee1-dead Parser: Refactor & better document `should_continue_as_assoc_expr` & `can_continue_expr_unambiguously` Whenever I stumbled across `should_continue_as_assoc_expr` I had to very slowly reread its impl because the contained `match` was written in an incredibly convoluted fashion rendering it illegible. Moreover, I always had a hard time figuring out what part of the code was recovery & what actual parsing, esp. since it does indeed save extra diagnostic info in the happy path (proactively). See commit message for details. <sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
Whenever I stumbled across
should_continue_as_assoc_exprI had to very slowly reread its impl because the containedmatchwas written in an incredibly convoluted fashion rendering it illegible. Moreover, I always had a hard time figuring out what part of the code was recovery & what actual parsing, esp. since it does indeed save extra diagnostic info in the happy path (proactively).See commit message for details.
(No LLM was or will be used by me during the entire creation process of this PR)