Skip to content

Commit 17684d0

Browse files
committed
SILGen: Admit ArrayExpr without an initializer
This is equivalent to the trivial case of an ArrayExpr with the Array.init(arrayLiteral: T...) initializer; it will be used by CSApply to build vararg arrays.
1 parent b9ef570 commit 17684d0

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,10 +3590,21 @@ RValue RValueEmitter::visitArrayExpr(ArrayExpr *E, SGFContext C) {
35903590
auto loc = SILLocation(E);
35913591
ArgumentScope scope(SGF, loc);
35923592

3593-
CanType elementType = E->getElementType()->getCanonicalType();
3594-
CanType arrayTy = ArraySliceType::get(elementType)->getCanonicalType();
3593+
// CSApply builds ArrayExprs without an initializer for the trivial case
3594+
// of emitting varargs.
3595+
CanType arrayType, elementType;
3596+
if (E->getInitializer()) {
3597+
elementType = E->getElementType()->getCanonicalType();
3598+
arrayType = ArraySliceType::get(elementType)->getCanonicalType();
3599+
} else {
3600+
arrayType = E->getType()->getCanonicalType();
3601+
auto genericType = cast<BoundGenericStructType>(arrayType);
3602+
assert(genericType->getDecl() == SGF.getASTContext().getArrayDecl());
3603+
elementType = genericType.getGenericArgs()[0];
3604+
}
3605+
35953606
VarargsInfo varargsInfo =
3596-
emitBeginVarargs(SGF, loc, elementType, arrayTy,
3607+
emitBeginVarargs(SGF, loc, elementType, arrayType,
35973608
E->getNumElements());
35983609

35993610
// Cleanups for any elements that have been initialized so far.
@@ -3628,14 +3639,14 @@ RValue RValueEmitter::visitArrayExpr(ArrayExpr *E, SGFContext C) {
36283639
for (auto destCleanup : cleanups)
36293640
SGF.Cleanups.setCleanupState(destCleanup, CleanupState::Dead);
36303641

3631-
RValue arg(SGF, loc, arrayTy,
3642+
RValue arg(SGF, loc, arrayType,
36323643
emitEndVarargs(SGF, loc, std::move(varargsInfo)));
36333644

36343645
arg = scope.popPreservingValue(std::move(arg));
36353646

36363647
// If we're building an array, we don't have to call the initializer;
36373648
// we've already built one.
3638-
if (arrayTy->isEqual(E->getType()))
3649+
if (arrayType->isEqual(E->getType()))
36393650
return arg;
36403651

36413652
// Call the builtin initializer.

0 commit comments

Comments
 (0)