Swift: add extraction of ThenStmt

These are currently added implicitly by the compiler in the context of
`if`/`switch` expressions. In the future, there might be explicit
`then <expr>` statement useful for cases where one would like to add
more than one statement in the branch, to mark what value to actually
use.

See https://forums.swift.org/t/pitch-multi-statement-if-switch-do-expressions/68443
This commit is contained in:
Paolo Tranquilli
2024-03-27 12:14:19 +01:00
parent 2382f76317
commit b8e38288e3
20 changed files with 213 additions and 9 deletions

View File

@@ -994,6 +994,20 @@ class ThrowStmt(Stmt):
class YieldStmt(Stmt):
results: list[Expr] | child
@qltest.test_with('SingleValueStmtExpr')
class ThenStmt(Stmt):
""" A statement implicitly wrapping values to be used in branches of if/switch expressions. For example in:
```
let rank = switch value {
case 0..<0x80: 1
case 0x80..<0x0800: 2
default: 3
}
```
the literal expressions `1`, `2` and `3` are wrapped in `ThenStmt`.
"""
result: Expr
class DoCatchStmt(LabeledStmt):
body: Stmt | child
catches: list[CaseStmt] | child