@@ -1315,6 +1315,17 @@ void SignatureExpansion::expandExternalSignatureTypes() {
13151315 // Swift parameters list?
13161316 size_t clangToSwiftParamOffset = paramTys.size ();
13171317
1318+ // This is exactly the same as "params" but without thin metatypes. This must
1319+ // exist so that when the path for "Indirect" arguments looks for the
1320+ // SILParameterInfo for a given index, we get the parameter info for the
1321+ // coorisponding clang type's index and not a "random" parameter's info.
1322+ //
1323+ // This is only an issue in very rare cases when we have a constructor that
1324+ // looks like this: (T, @thin U.Type) -> @out U. In this case "params" will
1325+ // contain two elements and "paramTys" will contain two elements so the
1326+ // "Indirect" argument path gets confused and selects the second parameter
1327+ // (the thin metatype) instead of the first one.
1328+ SmallVector<SILParameterInfo, 4 > adjustedSILParams;
13181329 // Convert each parameter to a Clang type.
13191330 for (auto param : params) {
13201331 auto clangTy = IGM.getClangType (param, FnType);
@@ -1324,6 +1335,7 @@ void SignatureExpansion::expandExternalSignatureTypes() {
13241335 continue ;
13251336 }
13261337 paramTys.push_back (clangTy);
1338+ adjustedSILParams.push_back (param);
13271339 }
13281340
13291341 // Generate function info for this signature.
@@ -1413,7 +1425,7 @@ void SignatureExpansion::expandExternalSignatureTypes() {
14131425 case clang::CodeGen::ABIArgInfo::Indirect: {
14141426 assert (i >= clangToSwiftParamOffset &&
14151427 " Unexpected index for indirect byval argument" );
1416- auto ¶m = params [i - clangToSwiftParamOffset];
1428+ auto ¶m = adjustedSILParams [i - clangToSwiftParamOffset];
14171429 auto paramTy = getSILFuncConventions ().getSILType (
14181430 param, IGM.getMaximalTypeExpansionContext ());
14191431 auto ¶mTI = cast<FixedTypeInfo>(IGM.getTypeInfo (paramTy));
0 commit comments