Skip to content

Commit 602430a

Browse files
committed
Update let chain grammar for minimum precedence
This adds some more exclusions to the let chain grammar to capture the minimum precedence while parsing chains. Note that this is incomplete, see #1811. I didn't want to block things on finalizing this. I also reworked the `while` grammar to just reuse the grammar from `if` to avoid the duplication.
1 parent 7f7e7da commit 602430a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/expressions/if-expr.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ r[expr.if]
44
r[expr.if.syntax]
55
```grammar,expressions
66
IfExpression ->
7-
`if` IfConditions BlockExpression
7+
`if` Conditions BlockExpression
88
(`else` ( BlockExpression | IfExpression ) )?
99
10-
IfConditions -> IfCondition ( `&&` IfCondition )*
11-
12-
IfCondition ->
10+
Conditions ->
1311
Expression _except [StructExpression]_
14-
| `let` Pattern `=` Scrutinee _except [LazyBooleanExpression]_
12+
| LetChain
13+
14+
LetChain -> LetChainCondition ( `&&` LetChainCondition )*
15+
16+
LetChainCondition ->
17+
Expression _except [ExcludedConditions]_
18+
| OuterAttribute* `let` Pattern `=` Scrutinee _except [ExcludedConditions]_
19+
20+
@root ExcludedConditions ->
21+
StructExpression
22+
| LazyBooleanExpression
23+
| RangeExpr
24+
| RangeFromExpr
25+
| RangeInclusiveExpr
26+
| AssignmentExpression
27+
| CompoundAssignmentExpression
1528
```
16-
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
29+
<!-- TODO: The struct exception above needs clarification, see https://github.com/rust-lang/reference/issues/1808
30+
The chain grammar could use some work, see https://github.com/rust-lang/reference/issues/1811
31+
-->
1732

1833
r[expr.if.intro]
1934
The syntax of an `if` expression is a sequence of one or more condition operands separated by `&&`,
@@ -161,7 +176,7 @@ if let Some(x) = foo && (condition1 || condition2) { /*...*/ }
161176

162177
r[expr.if.edition2024]
163178
> [!EDITION-2024]
164-
> Before the 2024 edition, let chains are not supported and only a single _IfCondition_ is allowed in an `if` expression.
179+
> Before the 2024 edition, let chains are not supported. That is, the [LetChain] grammar is not allowed in an `if` expression.
165180
166181
[_BlockExpression_]: block-expr.md
167182
[_Expression_]: ../expressions.md

src/expressions/loop-expr.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,8 @@ r[expr.loop.while]
5252

5353
r[expr.loop.while.grammar]
5454
```grammar,expressions
55-
PredicateLoopExpression -> `while` WhileConditions BlockExpression
56-
57-
WhileConditions -> WhileCondition ( `&&` WhileCondition )*
58-
59-
WhileCondition ->
60-
Expression _except [StructExpression]_
61-
| `let` Pattern `=` Scrutinee _except [LazyBooleanExpression]_
55+
PredicateLoopExpression -> `while` Conditions BlockExpression
6256
```
63-
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
6457

6558
r[expr.loop.while.intro]
6659
A `while` loop expression allows repeating the evaluation of a block while a set of conditions remain true.

0 commit comments

Comments
 (0)