Skip to content

IRGen: fix emission of constant single-case enums without payload #68616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/IRGen/GenConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
} else if (auto *ei = dyn_cast<EnumInst>(operand)) {
auto &strategy = getEnumImplStrategy(IGM, ei->getType());
if (strategy.emitPayloadDirectlyIntoConstant()) {
return emitConstantValue(IGM, ei->getOperand(), flatten);
if (ei->hasOperand()) {
return emitConstantValue(IGM, ei->getOperand(), flatten);
}
return Explosion();
}

Explosion data;
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/static_enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ enum SingleCaseEnum {
static var x = Self.a(b:true, i: 42)
}

enum SingleCaseEnumWithoutPayload {
case a
static var x = Self.a
}

enum NestedSingleCaseEnum {
case u
case v(SingleCaseEnumWithoutPayload)

static var x = Self.v(.a)
}

@main
struct Main {
static func main() {
Expand Down Expand Up @@ -301,6 +313,10 @@ struct Main {
printFunctionEnum()
// CHECK-OUTPUT: SingleCaseEnum: a(b: true, i: 42)
print("SingleCaseEnum:", SingleCaseEnum.x)
// CHECK-OUTPUT: SingleCaseEnumWithoutPayload: a
print("SingleCaseEnumWithoutPayload:", SingleCaseEnumWithoutPayload.x)
// CHECK-OUTPUT: NestedSingleCaseEnum: v(test.SingleCaseEnumWithoutPayload.a)
print("NestedSingleCaseEnum:", NestedSingleCaseEnum.x)
}
}

Expand Down
36 changes: 34 additions & 2 deletions validation-test/SILOptimizer/static_enums_fuzzing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
createTestfile()

func createTestfile() {
let globals = createGlobals(count: 500)
let globals = createGlobals(count: 1000)

print(typeDefinitions)

Expand Down Expand Up @@ -77,6 +77,10 @@ var typeDefinitions: String {
case A(T)
}

public enum SCEWP {
case A
}

public enum SPE<T> {
case A(T)
case B
Expand Down Expand Up @@ -322,6 +326,33 @@ struct SinglePayloadSingleCaseEnum : Value {
var containsEnum: Bool { true }
}

struct SingleCaseEnumWithoutPayload : Value {

init(generator: inout RandomGenerator, depth: Int) {
}

func getType() -> String {
"SCEWP"
}

func getInitValue() -> String {
return "SCEWP.A"
}

func getExpectedOutput(topLevel: Bool) -> String {
let prefix = topLevel ? "" : "\(getRuntimeTypeName(topLevel: topLevel))."
return "\(prefix)A"
}

func getRuntimeTypeName(topLevel: Bool) -> String {
let prefix = topLevel ? "" : "test."
return "\(prefix)SCEWP"
}

var containsEnum: Bool { true }
}


struct SinglePayloadEnum : Value {
let payload: any Value
let caseIdx: Int
Expand Down Expand Up @@ -477,7 +508,8 @@ struct RandomGenerator : RandomNumberGenerator {
LargeString.self,
Function.self,
Enum.self,
Size24Enum.self
Size24Enum.self,
SingleCaseEnumWithoutPayload.self
]
private static let allValueTypes: [any Value.Type] = allTerminalTypes + [
OptionalValue.self,
Expand Down