Skip to content

Commit f8d2881

Browse files
committed
IRGen: Relax an assertion
The result type mismatch comes up when we're calling a covariant override of a class method resiliently as well.
1 parent a5abb9d commit f8d2881

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,11 +1510,14 @@ void CallEmission::emitToUnmappedExplosion(Explosion &out) {
15101510

15111511
// For ABI reasons the result type of the call might not actually match the
15121512
// expected result type.
1513+
//
1514+
// This can happen when calling C functions, or class method dispatch thunks
1515+
// for methods that have covariant ABI-compatible overrides.
15131516
auto expectedNativeResultType = nativeSchema.getExpandedType(IGF.IGM);
15141517
if (result->getType() != expectedNativeResultType) {
1515-
// This should only be needed when we call C functions.
1516-
assert(getCallee().getOrigFunctionType()->getLanguage() ==
1517-
SILFunctionLanguage::C);
1518+
auto origFnType = getCallee().getOrigFunctionType();
1519+
assert(origFnType->getLanguage() == SILFunctionLanguage::C ||
1520+
origFnType->getRepresentation() == SILFunctionTypeRepresentation::Method);
15181521
result =
15191522
IGF.coerceValue(result, expectedNativeResultType, IGF.IGM.DataLayout);
15201523
}

test/IRGen/class_resilience_thunks.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ public func testDispatchThunkMyOverride(d: MyDerived, o: Object) {
5555

5656
// CHECK: ret void
5757
}
58+
59+
public func testDispatchThunkCast(d: Derived) {
60+
_ = d.returnsSuperclass()
61+
}

test/Inputs/resilient_class_thunks.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ public class Object {
22
public init() {}
33
}
44

5+
public class Subclass : Object {}
6+
57
open class Base<T> {
68
open func takesT(_: T) {}
79

810
open func takesInt(_: Int) {}
911

1012
open func takesReference(_: Object) {}
13+
14+
open func returnsSuperclass() -> Object {
15+
fatalError()
16+
}
1117
}
1218

1319
open class Derived : Base<Int> {
@@ -19,4 +25,9 @@ open class Derived : Base<Int> {
1925

2026
// Override has different formal type but is ABI-compatible
2127
open override func takesReference(_: Object?) {}
28+
29+
// Override has a more specific return type but is ABI-compatible
30+
open override func returnsSuperclass() -> Subclass {
31+
fatalError()
32+
}
2233
}

0 commit comments

Comments
 (0)