@@ -1834,14 +1834,14 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
1834
1834
// Emit the init accessor function partially applied to the base.
1835
1835
SILValue initFRef = emitGlobalFunctionRef (
1836
1836
loc, getAccessorDeclRef (field->getOpaqueAccessor (AccessorKind::Init)));
1837
+
1838
+ auto initTy = initFRef->getType ().castTo <SILFunctionType>();
1839
+
1837
1840
if (!substitutions.empty ()) {
1838
1841
// If there are substitutions we need to emit partial apply to
1839
1842
// apply substitutions to the init accessor reference type.
1840
- auto initTy =
1841
- initFRef->getType ().castTo <SILFunctionType>()->substGenericArgs (
1842
- SGM.M , substitutions, getTypeExpansionContext ());
1843
-
1844
- SILFunctionConventions setterConv (initTy, SGM.M );
1843
+ initTy = initTy->substGenericArgs (SGM.M , substitutions,
1844
+ getTypeExpansionContext ());
1845
1845
1846
1846
// Emit partial apply without argument to produce a substituted
1847
1847
// init accessor reference.
@@ -1851,6 +1851,20 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
1851
1851
initFRef = emitManagedRValueWithCleanup (initPAI).getValue ();
1852
1852
}
1853
1853
1854
+ // Check whether value is supposed to be passed indirectly and
1855
+ // materialize if required.
1856
+ {
1857
+ SILFunctionConventions initConv (initTy, SGM.M );
1858
+
1859
+ auto newValueArgIdx = initConv.getSILArgIndexOfFirstParam ();
1860
+ // If we need the argument in memory, materialize an address.
1861
+ if (initConv.getSILArgumentConvention (newValueArgIdx)
1862
+ .isIndirectConvention () &&
1863
+ !newValue.getType ().isAddress ()) {
1864
+ newValue = newValue.materialize (*this , loc);
1865
+ }
1866
+ }
1867
+
1854
1868
SILValue setterFRef;
1855
1869
if (auto *setter = field->getOpaqueAccessor (AccessorKind::Set)) {
1856
1870
setterFRef = emitApplyOfSetterToBase (loc, SILDeclRef (setter), selfValue,
0 commit comments