@@ -2109,7 +2109,8 @@ VarargsInfo Lowering::emitBeginVarargs(SILGenFunction &SGF, SILLocation loc,
2109
2109
}
2110
2110
2111
2111
ManagedValue Lowering::emitEndVarargs (SILGenFunction &SGF, SILLocation loc,
2112
- VarargsInfo &&varargs) {
2112
+ VarargsInfo &&varargs,
2113
+ unsigned numElements) {
2113
2114
// Kill the abort cleanup.
2114
2115
SGF.Cleanups .setCleanupState (varargs.getAbortCleanup (), CleanupState::Dead);
2115
2116
@@ -2118,6 +2119,14 @@ ManagedValue Lowering::emitEndVarargs(SILGenFunction &SGF, SILLocation loc,
2118
2119
if (array.hasCleanup ())
2119
2120
SGF.Cleanups .setCleanupState (array.getCleanup (), CleanupState::Active);
2120
2121
2122
+ // Array literals only need to be finalized, if the array is really allocated.
2123
+ // In case of zero elements, no allocation is done, but the empty-array
2124
+ // singleton is used. "Finalization" means to emit an end_cow_mutation
2125
+ // instruction on the array. As the empty-array singleton is a read-only and
2126
+ // shared object, it's not legal to do a end_cow_mutation on it.
2127
+ if (numElements == 0 )
2128
+ return array;
2129
+
2121
2130
return SGF.emitUninitializedArrayFinalization (loc, std::move (array));
2122
2131
}
2123
2132
@@ -3870,7 +3879,7 @@ RValue RValueEmitter::visitCollectionExpr(CollectionExpr *E, SGFContext C) {
3870
3879
SGF.Cleanups .setCleanupState (destCleanup, CleanupState::Dead);
3871
3880
3872
3881
RValue array (SGF, loc, arrayType,
3873
- emitEndVarargs (SGF, loc, std::move (varargsInfo)));
3882
+ emitEndVarargs (SGF, loc, std::move (varargsInfo), E-> getNumElements ( )));
3874
3883
3875
3884
array = scope.popPreservingValue (std::move (array));
3876
3885
0 commit comments