Skip to content

Commit 04ed960

Browse files
authored
Merge pull request swiftlang#84157 from jckarter/protocol-objc-class-dyn-self-mismatch-6.2
[6.2] SILGen: Use lowered function type parameters in objc-to-swift thunks for initializers.
2 parents e9bdb06 + f3d3c5a commit 04ed960

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,23 +2170,22 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
21702170
SmallVector<SILValue, 8> params;
21712171

21722172
bindParametersForForwarding(fd->getParameters(), params);
2173-
if (thunk.kind != SILDeclRef::Kind::Allocator)
2174-
if (auto *selfDecl = fd->getImplicitSelfDecl())
2175-
bindParameterForForwarding(selfDecl, params);
2176-
2177-
// For allocating constructors, 'self' is a metatype, not the 'self' value
2178-
// formally present in the constructor body.
21792173
Type allocatorSelfType;
2180-
if (thunk.kind == SILDeclRef::Kind::Allocator) {
2181-
auto *selfDecl = fd->getImplicitSelfDecl();
2182-
allocatorSelfType = F.mapTypeIntoContext(
2183-
fd->getDeclContext()->getSelfInterfaceType());
2184-
2185-
auto selfMetatype =
2186-
CanMetatypeType::get(allocatorSelfType->getCanonicalType());
2187-
auto selfArg = F.begin()->createFunctionArgument(
2188-
getLoweredLoadableType(selfMetatype), selfDecl);
2174+
if (auto *selfDecl = fd->getImplicitSelfDecl()) {
2175+
// The self declaration sometimes differ in dynamic-self-ness from the
2176+
// lowered function type, so get the self parameter type from the function
2177+
// type rather than the declaration.
2178+
auto selfArgTy = F.getLoweredFunctionType()->getSelfParameter()
2179+
.getSILStorageType(getModule(), F.getLoweredFunctionType(), getTypeExpansionContext());
2180+
auto selfArg = F.begin()->createFunctionArgument(F.mapTypeIntoContext(selfArgTy), selfDecl);
21892181
params.push_back(selfArg);
2182+
2183+
// For allocating constructors, 'self' is a metatype, not the 'self' value
2184+
// formally present in the constructor body.
2185+
if (thunk.kind == SILDeclRef::Kind::Allocator) {
2186+
allocatorSelfType = F.mapTypeIntoContext(
2187+
fd->getDeclContext()->getSelfInterfaceType());
2188+
}
21902189
}
21912190

21922191
// Set up the throw destination if necessary.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import Foundation;
2+
3+
@interface C: NSObject
4+
5+
+(_Nonnull instancetype)foo;
6+
7+
@end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) -import-objc-header %S/Inputs/protocol-static-reqt-objc-class-impl.h %s -verify
2+
// REQUIRES: objc_interop
3+
4+
protocol P {
5+
static func foo() -> Self
6+
}
7+
8+
extension C: P {}
9+

0 commit comments

Comments
 (0)