@@ -1868,14 +1868,14 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18681868 // Emit the init accessor function partially applied to the base.
18691869 SILValue initFRef = emitGlobalFunctionRef (
18701870 loc, getAccessorDeclRef (field->getOpaqueAccessor (AccessorKind::Init)));
1871+
1872+ auto initTy = initFRef->getType ().castTo <SILFunctionType>();
1873+
18711874 if (!substitutions.empty ()) {
18721875 // If there are substitutions we need to emit partial apply to
18731876 // apply substitutions to the init accessor reference type.
1874- auto initTy =
1875- initFRef->getType ().castTo <SILFunctionType>()->substGenericArgs (
1876- SGM.M , substitutions, getTypeExpansionContext ());
1877-
1878- SILFunctionConventions setterConv (initTy, SGM.M );
1877+ initTy = initTy->substGenericArgs (SGM.M , substitutions,
1878+ getTypeExpansionContext ());
18791879
18801880 // Emit partial apply without argument to produce a substituted
18811881 // init accessor reference.
@@ -1885,6 +1885,20 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18851885 initFRef = emitManagedRValueWithCleanup (initPAI).getValue ();
18861886 }
18871887
1888+ // Check whether value is supposed to be passed indirectly and
1889+ // materialize if required.
1890+ {
1891+ SILFunctionConventions initConv (initTy, SGM.M );
1892+
1893+ auto newValueArgIdx = initConv.getSILArgIndexOfFirstParam ();
1894+ // If we need the argument in memory, materialize an address.
1895+ if (initConv.getSILArgumentConvention (newValueArgIdx)
1896+ .isIndirectConvention () &&
1897+ !newValue.getType ().isAddress ()) {
1898+ newValue = newValue.materialize (*this , loc);
1899+ }
1900+ }
1901+
18881902 SILValue setterFRef;
18891903 if (auto *setter = field->getOpaqueAccessor (AccessorKind::Set)) {
18901904 setterFRef = emitApplyOfSetterToBase (loc, SILDeclRef (setter), selfValue,
0 commit comments