@@ -2109,7 +2109,8 @@ VarargsInfo Lowering::emitBeginVarargs(SILGenFunction &SGF, SILLocation loc,
21092109}
21102110
21112111ManagedValue Lowering::emitEndVarargs (SILGenFunction &SGF, SILLocation loc,
2112- VarargsInfo &&varargs) {
2112+ VarargsInfo &&varargs,
2113+ unsigned numElements) {
21132114 // Kill the abort cleanup.
21142115 SGF.Cleanups .setCleanupState (varargs.getAbortCleanup (), CleanupState::Dead);
21152116
@@ -2118,6 +2119,14 @@ ManagedValue Lowering::emitEndVarargs(SILGenFunction &SGF, SILLocation loc,
21182119 if (array.hasCleanup ())
21192120 SGF.Cleanups .setCleanupState (array.getCleanup (), CleanupState::Active);
21202121
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+
21212130 return SGF.emitUninitializedArrayFinalization (loc, std::move (array));
21222131}
21232132
@@ -3870,7 +3879,7 @@ RValue RValueEmitter::visitCollectionExpr(CollectionExpr *E, SGFContext C) {
38703879 SGF.Cleanups .setCleanupState (destCleanup, CleanupState::Dead);
38713880
38723881 RValue array (SGF, loc, arrayType,
3873- emitEndVarargs (SGF, loc, std::move (varargsInfo)));
3882+ emitEndVarargs (SGF, loc, std::move (varargsInfo), E-> getNumElements ( )));
38743883
38753884 array = scope.popPreservingValue (std::move (array));
38763885
0 commit comments