@@ -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.
0 commit comments