Files
swift-mirror/test/Constraints/if_switch_expr_cgfloat_double_conversion.swift
Hamish Knight a40f1abaff Introduce if/switch expressions
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.
2023-02-01 15:30:18 +00:00

48 lines
922 B
Swift

// RUN: %target-typecheck-verify-swift %clang-importer-sdk
// REQUIRES: objc_interop
import Foundation
func testReturn1(_ d: Double) -> CGFloat {
if .random() { d } else { 0 }
}
func testReturn2(_ d: Double) -> CGFloat {
return if .random() { d } else { 0 }
}
func testReturn3(_ d: Double) -> CGFloat {
switch Bool.random() { case true: d case false: 0.0 }
}
func testClosure(_ d: CGFloat) {
func fn(_: () -> Double) {}
fn {
if .random() { d } else { 0.0 }
}
fn {
if .random() { 0 } else { d }
}
fn {
if .random() { d } else { d }
}
fn {
return if .random() { d } else { d }
}
fn {
switch Bool.random() {
case true:
d
case false:
0.0
}
}
}
func testAssignment(_ d: CGFloat) -> Double {
let d1: Double = if .random() { d } else { 0.0 }
let d2: Double = switch Bool.random() { case true: d case false: 0.0 }
return .random() ? d1 : d2
}