Files
swift-mirror/test/embedded/existential-default-method.swift
T
Doug Gregor ae1e3d0865 Build a static library that implements the Embedded Swift platform layer on POSIX
The new library, swiftEmbeddedPlatformPOSIX, implements all of the
_swift_XYZ functions needed to support Embedded Swift as shims on top
of a POSIX system that provides posix_memalign, free, putchar, and so
on. This offers an easier way to bridge between the prior ad hoc
requirements of Embedded Swift and the newer platform abstraction
layer.

Part of rdar://164057124
2026-03-03 09:05:30 -08:00

102 lines
2.0 KiB
Swift

// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo %target-embedded-posix-shim) | %FileCheck %s
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -O %target-embedded-posix-shim) | %FileCheck %s
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -Osize %target-embedded-posix-shim) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: swift_feature_Embedded
// Simple case
public protocol ProtocolWithDefaultMethod: AnyObject {
func getInt() -> Int
}
extension ProtocolWithDefaultMethod {
public func getInt() -> Int {
return 42
}
}
public class Class: ProtocolWithDefaultMethod {
}
public class GenClass<T>: ProtocolWithDefaultMethod {
}
func test(existential: any ProtocolWithDefaultMethod) {
print(existential.getInt())
}
// Test that we specialize for the correct derived class
class C {
class func g() -> Int {
return 1
}
}
class D: C {
override class func g() -> Int {
return 2
}
}
protocol P: AnyObject {
static func g() -> Int
func test() -> Int
}
extension P {
func test() -> Int {
Self.g()
}
}
extension C: P {}
func createDerived() -> P {
return D()
}
// Test that we don't end up in an infinite recursion loop
public protocol RecursiveProto: AnyObject {
associatedtype T: RecursiveProto
func getInt() -> Int
func getT() -> T
}
extension RecursiveProto {
public func getInt() -> Int {
return 27
}
}
public class RecursiveClass: RecursiveProto {
public typealias T = RecursiveClass
public func getT() -> RecursiveClass {
return self
}
}
func testRecursive(existential: any RecursiveProto) {
print(existential.getT().getInt())
}
@main
struct Main {
static func main() {
// CHECK: 42
test(existential: Class())
// CHECK: 42
test(existential: GenClass<Int>())
// CHECK: 2
print(createDerived().test())
// CHECK: 27
testRecursive(existential: RecursiveClass())
}
}