Skip to content

Commit 511c46f

Browse files
authored
Merge pull request #69708 from hyp/eng/102393950
[interop][SwiftToCxx] dispatch 'class' methods directly to avoid brok…
2 parents 3d9533d + bcdfb8d commit 511c46f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
11311131
}
11321132
llvm::Optional<StringRef> indirectFunctionVar;
11331133
using DispatchKindTy = IRABIDetailsProvider::MethodDispatchInfo::Kind;
1134-
if (dispatchInfo) {
1134+
if (dispatchInfo && !isStaticMethod) {
1135+
// Always dispatch class methods directly to the concrete class instance.
11351136
switch (dispatchInfo->getKind()) {
11361137
case DispatchKindTy::Direct:
11371138
break;

test/Interop/SwiftToCxx/methods/method-in-cxx-execution.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,13 @@ int main() {
7272
largeStruct.dump();
7373
// CHECK-NEXT: 1, -1, -9075, -2, 9075, -456
7474
}
75+
76+
{
77+
auto x = ClassWithNonFinalMethods::classClassMethod(3);
78+
assert(x == 5);
79+
ClassWithNonFinalMethods::staticClassMethod();
80+
}
81+
// CHECK-NEXT: ClassWithNonFinalMethods.classClassMethod;
82+
// CHECK-NEXT: ClassWithNonFinalMethods.staticClassMethod;
7583
return 0;
7684
}

test/Interop/SwiftToCxx/methods/method-in-cxx.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public final class ClassWithMethods {
6161
}
6262
}
6363

64+
public class ClassWithNonFinalMethods {
65+
class public func classClassMethod(x: Int) -> Int {
66+
print("ClassWithNonFinalMethods.classClassMethod;")
67+
return x + 2
68+
}
69+
static public func staticClassMethod() {
70+
print("ClassWithNonFinalMethods.staticClassMethod;")
71+
}
72+
}
73+
6474
public final class PassStructInClassMethod {
6575
var largeStruct: LargeStruct
6676
init() { largeStruct = LargeStruct(x1: 1, x2: 2, x3: 3, x4: 4, x5: 5, x6: 6) }
@@ -163,6 +173,12 @@ public func createPassStructInClassMethod() -> PassStructInClassMethod {
163173
// CHECK-NEXT: _impl::$s7Methods09ClassWithA0C011staticFinalB6Method1xAA11LargeStructVSi_tFZ(result, x, swift::TypeMetadataTrait<ClassWithMethods>::getTypeMetadata());
164174
// CHECK-NEXT: });
165175
// CHECK-NEXT: }
176+
// CHECK-NEXT: SWIFT_INLINE_THUNK swift::Int ClassWithNonFinalMethods::classClassMethod(swift::Int x) {
177+
// CHECK-NEXT: return _impl::$s7Methods017ClassWithNonFinalA0C05classB6Method1xS2i_tFZ(x, swift::TypeMetadataTrait<ClassWithNonFinalMethods>::getTypeMetadata());
178+
// CHECK-NEXT: }
179+
// CHECK-NEXT: SWIFT_INLINE_THUNK void ClassWithNonFinalMethods::staticClassMethod() {
180+
// CHECK-NEXT: return _impl::$s7Methods017ClassWithNonFinalA0C06staticB6MethodyyFZ(swift::TypeMetadataTrait<ClassWithNonFinalMethods>::getTypeMetadata());
181+
// CHECK-NEXT: }
166182

167183
// CHECK: SWIFT_INLINE_THUNK LargeStruct LargeStruct::doubled() const {
168184
// CHECK-NEXT: return _impl::_impl_LargeStruct::returnNewValue([&](char * _Nonnull result) SWIFT_INLINE_THUNK_ATTRIBUTES {

0 commit comments

Comments
 (0)