mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +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.
22 lines
1.3 KiB
Swift
22 lines
1.3 KiB
Swift
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_if_expr %s | %FileCheck %s
|
|
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -emit-ir %s
|
|
|
|
// CHECK-LABEL: sil_coverage_map {{.*}} "$s16coverage_if_expr0b1_C0SiyF"
|
|
func if_expr() -> Int { // CHECK: [[@LINE]]:23 -> {{[0-9]+}}:2 : 0
|
|
if .random() { // CHECK-NEXT: [[@LINE]]:6 -> [[@LINE]]:15 : 0
|
|
0 // CHECK-NEXT: [[@LINE-1]]:16 -> [[@LINE+4]]:4 : 1
|
|
// CHECK-NEXT: [[@LINE+3]]:4 -> {{[0-9]+}}:2 : 0
|
|
|
|
// FIXME: The below line is incorrect, it should be (0 - 1), but that's an existing bug with else ifs.
|
|
} else if .random() { // CHECK-NEXT: [[@LINE]]:13 -> [[@LINE]]:22 : 0
|
|
1 // CHECK-NEXT: [[@LINE-1]]:23 -> [[@LINE+1]]:4 : 2
|
|
} else { // CHECK-NEXT: [[@LINE]]:4 -> {{[0-9]+}}:2 : 0
|
|
|
|
// FIXME: The below line is incorrect, it should be ((0 - 1) - 2), but that's an existing bug with else ifs.
|
|
2 // CHECK-NEXT: [[@LINE-3]]:10 -> [[@LINE+4]]:4 : (0 - 2)
|
|
|
|
// FIXME: Also incorrect
|
|
// CHECK-NEXT: [[@LINE+1]]:4 -> [[@LINE+2]]:2 : 0
|
|
} // CHECK-NEXT: }
|
|
}
|