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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// RUN: %target-swift-emit-silgen -parse-as-library %s | %FileCheck %s
enum Boolish {
case falsy
case truthy
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum13Boolish_casesyyF
func Boolish_cases() {
// CHECK: [[BOOLISH:%[0-9]+]] = metatype $@thin Boolish.Type
// CHECK-NEXT: [[FALSY:%[0-9]+]] = enum $Boolish, #Boolish.falsy!enumelt
_ = Boolish.falsy
// CHECK-NEXT: [[BOOLISH:%[0-9]+]] = metatype $@thin Boolish.Type
// CHECK-NEXT: [[TRUTHY:%[0-9]+]] = enum $Boolish, #Boolish.truthy!enumelt
_ = Boolish.truthy
}
enum Optionable {
case nought
case mere(Int)
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum16Optionable_casesyySiF
func Optionable_cases(_ x: Int) {
// CHECK: [[METATYPE:%.*]] = metatype $@thin Optionable.Type
// CHECK: [[FN:%.*]] = function_ref @$s4enum16Optionable_casesyySiFAA0B0OSicADmcfu_
_ = Optionable.mere
// CHECK: [[METATYPE:%.*]] = metatype $@thin Optionable.Type
// CHECK: [[RES:%.*]] = enum $Optionable, #Optionable.mere!enumelt, %0 : $Int
_ = Optionable.mere(x)
}
// CHECK-LABEL: sil private [ossa] @$s4enum16Optionable_casesyySiFAA0B0OSicADmcfu_ : $@convention(thin) (@thin Optionable.Type) -> @owned @callee_guaranteed (Int) -> Optionable
// CHECK-LABEL: sil private [ossa] @$s4enum16Optionable_casesyySiFAA0B0OSicADmcfu_ADSicfu0_ : $@convention(thin) (Int, @thin Optionable.Type) -> Optionable {
protocol P {}
struct S : P {}
enum AddressOnly {
case nought
case mere(P)
case phantom(S)
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum17AddressOnly_casesyyAA1SVF
func AddressOnly_cases(_ s: S) {
// CHECK: [[METATYPE:%.*]] = metatype $@thin AddressOnly.Type
// CHECK: [[FN:%.*]] = function_ref @$s4enum17AddressOnly_casesyyAA1SVFAA0bC0OAA1P_pcAFmcfu_
// CHECK-NEXT: [[CTOR:%.*]] = apply [[FN]]([[METATYPE]])
// CHECK-NEXT: destroy_value [[CTOR]]
_ = AddressOnly.mere
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin AddressOnly.Type
// CHECK-NEXT: [[NOUGHT:%.*]] = alloc_stack $AddressOnly
// CHECK-NEXT: inject_enum_addr [[NOUGHT]]
// CHECK-NEXT: destroy_addr [[NOUGHT]]
// CHECK-NEXT: dealloc_stack [[NOUGHT]]
_ = AddressOnly.nought
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin AddressOnly.Type
// CHECK-NEXT: [[MERE:%.*]] = alloc_stack $AddressOnly
// CHECK-NEXT: [[PAYLOAD:%.*]] = init_enum_data_addr [[MERE]]
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = init_existential_addr [[PAYLOAD]]
// CHECK-NEXT: store %0 to [trivial] [[PAYLOAD_ADDR]]
// CHECK-NEXT: inject_enum_addr [[MERE]]
// CHECK-NEXT: destroy_addr [[MERE]]
// CHECK-NEXT: dealloc_stack [[MERE]]
_ = AddressOnly.mere(s)
// Address-only enum vs loadable payload
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin AddressOnly.Type
// CHECK-NEXT: [[PHANTOM:%.*]] = alloc_stack $AddressOnly
// CHECK-NEXT: [[PAYLOAD:%.*]] = init_enum_data_addr [[PHANTOM]] : $*AddressOnly, #AddressOnly.phantom!enumelt
// CHECK-NEXT: store %0 to [trivial] [[PAYLOAD]]
// CHECK-NEXT: inject_enum_addr [[PHANTOM]] : $*AddressOnly, #AddressOnly.phantom!enumelt
// CHECK-NEXT: destroy_addr [[PHANTOM]]
// CHECK-NEXT: dealloc_stack [[PHANTOM]]
_ = AddressOnly.phantom(s)
// CHECK: return
}
enum PolyOptionable<T> {
case nought
case mere(T)
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum20PolyOptionable_casesyyxlF
func PolyOptionable_cases<T>(_ t: T) {
// CHECK: [[METATYPE:%.*]] = metatype $@thin PolyOptionable<T>.Type
// CHECK-NEXT: [[NOUGHT:%.*]] = alloc_stack $PolyOptionable<T>
// CHECK-NEXT: inject_enum_addr [[NOUGHT]]
// CHECK-NEXT: destroy_addr [[NOUGHT]]
// CHECK-NEXT: dealloc_stack [[NOUGHT]]
_ = PolyOptionable<T>.nought
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin PolyOptionable<T>.Type
// CHECK-NEXT: [[MERE:%.*]] = alloc_stack $PolyOptionable<T>
// CHECK-NEXT: [[PAYLOAD:%.*]] = init_enum_data_addr [[MERE]]
// CHECK-NEXT: copy_addr %0 to [init] [[PAYLOAD]] : $*T
// CHECK-NEXT: inject_enum_addr [[MERE]]
// CHECK-NEXT: destroy_addr [[MERE]]
// CHECK-NEXT: dealloc_stack [[MERE]]
_ = PolyOptionable<T>.mere(t)
// CHECK-NOT: destroy_addr %0
// CHECK: return
}
// The substituted type is loadable and trivial here
// CHECK-LABEL: sil hidden [ossa] @$s4enum32PolyOptionable_specialized_casesyySiF
func PolyOptionable_specialized_cases(_ t: Int) {
// CHECK: [[METATYPE:%.*]] = metatype $@thin PolyOptionable<Int>.Type
// CHECK-NEXT: [[NOUGHT:%.*]] = enum $PolyOptionable<Int>, #PolyOptionable.nought!enumelt
_ = PolyOptionable<Int>.nought
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin PolyOptionable<Int>.Type
// CHECK-NEXT: [[NOUGHT:%.*]] = enum $PolyOptionable<Int>, #PolyOptionable.mere!enumelt, %0
_ = PolyOptionable<Int>.mere(t)
// CHECK: return
}
// Regression test for a bug where temporary allocations created as a result of
// tuple implosion were not deallocated in enum constructors.
struct String { var ptr: AnyObject }
enum Foo { case a(P, String) }
func Foo_cases() {
_ = Foo.a
}
enum Indirect<T> {
indirect case payload((T, other: T))
case none
}
// CHECK-LABEL: sil{{.*}} @{{.*}}makeIndirectEnum{{.*}} : $@convention(thin) <T> (@in_guaranteed T) -> @owned Indirect<T>
// CHECK: [[BOX:%.*]] = alloc_box $<τ_0_0> { var (τ_0_0, other: τ_0_0) } <T>
// CHECK: enum $Indirect<T>, #Indirect.payload!enumelt, [[BOX]] : $<τ_0_0> { var (τ_0_0, other: τ_0_0) } <T>
func makeIndirectEnum<T>(_ payload: T) -> Indirect<T> {
return Indirect.payload((payload, other: payload))
}
// https://github.com/apple/swift/issues/52118
enum TrailingClosureConcrete {
case label(fn: () -> Int)
case noLabel(() -> Int)
case twoElementsLabel(x: Int, fn: () -> Int)
case twoElementsNoLabel(_ x: Int, _ fn: () -> Int)
}
func useTrailingClosureConcrete() {
_ = TrailingClosureConcrete.label { 0 }
_ = TrailingClosureConcrete.noLabel { 0 }
_ = TrailingClosureConcrete.twoElementsLabel(x: 0) { 0 }
_ = TrailingClosureConcrete.twoElementsNoLabel(0) { 0 }
}
enum TrailingClosureGeneric<T> {
case label(fn: () -> T)
case noLabel(() -> T)
case twoElementsLabel(x: T, fn: () -> T)
case twoElementsNoLabel(_ x: T, _ fn: () -> T)
}
func useTrailingClosureGeneric<T>(t: T) {
_ = TrailingClosureGeneric<T>.label { t }
_ = TrailingClosureGeneric<T>.noLabel { t }
_ = TrailingClosureGeneric<T>.twoElementsLabel(x: t) { t }
_ = TrailingClosureGeneric<T>.twoElementsNoLabel(t) { t }
}
// https://github.com/apple/swift/issues/50338
enum OneOrTwo {
case one
case two
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum18matchOptionalEnum13baryAA8OneOrTwoOSg_tF : $@convention(thin) (Optional<OneOrTwo>) -> () {
// CHECK: bb0(%0 : $Optional<OneOrTwo>):
// CHECK-NEXT: debug_value %0 : $Optional<OneOrTwo>, let, name "bar", argno 1
// CHECK-NEXT: switch_enum %0 : $Optional<OneOrTwo>, case #Optional.some!enumelt: bb1, default bb4
// CHECK: bb1([[PHI_ARG:%.*]] : $OneOrTwo):
// CHECK-NEXT: switch_enum [[PHI_ARG]] : $OneOrTwo, case #OneOrTwo.one!enumelt: bb2, case #OneOrTwo.two!enumelt: bb3
func matchOptionalEnum1(bar: OneOrTwo?) {
switch bar {
case .one: print("one")
case .two?: print("two")
default: print("default")
}
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum18matchOptionalEnum23baryAA8OneOrTwoOSgSg_tF : $@convention(thin) (Optional<Optional<OneOrTwo>>) -> () {
// CHECK: bb0(%0 : $Optional<Optional<OneOrTwo>>):
// CHECK-NEXT: debug_value %0 : $Optional<Optional<OneOrTwo>>, let, name "bar", argno 1
// CHECK-NEXT: switch_enum %0 : $Optional<Optional<OneOrTwo>>, case #Optional.none!enumelt: bb1, default bb2
func matchOptionalEnum2(bar: OneOrTwo??) {
switch bar {
case .none: print("none")
default: print("default")
}
}
enum OneTwoOrNone {
case one
case two
var none: Self {
.one
}
}
// CHECK-LABEL: sil hidden [ossa] @$s4enum18matchOptionalEnum33baryAA12OneTwoOrNoneOSg_tF : $@convention(thin) (Optional<OneTwoOrNone>) -> () {
// CHECK: bb0(%0 : $Optional<OneTwoOrNone>):
// CHECK-NEXT: debug_value %0 : $Optional<OneTwoOrNone>, let, name "bar", argno 1
// CHECK-NEXT: switch_enum %0 : $Optional<OneTwoOrNone>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb4
// CHECK: bb1([[PHI_ARG:%.*]] : $OneTwoOrNone):
// CHECK-NEXT: switch_enum [[PHI_ARG]] : $OneTwoOrNone, case #OneTwoOrNone.one!enumelt: bb2, case #OneTwoOrNone.two!enumelt: bb3
func matchOptionalEnum3(bar: OneTwoOrNone?) {
switch bar {
case .one: print("one")
case .two?: print("two")
case .none: print("none")
default: print("default")
}
}
// Make sure that we handle enum, tuple initialization composed
// correctly. Previously, we leaked down a failure path due to us misusing
// scopes.
enum rdar81817725 {
case localAddress
case setOption(Int, Any)
static func takeAny(_:Any) -> Bool { return true }
static func testSwitchCleanup(syscall: rdar81817725, expectedLevel: Int,
valueMatcher: (Any) -> Bool)
throws -> Bool {
if case .setOption(expectedLevel, let value) = syscall {
return rdar81817725.takeAny(value)
} else {
return false
}
}
}
enum Indirected {
case a
case b
indirect case c(Int)
}
func throwingFunction() throws -> Int { return 0 }
// CHECK-LABEL: sil hidden [ossa] @$s4enum29throwInIndirectConstructorArgAA10IndirectedOyKF
// CHECK: [[BOX:%.*]] = alloc_box ${ var Int }
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = project_box [[BOX]]
// CHECK-NEXT: // function_ref
// CHECK-NEXT: [[FN:%.*]] = function_ref
// CHECK-NEXT: try_apply [[FN]]()
// CHECK: bb1([[RESULT:%.*]] : $Int):
// CHECK-NEXT: store [[RESULT]] to [trivial] [[PAYLOAD_ADDR]]
// CHECK-NEXT: [[ENUM:%.*]] = enum $Indirected, #Indirected.c!enumelt, [[BOX]]
// CHECK-NEXT: return [[ENUM]] : $Indirected
// CHECK: bb2([[ERROR:%.*]] : @owned $any Error):
// CHECK-NEXT: dealloc_box [[BOX]]
// CHECK-NEXT: throw [[ERROR]]
func throwInIndirectConstructorArg() throws -> Indirected {
return .c(try throwingFunction())
}
|