Skip to content

Commit b6b87d6

Browse files
committed
let-else, disallow any expr ending with }.
As per discussion in GitHub and Zulip, this seems like the most straightforward path until practical experimentation can be done.
1 parent 76e8bb5 commit b6b87d6

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

text/0000-let-else.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,13 @@ let (each, binding) = match expr {
209209
```
210210

211211
Most expressions may be put into the expression position with two restrictions:
212-
1. May not include a block outside of parenthesis.
213-
- Must be an [`ExpressionWithoutBlock`][expressions].
214-
- [`GroupedExpression`][grouped-expr]-s ending with a `}` are additionally not allowed and must be put in parenthesis.
212+
1. May not end with a `}` (before macro expansion). (Such things must be put in parenthesis.)
215213
2. May not be just a lazy boolean expression (`&&` or `||`). (Must not be a [`LazyBooleanExpression`][lazy-boolean-operators].)
216214

217215
While allowing e.g. `if {} else {}` directly in the expression position is technically feasible this RFC proposes it be
218216
disallowed for programmer clarity so as to avoid `... else {} else {}` situations as discussed in the [drawbacks][] section.
219217
Boolean matches are not useful with let-else and so lazy boolean expressions are disallowed for reasons noted in [future-possibilities][].
220-
These types of expressions can still be used when combined in a less ambiguous manner with parenthesis, thus forming a [`GroupedExpression`][grouped-expr],
218+
These types of expressions can still be used when combined in a less ambiguous manner with parenthesis,
221219
which is allowed under the two expression restrictions.
222220

223221
Any refutable pattern that could be put into if-let's pattern position can be put into let-else's pattern position.
@@ -278,7 +276,7 @@ because the compiler won't interpret it as `let PATTERN = (if y { a }) else { b
278276
This can be overcome by making a raw if-else in the expression position a compile error and instead requiring that parentheses are inserted to disambiguate:
279277
`let PATTERN = (if { a } else { b }) else { c };`.
280278

281-
Rust already provides us with such a restriction, and so the expression can be restricted to be a [`ExpressionWithoutBlock`][expressions].
279+
This restriction can be made by checking if the expression ends in `}` after parsing but _before_ macro expansion.
282280

283281
# Rationale and alternatives
284282
[rationale-and-alternatives]: #rationale-and-alternatives
@@ -304,7 +302,7 @@ let true = a && b else {
304302
};
305303
```
306304

307-
The expression can be any [`ExpressionWithoutBlock`][expressions], in order to prevent `else {} else {}` confusion, as noted in [drawbacks][#drawbacks].
305+
The expression must not end with a `}`, in order to prevent `else {} else {}` (and similar) confusion, as noted in [drawbacks][#drawbacks].
308306

309307
The `else` must be followed by a block, as in `if {} else {}`. This else block must be diverging as the outer
310308
context cannot be guaranteed to continue soundly without assignment, and no alternate assignment syntax is provided.
@@ -643,7 +641,6 @@ because it is confusing to read syntactically, and it is functionally similar to
643641
[`const`]: https://doc.rust-lang.org/reference/items/constant-items.html
644642
[`static`]: https://doc.rust-lang.org/reference/items/static-items.html
645643
[expressions]: https://doc.rust-lang.org/reference/expressions.html#expressions
646-
[grouped-expr]: https://doc.rust-lang.org/reference/expressions/grouped-expr.html
647644
[guard-crate]: https://crates.io/crates/guard
648645
[guard-repo]: https://github.com/durka/guard
649646
[if-let]: https://rust-lang.github.io/rfcs/0160-if-let.html

0 commit comments

Comments
 (0)