Files
swift-mirror/test/Interop/Cxx/foreign-reference/super-virtual-dispatch-silgen.swift
Egor Zhdan 338c8eafd8 [cxx-interop] Support static dispatch for virtual methods using super
This adds support for calling virtual methods of C++ foreign reference types with static dispatch.

In C++, one can do:
```cpp
this->Base::virtualMethod()
```

Swift doesn't allow calling a particular overload of an overridden method, however, it allows calling an overload from the superclass with `super`:
```swift
extension Derived {
  public func callSuper() {
    return super.foo()
  }
}
```

For inherited FRTs, the example above behaved in an unexpected way: the call would still be dispatched dynamically, despite using the syntax reserved for static dispatch. This change makes it behave similarly to pure-Swift classes.

rdar://177619427
2026-06-07 12:14:31 +01:00

40 lines
1.2 KiB
Swift

// RUN: %target-swift-emit-silgen %s -I %S/Inputs -cxx-interoperability-mode=default -enable-experimental-feature ForeignReferenceTypeInheritance -disable-availability-checking | %FileCheck %s
// REQUIRES: swift_feature_ForeignReferenceTypeInheritance
import SuperVirtualDispatch
extension Derived {
public func callSuperVirtual() -> Int32 {
return super.virtualMethod()
}
}
extension LeafOverNoOverride {
public func callSuperVirtualFromLeafOverNoOverride() -> Int32 {
return super.virtualMethod()
}
}
extension DerivedWithUnrelatedBase {
public func callSuperVirtualFromMultiBase() -> Int32 {
return super.virtualMethod()
}
}
// CHECK-LABEL: Derived.callSuperVirtual()
// CHECK-NOT: super_method
// CHECK-NOT: objc_super_method
// CHECK-NOT: _ZNK7Derived
// CHECK-NOT: __synthesizedVirtualCall
// CHECK: function_ref {{.*}}Base{{.*}}__staticCall_virtualMethod
// CHECK: end sil function
// CHECK-LABEL: LeafOverNoOverride.callSuperVirtualFromLeafOverNoOverride()
// CHECK-NOT: super_method
// CHECK-NOT: objc_super_method
// CHECK-NOT: _ZNK18LeafOverNoOverride
// CHECK-NOT: __synthesizedVirtualCall
// CHECK: function_ref {{.*}}Base{{.*}}__staticCall_virtualMethod
// CHECK: end sil function