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.
36 lines
607 B
Swift
36 lines
607 B
Swift
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15
|
|
// REQUIRES: OS=macosx
|
|
|
|
import SwiftUI
|
|
|
|
struct A {
|
|
func bar() -> Bool { true }
|
|
}
|
|
|
|
struct B: View {
|
|
var body: some View { fatalError() }
|
|
}
|
|
|
|
struct C<T: View>: View {
|
|
init(@ViewBuilder _: () -> T, _: () -> Void) {}
|
|
var body: some View { fatalError() }
|
|
}
|
|
|
|
func foo<C: Collection, R>(_: C, @ViewBuilder _: (C.Element) -> R) -> R {
|
|
fatalError()
|
|
}
|
|
|
|
let arr: [A] = []
|
|
|
|
@ViewBuilder
|
|
func blah() -> some View {
|
|
foo(arr) { a in
|
|
let b = a.bar()
|
|
C {
|
|
if b { B() }
|
|
} _: {
|
|
if b { () } else { () }
|
|
}
|
|
}
|
|
}
|