Files
swift-mirror/test/SILOptimizer/moveonly_consuming_switch.swift
Kavon Farvardin 04454054b7 NCGenerics: add availability checking
Not all runtimes can correctly operate with types that use noncopyable
generics. When the generic argument of a type is noncopyable, old
runtimes can't recognize that to correctly check conformances that may
be conditional on those arguments being Copyable, etc.

resolves rdar://126239335
2024-07-07 15:38:00 -07:00

56 lines
1.3 KiB
Swift

// RUN: %target-swift-frontend -emit-sil -verify %s -disable-availability-checking
struct Box<Wrapped: ~Copyable>: ~Copyable {
private let _pointer: UnsafeMutablePointer<Wrapped>
init(_ element: consuming Wrapped) {
_pointer = .allocate(capacity: 1)
print("allocating",_pointer)
_pointer.initialize(to: element)
}
deinit {
print("deallocating",_pointer)
_pointer.deinitialize(count: 1)
_pointer.deallocate()
}
consuming func move() -> Wrapped {
let wrapped = _pointer.move()
print("deallocating", _pointer)
_pointer.deallocate()
discard self
return wrapped
}
}
enum List<Element: ~Copyable>: ~Copyable {
case empty
case node(Element, Box<List>)
}
extension List {
init() { self = .empty }
mutating func push(_ element: consuming Element) {
self = .node(element, Box(self))
}
mutating func pop() -> Element {
switch consume self {
case .node(let element, let box):
self = box.move()
return element
case .empty:
fatalError()
}
}
var isEmpty: Bool {
switch self {
case .empty: true
case .node: false
}
}
}