Skip to content

Commit

Permalink
Work around excessive stack space in non optimized builds during oneo…
Browse files Browse the repository at this point in the history
…f isInitialized

Progress on: apple#1034
  • Loading branch information
thomasvl committed Aug 11, 2020
1 parent 1a2e235 commit 2b18383
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ class OneofGenerator {
"guard case \(f.dottedSwiftName)(let v) = self else {return true}\n",
"return v.isInitialized\n")
} else if fieldsToCheck.count > 1 {
p.print("switch self {\n")
p.print(
"// The use of inline closures is to circumvent an issue where the compiler\n",
"// allocates stack space for every case branch when no optimizations are\n",
"// enabled. https://github.com/apple/swift-protobuf/issues/1034\n",
"switch self {\n")
for f in fieldsToCheck {
p.print("case \(f.dottedSwiftName)(let v): return v.isInitialized\n")
p.print("case \(f.dottedSwiftName): return {\n")
p.indent()
p.print("guard case \(f.dottedSwiftName)(let v) = self else { preconditionFailure() }\n")
p.print("return v.isInitialized\n")
p.outdent()
p.print("}()\n")
}
// If there were other cases, add a default.
if fieldsToCheck.count != fields.count {
Expand Down

0 comments on commit 2b18383

Please sign in to comment.