Skip to content

Commit 1a5ea19

Browse files
committed
SimplifyApply: don't do the raw-enum comparison optimization for custom RawRepresentable enums
Because we don't know what the custom `rawValue` can do. Fixes a miscompile rdar://152143111
1 parent 28985f4 commit 1a5ea19

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyApply.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ private func tryOptimizeEnumComparison(apply: ApplyInst, _ context: SimplifyCont
135135
let lhs = apply.arguments[0]
136136
let rhs = apply.arguments[1]
137137
guard let enumDecl = lhs.type.nominal as? EnumDecl,
138+
enumDecl.hasRawType,
138139
!enumDecl.isResilient(in: apply.parentFunction),
139140
!enumDecl.hasClangNode,
140141
lhs.type.isAddress,

test/SILOptimizer/enum-comparison.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ func compare4(_ x: LargeEnum) -> Bool {
8080
return x == .e3(28)
8181
}
8282

83+
enum CustomRawValue: RawRepresentable {
84+
case a, b, c
85+
86+
init(rawValue: Int) {
87+
self = .a
88+
}
89+
90+
@inline(never)
91+
var rawValue: Int {
92+
print(0)
93+
return 0
94+
}
95+
}
96+
97+
// CHECK-LABEL: define {{.*}} i1 @"$s4test8compare5ySbAA14CustomRawValueOF"(i8 %0)
98+
// CHECK: entry:
99+
// CHECK-NEXT: call
100+
@inline(never)
101+
func compare5(_ x: CustomRawValue) -> Bool {
102+
return x == .b
103+
}
104+
83105
// OUT: 1: false
84106
print("1: \(compareeq(.c, .long_case_name_for_testing))")
85107

@@ -116,3 +138,6 @@ print("11: \(compare4(.e3(28)))")
116138
// OUT: 12: false
117139
print("12: \(compare4(.e3(27)))")
118140

141+
// OUT: 13: true
142+
print("13: \(compare5(.a))")
143+

0 commit comments

Comments
 (0)