Skip to content

Commit d590b94

Browse files
committed
InitializeStaticGlobals: fix handling of nested non-loadable enums
Fixes a compiler crash rdar://160880083
1 parent 648aa41 commit d590b94

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,15 @@ private indirect enum GlobalInitValue {
233233
}
234234

235235
private mutating func setEnumCase(to value: InitValue, at path: SmallProjectionPath, index: Int, type: Type) -> Bool {
236-
switch value {
236+
if path.isEmpty, case .enumCaseWithoutPayload(let iea) = value {
237237

238-
case .enumCaseWithoutPayload(let iea):
239238
guard case .undefined = self else {
240239
// The enum was set twice.
241240
return false
242241
}
243242
assert(index == iea.caseIndex)
244243
self = .enumCase(caseIndex: index)
245-
246-
case .value:
244+
} else {
247245
guard let payloadType = type.getEnumCases(in: value.parentFunction)!.getPayloadType(ofCaseIndex: index) else {
248246
return false
249247
}

test/SILOptimizer/init_static_globals.sil

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ struct TwoFields {
3737
let b: Int32
3838
}
3939

40+
struct WithOptionalEnum {
41+
var e: E?
42+
}
43+
44+
enum E {
45+
case a
46+
case b
47+
}
48+
4049
@_rawLayout(like: Int32)
4150
struct Raw: ~Copyable {}
4251

@@ -183,6 +192,13 @@ sil_global [let] @gIndirectNone: $Optional<TwoFields>
183192
// CHECK-NEXT: }
184193
sil_global [let] @gIndirectSome: $Optional<TwoFields>
185194

195+
// CHECK-LABEL: sil_global [let] @structWithOptionalEnum : $WithOptionalEnum = {
196+
// CHECK-NEXT: %0 = enum $E, #E.a!enumelt
197+
// CHECK-NEXT: %1 = enum $Optional<E>, #Optional.some!enumelt, %0
198+
// CHECK-NEXT: %initval = struct $WithOptionalEnum (%1)
199+
// CHECK-NEXT: }
200+
sil_global [let] @structWithOptionalEnum : $WithOptionalEnum
201+
186202
sil @unknownfunc : $@convention(thin) () -> ()
187203

188204
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func :
@@ -620,3 +636,15 @@ bb0:
620636
return %10 : $()
621637
}
622638

639+
sil [global_init_once_fn] @globalinit_structWithOptionalEnum : $@convention(c) (Builtin.RawPointer) -> () {
640+
bb0(%0 : $Builtin.RawPointer):
641+
alloc_global @structWithOptionalEnum
642+
%2 = global_addr @structWithOptionalEnum : $*WithOptionalEnum
643+
%3 = struct_element_addr %2, #WithOptionalEnum.e
644+
%4 = init_enum_data_addr %3, #Optional.some!enumelt
645+
inject_enum_addr %4, #E.a!enumelt
646+
inject_enum_addr %3, #Optional.some!enumelt
647+
%7 = tuple ()
648+
return %7
649+
}
650+

0 commit comments

Comments
 (0)