Files
swift-mirror/test/SILGen/moveonly_failable_init.swift
Doug Gregor bd696f9c05 Enable failable initializers for noncopyable types
Noncopyable types were prevented from having failable initializers
because `Optional` itself didn't support noncopyable types. Now
`Optional` does, so lift this restriction and add a test.
2024-04-01 17:29:41 -07:00

30 lines
1.0 KiB
Swift

// RUN: %target-swift-emit-sil -module-name test %s | %FileCheck %s --enable-var-scope
struct MoveWithDeinit: ~Copyable {
deinit { }
}
struct MyType: ~Copyable {
var handle: MoveWithDeinit
// CHECK-LABEL: sil hidden @$s4test6MyTypeV6handleACSgAA14MoveWithDeinitVSg_tcfC : $@convention(method) (@owned Optional<MoveWithDeinit>, @thin MyType.Type) -> @owned Optional<MyType>
// CHECK: bb0(%0 : $Optional<MoveWithDeinit>, %1 : $@thin MyType.Type):
init?(handle: consuming MoveWithDeinit?) {
// CHECK: switch_enum [[SUBJECT:%.*]] : $Optional<MoveWithDeinit>, case #Optional.some!enumelt: bb2, case #Optional.none!enumelt: bb1
guard let handle = consume handle else {
// CHECK: bb1:
// CHECK: [[NONE:%.*]] = enum $Optional<MyType>, #Optional.none!enumelt
// CHECK: br bb3([[NONE]] : $Optional<MyType>)
return nil
}
// CHECK: bb2([[WRAPPED:%.*]] : $MoveWithDeinit):
// CHECK: br bb3
self.handle = handle
}
}
func test() -> MyType? {
return MyType(handle: MoveWithDeinit())
}