From 2b1838350bc4206dc49695bfb3ac595c6b9b95f6 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 11 Aug 2020 16:49:13 -0400 Subject: [PATCH] Work around excessive stack space in non optimized builds during oneof isInitialized Progress on: https://github.com/apple/swift-protobuf/issues/1034 --- Sources/protoc-gen-swift/OneofGenerator.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/protoc-gen-swift/OneofGenerator.swift b/Sources/protoc-gen-swift/OneofGenerator.swift index 53b7370f3..313dea59b 100644 --- a/Sources/protoc-gen-swift/OneofGenerator.swift +++ b/Sources/protoc-gen-swift/OneofGenerator.swift @@ -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 {