diff --git a/lib/SILGen/SILGenBridging.cpp b/lib/SILGen/SILGenBridging.cpp index d7cb7c1324009..de9f21aa75131 100644 --- a/lib/SILGen/SILGenBridging.cpp +++ b/lib/SILGen/SILGenBridging.cpp @@ -2170,23 +2170,22 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) { SmallVector params; bindParametersForForwarding(fd->getParameters(), params); - if (thunk.kind != SILDeclRef::Kind::Allocator) - if (auto *selfDecl = fd->getImplicitSelfDecl()) - bindParameterForForwarding(selfDecl, params); - - // For allocating constructors, 'self' is a metatype, not the 'self' value - // formally present in the constructor body. Type allocatorSelfType; - if (thunk.kind == SILDeclRef::Kind::Allocator) { - auto *selfDecl = fd->getImplicitSelfDecl(); - allocatorSelfType = F.mapTypeIntoContext( - fd->getDeclContext()->getSelfInterfaceType()); - - auto selfMetatype = - CanMetatypeType::get(allocatorSelfType->getCanonicalType()); - auto selfArg = F.begin()->createFunctionArgument( - getLoweredLoadableType(selfMetatype), selfDecl); + if (auto *selfDecl = fd->getImplicitSelfDecl()) { + // The self declaration sometimes differ in dynamic-self-ness from the + // lowered function type, so get the self parameter type from the function + // type rather than the declaration. + auto selfArgTy = F.getLoweredFunctionType()->getSelfParameter() + .getSILStorageType(getModule(), F.getLoweredFunctionType(), getTypeExpansionContext()); + auto selfArg = F.begin()->createFunctionArgument(F.mapTypeIntoContext(selfArgTy), selfDecl); params.push_back(selfArg); + + // For allocating constructors, 'self' is a metatype, not the 'self' value + // formally present in the constructor body. + if (thunk.kind == SILDeclRef::Kind::Allocator) { + allocatorSelfType = F.mapTypeIntoContext( + fd->getDeclContext()->getSelfInterfaceType()); + } } // Set up the throw destination if necessary. diff --git a/test/SILGen/Inputs/protocol-static-reqt-objc-class-impl.h b/test/SILGen/Inputs/protocol-static-reqt-objc-class-impl.h new file mode 100644 index 0000000000000..9f8932fb7fd25 --- /dev/null +++ b/test/SILGen/Inputs/protocol-static-reqt-objc-class-impl.h @@ -0,0 +1,7 @@ +@import Foundation; + +@interface C: NSObject + ++(_Nonnull instancetype)foo; + +@end diff --git a/test/SILGen/protocol-static-reqt-objc-class-impl.swift b/test/SILGen/protocol-static-reqt-objc-class-impl.swift new file mode 100644 index 0000000000000..20d08b95ed79a --- /dev/null +++ b/test/SILGen/protocol-static-reqt-objc-class-impl.swift @@ -0,0 +1,9 @@ +// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) -import-objc-header %S/Inputs/protocol-static-reqt-objc-class-impl.h %s -verify +// REQUIRES: objc_interop + +protocol P { + static func foo() -> Self +} + +extension C: P {} +