Files
swift-mirror/test/embedded/existential-class-bound5.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

36 lines
977 B
Swift

// RUN: %target-swift-emit-ir -parse-as-library -module-name main -verify %s -enable-experimental-feature Embedded -wmo
// REQUIRES: swift_in_compiler
// REQUIRES: optimized_stdlib
// REQUIRES: OS=macosx || OS=linux-gnu
protocol ClassBound: AnyObject {
func foo()
}
protocol NotClassBound {
func foo()
}
class MyClass {}
extension MyClass: ClassBound, NotClassBound {
func foo() { print("MyClass.foo()") }
}
func test(existential: any ClassBound) {
existential.foo() // ok
}
func test(existential: any NotClassBound) {
existential.foo() // expected-error {{cannot use a value of protocol type in embedded Swift}}
// expected-note@+3 {{called from here}}
}
@main
struct Main {
static func main() {
test(existential: MyClass() as (any ClassBound)) // ok
test(existential: MyClass() as (any NotClassBound)) // expected-error {{cannot use a value of protocol type 'any NotClassBound' in embedded Swift}}
}
}