Skip to content

Commit 4c470c3

Browse files
authored
Merge pull request #68623 from eeckstein/fix-singleton-enum-5.10
[5.10] IRGen: fix emission of constant single-case enums without payload
2 parents 34322fc + db16753 commit 4c470c3

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

lib/IRGen/GenConstant.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
275275
} else if (auto *ei = dyn_cast<EnumInst>(operand)) {
276276
auto &strategy = getEnumImplStrategy(IGM, ei->getType());
277277
if (strategy.emitPayloadDirectlyIntoConstant()) {
278-
return emitConstantValue(IGM, ei->getOperand(), flatten);
278+
if (ei->hasOperand()) {
279+
return emitConstantValue(IGM, ei->getOperand(), flatten);
280+
}
281+
return Explosion();
279282
}
280283

281284
Explosion data;

test/SILOptimizer/static_enums.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ enum SingleCaseEnum {
224224
static var x = Self.a(b:true, i: 42)
225225
}
226226

227+
enum SingleCaseEnumWithoutPayload {
228+
case a
229+
static var x = Self.a
230+
}
231+
232+
enum NestedSingleCaseEnum {
233+
case u
234+
case v(SingleCaseEnumWithoutPayload)
235+
236+
static var x = Self.v(.a)
237+
}
238+
227239
@main
228240
struct Main {
229241
static func main() {
@@ -301,6 +313,10 @@ struct Main {
301313
printFunctionEnum()
302314
// CHECK-OUTPUT: SingleCaseEnum: a(b: true, i: 42)
303315
print("SingleCaseEnum:", SingleCaseEnum.x)
316+
// CHECK-OUTPUT: SingleCaseEnumWithoutPayload: a
317+
print("SingleCaseEnumWithoutPayload:", SingleCaseEnumWithoutPayload.x)
318+
// CHECK-OUTPUT: NestedSingleCaseEnum: v(test.SingleCaseEnumWithoutPayload.a)
319+
print("NestedSingleCaseEnum:", NestedSingleCaseEnum.x)
304320
}
305321
}
306322

validation-test/SILOptimizer/static_enums_fuzzing.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
createTestfile()
1313

1414
func createTestfile() {
15-
let globals = createGlobals(count: 500)
15+
let globals = createGlobals(count: 1000)
1616

1717
print(typeDefinitions)
1818

@@ -77,6 +77,10 @@ var typeDefinitions: String {
7777
case A(T)
7878
}
7979
80+
public enum SCEWP {
81+
case A
82+
}
83+
8084
public enum SPE<T> {
8185
case A(T)
8286
case B
@@ -322,6 +326,33 @@ struct SinglePayloadSingleCaseEnum : Value {
322326
var containsEnum: Bool { true }
323327
}
324328

329+
struct SingleCaseEnumWithoutPayload : Value {
330+
331+
init(generator: inout RandomGenerator, depth: Int) {
332+
}
333+
334+
func getType() -> String {
335+
"SCEWP"
336+
}
337+
338+
func getInitValue() -> String {
339+
return "SCEWP.A"
340+
}
341+
342+
func getExpectedOutput(topLevel: Bool) -> String {
343+
let prefix = topLevel ? "" : "\(getRuntimeTypeName(topLevel: topLevel))."
344+
return "\(prefix)A"
345+
}
346+
347+
func getRuntimeTypeName(topLevel: Bool) -> String {
348+
let prefix = topLevel ? "" : "test."
349+
return "\(prefix)SCEWP"
350+
}
351+
352+
var containsEnum: Bool { true }
353+
}
354+
355+
325356
struct SinglePayloadEnum : Value {
326357
let payload: any Value
327358
let caseIdx: Int
@@ -477,7 +508,8 @@ struct RandomGenerator : RandomNumberGenerator {
477508
LargeString.self,
478509
Function.self,
479510
Enum.self,
480-
Size24Enum.self
511+
Size24Enum.self,
512+
SingleCaseEnumWithoutPayload.self
481513
]
482514
private static let allValueTypes: [any Value.Type] = allTerminalTypes + [
483515
OptionalValue.self,

0 commit comments

Comments
 (0)