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: #1034
  • Loading branch information
thomasvl committed Aug 13, 2020
1 parent 262127d commit d6422ae
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 d6422ae

Please sign in to comment.