@@ -69,9 +69,16 @@ void TupleInitialization::copyOrInitValueInto(SILGenFunction &SGF,
6969 // In the address case, we forward the underlying value and store it
7070 // into memory and then create a +1 cleanup. since we assume here
7171 // that we have a +1 value since we are forwarding into memory.
72+ //
73+ // In order to ensure that we properly clean up along any failure paths, we
74+ // need to mark value as being persistently active. We then unforward it once
75+ // we are done.
7276 assert (value.isPlusOne (SGF) && " Can not store a +0 value into memory?!" );
73- value = ManagedValue::forUnmanaged (value.forward (SGF));
74- return copyOrInitValueIntoHelper (
77+ CleanupStateRestorationScope valueScope (SGF.Cleanups );
78+ if (value.hasCleanup ())
79+ valueScope.pushCleanupState (value.getCleanup (),
80+ CleanupState::PersistentlyActive);
81+ copyOrInitValueIntoHelper (
7582 SGF, loc, value, isInit, SubInitializations,
7683 [&](ManagedValue aggregate, unsigned i,
7784 SILType fieldType) -> ManagedValue {
@@ -83,6 +90,8 @@ void TupleInitialization::copyOrInitValueInto(SILGenFunction &SGF,
8390
8491 return SGF.emitManagedRValueWithCleanup (elt.getValue ());
8592 });
93+ std::move (valueScope).pop ();
94+ value.forward (SGF);
8695}
8796
8897void TupleInitialization::finishUninitialized (SILGenFunction &SGF) {
0 commit comments