1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver -Xfrontend -enable-experimental-feature -Xfrontend ImplicitLastExprResults -o %t/a.out %s
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
// Required for experimental features
// REQUIRES: asserts
func testBinding(_ cond: Bool) -> Int {
let x = if cond {
let a = 1
a
} else {
let b = 1
if cond { b } else { b + 1 }
}
return x
}
print(testBinding(true))
print(testBinding(false))
// CHECK: 1
// CHECK-NEXT: 2
func testReturn(_ cond: Bool) -> Int {
()
if cond {
let a = 1
a
} else {
let b = 1
if cond { b } else { b + 1 }
}
}
print(testReturn(true))
print(testReturn(false))
// CHECK-NEXT: 1
// CHECK-NEXT: 2
func testDefer(_ cond: Bool) -> Int {
defer {
print("defer fn")
}
let x = if cond {
defer {
print("defer if")
}
print("enter if")
1
} else {
print("else branch")
2
}
print("after if")
return x
}
_ = testDefer(true)
_ = testDefer(false)
// CHECK: enter if
// CHECK-NEXT: defer if
// CHECK-NEXT: after if
// CHECK-NEXT: defer fn
// CHECK-NEXT: else branch
// CHECK-NEXT: after if
// CHECK-NEXT: defer fn
func testGuard(_ cond: Bool) -> Int {
switch cond {
case true:
let x: Int? = 1
guard let y = x else { fatalError() }
y
case false:
2
}
}
print(testGuard(true))
// CHECK-NEXT: 1
func testNestedType(_ cond: Bool) -> Int {
if cond {
struct S {
var x: Int
}
S(x: 1).x
} else {
2
}
}
print(testNestedType(true))
// CHECK-NEXT: 1
func testClosure(_ cond: Bool) -> Int {
let fn = {
()
if cond {
let a = 1
a
} else {
let b = 1
if cond { b } else { b + 1 }
}
}
return fn()
}
print(testClosure(true))
print(testClosure(false))
// CHECK-NEXT: 1
// CHECK-NEXT: 2
|