Skip to content

Commit b380bf9

Browse files
committed
WIP: Adjust grammar and make related changes for let chains
1 parent ef9e1f7 commit b380bf9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/expressions/if-expr.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ r[expr.if]
44
r[expr.if.syntax]
55
```grammar,expressions
66
IfExpression ->
7-
`if` IfConditions BlockExpression
7+
`if` Condition BlockExpression
88
(`else` ( BlockExpression | IfExpression ) )?
99
10-
IfConditions -> IfCondition ( `&&` IfCondition )*
10+
Condition -> Scrutinee | LetChain
1111
12-
IfCondition ->
13-
Expression _except [StructExprStruct]_
14-
| `let` Pattern `=` Scrutinee _except [LazyBooleanExpression]_
12+
LetChain -> ( LetChainScrutinee | LetExpr ) ( `&&` LetChain )?
13+
14+
LetExpr -> `let` Pattern `=` LetChainScrutinee
15+
16+
LetChainScrutinee -> Scrutinee _except [BinOpsBelowAnd]_
17+
18+
@root BinOpsBelowAnd ->
19+
LazyBooleanExpression
20+
| RangeExpression
21+
| AssignmentExpression
22+
| CompoundAssignmentExpression
1523
```
1624
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
1725

1826
r[expr.if.intro]
19-
The syntax of an `if` expression is a sequence of one or more condition operands separated by `&&`,
20-
followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block.
27+
An `if` expression is a conditional branch in program control. The syntax of an `if` expression is a condition followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block. The condition may be an expression or a let chain. A let chain is a sequence of operands separated by `&&`.
2128

2229
r[expr.if.condition]
2330
Condition operands must be either an [_Expression_] with a [boolean type] or a conditional `let` match.
@@ -110,7 +117,7 @@ r[expr.if.chains.intro]
110117
Multiple condition operands can be separated with `&&`.
111118

112119
r[expr.if.chains.order]
113-
Similar to a `&&` [_LazyBooleanOperatorExpression_], each operand is evaluated from left-to-right until an operand evaluates as `false` or a `let` match fails,
120+
Similar to an `&&` [_LazyBooleanOperatorExpression_], each operand is evaluated from left-to-right until an operand evaluates as `false` or a `let` match fails,
114121
in which case the subsequent operands are not evaluated.
115122

116123
r[expr.if.chains.bindings]
@@ -148,7 +155,7 @@ fn nested() {
148155
```
149156

150157
r[expr.if.chains.or]
151-
If any condition operand is a `let` pattern, then none of the condition operands can be a `||` [lazy boolean operator expression][_LazyBooleanOperatorExpression_] due to ambiguity and precedence with the `let` scrutinee.
158+
If any condition operand is a `let` expression, then none of the condition operands can be an `||` [_LazyBooleanOperatorExpression_] or any other binary [operators] with a lower [precedence] than `&&` due to ambiguity and precedence with the `let` scrutinee.
152159
If a `||` expression is needed, then parentheses can be used. For example:
153160

154161
```rust
@@ -160,7 +167,8 @@ if let Some(x) = foo && (condition1 || condition2) { /*...*/ }
160167
```
161168

162169
r[expr.if.edition2024]
163-
> **Edition differences**: Before the 2024 edition, let chains are not supported and only a single _IfCondition_ is allowed in an `if` expression.
170+
> [!EDITION-2024]
171+
> Before the 2024 edition, let chains are not supported and only a single [_Scrutinee_][grammar-Scrutinee] is allowed as the conditional expression in an `if` expression.
164172
165173
[_BlockExpression_]: block-expr.md
166174
[_Expression_]: ../expressions.md
@@ -169,4 +177,6 @@ r[expr.if.edition2024]
169177
[_Scrutinee_]: match-expr.md
170178
[`match` expressions]: match-expr.md
171179
[boolean type]: ../types/boolean.md
180+
[operators]: expr.operator
181+
[precedence]: expr.precedence
172182
[scrutinee]: ../glossary.md#scrutinee

0 commit comments

Comments
 (0)