mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Introduce SingleValueStmtExpr, which allows the embedding of a statement in an expression context. This then allows us to parse and type-check `if` and `switch` statements as expressions, gated behind the `IfSwitchExpression` experimental feature for now. In the future, SingleValueStmtExpr could also be used for e.g `do` expressions. For now, only single expression branches are supported for producing a value from an `if`/`switch` expression, and each branch is type-checked independently. A multi-statement branch may only appear if it ends with a `throw`, and it may not `break`, `continue`, or `return`. The placement of `if`/`switch` expressions is also currently limited by a syntactic use diagnostic. Currently they're only allowed in bindings, assignments, throws, and returns. But this could be lifted in the future if desired.
15 lines
1020 B
Swift
15 lines
1020 B
Swift
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_switch_expr %s | %FileCheck %s
|
|
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -emit-ir %s
|
|
|
|
// CHECK-LABEL: sil_coverage_map {{.*}} "$s20coverage_switch_expr0b1_C0ySiSbSgF"
|
|
func switch_expr(_ b: Bool?) -> Int { // CHECK-NEXT: [[@LINE]]:37 -> {{[0-9]+}}:2 : 0
|
|
switch b { // CHECK-NEXT: [[@LINE]]:10 -> [[@LINE]]:11 : 0
|
|
case true?: // CHECK-NEXT: [[@LINE]]:3 -> [[@LINE+1]]:6 : 1
|
|
0
|
|
case false?: // CHECK-NEXT: [[@LINE]]:3 -> [[@LINE+1]]:6 : 2
|
|
1
|
|
case nil: // CHECK-NEXT: [[@LINE]]:3 -> [[@LINE+1]]:6 : 3
|
|
2 // FIXME: The following is incorrect
|
|
} // CHECK-NEXT: [[@LINE]]:4 -> [[@LINE+1]]:2 : ((1 + 2) + 3)
|
|
} // CHECK-NEXT: }
|