@@ -2170,23 +2170,22 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
2170
2170
SmallVector<SILValue, 8 > params;
2171
2171
2172
2172
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.
2179
2173
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);
2189
2181
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
+ }
2190
2189
}
2191
2190
2192
2191
// Set up the throw destination if necessary.
0 commit comments