Files
swift-mirror/test/embedded/existential-class-bound4.swift
Kuba Mracek 6b9a3051e3 [embedded] Introduce class-bound existentials into Embedded Swift
Motivated by need for protocol-based dynamic dispatch, which hasn't been possible in Embedded Swift due to a full ban on existentials. This lifts that restriction but only for class-bound existentials: Class-bound existentials are already (even in desktop Swift) much more lightweight than full existentials, as they don't need type metadata, their containers are typically 2 words only (reference + wtable pointer), don't incur copies (only retains+releases).

Included in this PR:
[x] Non-generic class-bound existentials, executable tests for those.
[x] Extension methods on protocols and using those from a class-bound existential.
[x] RuntimeEffects now differentiate between Existential and ExistentialClassBound.
[x] PerformanceDiagnostics don't flag ExistentialClassBound in Embedded Swift.
[x] WTables are generated in IRGen when needed.

Left for follow-up PRs:
[ ] Generic classes support
2024-09-19 07:49:50 -07:00

45 lines
1.0 KiB
Swift

// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo) | %FileCheck %s
// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: OS=macosx || OS=linux-gnu
// Generic classes don't work yet.
// XFAIL: *
protocol ClassBound: AnyObject {
func foo()
func bar()
}
class MyGenericClass<T> {
var typ: String
init(typ: String) { self.typ = typ }
}
extension MyGenericClass: ClassBound {
func foo() { print("MyGenericClass<\(typ)>.foo()") }
func bar() { print("MyGenericClass<\(typ)>.bar()") }
}
@main
struct Main {
static func main() {
var array: [any ClassBound] = []
array.append(MyGenericClass<Int>(typ: "Int"))
array.append(MyGenericClass<String>(typ: "String"))
for e in array {
e.foo()
e.bar()
}
// CHECK: MyGenericClass<Int>.foo()
// CHECK: MyGenericClass<Int>.bar()
// CHECK: MyGenericClass<String>.foo()
// CHECK: MyGenericClass<String>.bar()
}
}