Skip to content

Commit 1eb3548

Browse files
committed
(Mostly) revert "SILGen: TupleShuffleExprs in the rvalue emission path can't have scalar-to-tuple or tuple-to-scalar"
We use TupleShuffleExpr in RValue position for enum element payloads, and it can happen that the source is a scalar if you're calling the enum element constructor with a trailing closure. This reverts commit 7960660. Fixes <https://bugs.swift.org/browse/SR-9675>.
1 parent c04b9fe commit 1eb3548

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,9 +2292,13 @@ static void emitTupleShuffleExprInto(RValueEmitter &emitter,
22922292
// Map outer initializations into a tuple of inner initializations:
22932293
// - fill out the initialization elements with null
22942294
TupleInitialization innerTupleInit;
2295-
CanTupleType innerTuple =
2296-
cast<TupleType>(E->getSubExpr()->getType()->getCanonicalType());
2297-
innerTupleInit.SubInitializations.resize(innerTuple->getNumElements());
2295+
if (E->isSourceScalar()) {
2296+
innerTupleInit.SubInitializations.push_back(nullptr);
2297+
} else {
2298+
CanTupleType innerTuple =
2299+
cast<TupleType>(E->getSubExpr()->getType()->getCanonicalType());
2300+
innerTupleInit.SubInitializations.resize(innerTuple->getNumElements());
2301+
}
22982302

22992303
// Map all the outer initializations to their appropriate targets.
23002304
for (unsigned outerIndex = 0; outerIndex != outerInits.size(); outerIndex++) {
@@ -2312,14 +2316,20 @@ static void emitTupleShuffleExprInto(RValueEmitter &emitter,
23122316
#endif
23132317

23142318
// Emit the sub-expression into the tuple initialization we just built.
2315-
emitter.SGF.emitExprInto(E->getSubExpr(), &innerTupleInit);
2319+
if (E->isSourceScalar()) {
2320+
emitter.SGF.emitExprInto(E->getSubExpr(),
2321+
innerTupleInit.SubInitializations[0].get());
2322+
} else {
2323+
emitter.SGF.emitExprInto(E->getSubExpr(), &innerTupleInit);
2324+
}
23162325

23172326
outerTupleInit->finishInitialization(emitter.SGF);
23182327
}
23192328

23202329
RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
23212330
SGFContext C) {
2322-
assert(!E->isSourceScalar());
2331+
// FIXME: Once we're no longer using this code path for enum element payloads,
2332+
// also assert that !E->isSourceScalar().
23232333
assert(!E->isResultScalar());
23242334

23252335
// If we're emitting into an initialization, we can try shuffling the
@@ -2333,7 +2343,11 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
23332343

23342344
// Emit the sub-expression tuple and destructure it into elements.
23352345
SmallVector<RValue, 4> elements;
2336-
visit(E->getSubExpr()).extractElements(elements);
2346+
if (E->isSourceScalar()) {
2347+
elements.push_back(visit(E->getSubExpr()));
2348+
} else {
2349+
visit(E->getSubExpr()).extractElements(elements);
2350+
}
23372351

23382352
// Prepare a new tuple to hold the shuffled result.
23392353
RValue result(E->getType()->getCanonicalType());

test/SILGen/enum.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,33 @@ enum Indirect<T> {
204204
func makeIndirectEnum<T>(_ payload: T) -> Indirect<T> {
205205
return Indirect.payload((payload, other: payload))
206206
}
207+
208+
// https://bugs.swift.org/browse/SR-9675
209+
210+
enum TrailingClosureConcrete {
211+
case label(fn: () -> Int)
212+
case noLabel(() -> Int)
213+
case twoElementsLabel(x: Int, fn: () -> Int)
214+
case twoElementsNoLabel(_ x: Int, _ fn: () -> Int)
215+
}
216+
217+
func useTrailingClosureConcrete() {
218+
_ = TrailingClosureConcrete.label { 0 }
219+
_ = TrailingClosureConcrete.noLabel { 0 }
220+
_ = TrailingClosureConcrete.twoElementsLabel(x: 0) { 0 }
221+
_ = TrailingClosureConcrete.twoElementsNoLabel(0) { 0 }
222+
}
223+
224+
enum TrailingClosureGeneric<T> {
225+
case label(fn: () -> T)
226+
case noLabel(() -> T)
227+
case twoElementsLabel(x: T, fn: () -> T)
228+
case twoElementsNoLabel(_ x: T, _ fn: () -> T)
229+
}
230+
231+
func useTrailingClosureGeneric<T>(t: T) {
232+
_ = TrailingClosureGeneric<T>.label { t }
233+
_ = TrailingClosureGeneric<T>.noLabel { t }
234+
_ = TrailingClosureGeneric<T>.twoElementsLabel(x: t) { t }
235+
_ = TrailingClosureGeneric<T>.twoElementsNoLabel(t) { t }
236+
}

0 commit comments

Comments
 (0)