Files
swift-mirror/test/SILOptimizer/switch_expr.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

24 lines
428 B
Swift

// RUN: %target-swift-emit-sil -verify %s -o /dev/null
func foo() -> Int {
switch Bool.random() {
case true:
0 // expected-warning {{integer literal is unused}}
case false:
do {}
}
} // expected-error {{missing return in global function expected to return 'Int'}}
enum E<T> {
case x(T), y
func foo() -> E<T> {
switch self {
case .x:
while true {}
case .y:
fatalError()
}
}
}