From 16e01f16c6c43333e5878397847740e662b7c45d Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Sat, 20 Jun 2026 14:37:59 +0900 Subject: [PATCH 1/2] CodePrinting: add printGuardBlock helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirror `printIfBlock` for Swift `guard else { … }` blocks, so generators don't have to spell `printBraceBlock("guard \(cond) else")` inline. Migrates the one existing call site in `JNISwift2JavaGenerator+SwiftThunkPrinting.swift` to use it. The condition is passed through verbatim — `guard` in Swift is written without parentheses around the condition, and the condition often binds (`let x = optional`) so wrapping it would be wrong. --- Sources/CodePrinting/CodePrinter.swift | 11 ++++ ...ift2JavaGenerator+SwiftThunkPrinting.swift | 2 +- .../PrintGuardBlockTests.swift | 51 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 Tests/CodePrintingTests/PrintGuardBlockTests.swift diff --git a/Sources/CodePrinting/CodePrinter.swift b/Sources/CodePrinting/CodePrinter.swift index 4d91c884b..1fa3db8dd 100644 --- a/Sources/CodePrinting/CodePrinter.swift +++ b/Sources/CodePrinting/CodePrinter.swift @@ -134,6 +134,17 @@ public struct CodePrinter: Sendable { try printBraceBlock("if (\(condition))", function: function, file: file, line: line, body: body) } + /// Print a Swift `guard else { … }` block. + public mutating func printGuardBlock( + _ condition: Any, + function: String = #function, + file: String = #fileID, + line: UInt = #line, + body: (inout CodePrinter) throws -> Void + ) rethrows { + try printBraceBlock("guard \(condition) else", function: function, file: file, line: line, body: body) + } + public mutating func printParts( _ parts: String..., terminator: PrinterTerminator = .newLine, diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift index 21063d3dc..71c30cb8a 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift @@ -602,7 +602,7 @@ extension JNISwift2JavaGenerator { printer.print("#if _pointerBitWidth(_32)") for check in int32OverflowChecks { - printer.printBraceBlock("guard \(check) else") { printer in + printer.printGuardBlock(check) { printer in printer.print("environment.throwJavaException(javaException: .integerOverflow)") printer.print(dummyReturn(for: nativeSignature)) } diff --git a/Tests/CodePrintingTests/PrintGuardBlockTests.swift b/Tests/CodePrintingTests/PrintGuardBlockTests.swift new file mode 100644 index 000000000..ae3593434 --- /dev/null +++ b/Tests/CodePrintingTests/PrintGuardBlockTests.swift @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import CodePrinting +import Testing + +@Suite("CodePrinter.printGuardBlock") +struct PrintGuardBlockSuite { + + @Test func emitsGuardElseBlockWithoutCondParens() { + var p = CodePrinter() + p.emitSourceLocations = false + p.printGuardBlock("let x = optional") { inner in + inner.print("return nil") + } + + let out = p.contents + #expect(out.contains("guard let x = optional else {")) + #expect(out.contains(" return nil")) + #expect(out.contains("}")) + #expect(!out.contains("guard (let x = optional) else")) + } + + @Test func nestsAndIndentsBody() { + var p = CodePrinter() + p.emitSourceLocations = false + p.printBraceBlock("func foo()") { fn in + fn.printGuardBlock("let v = maybe") { g in + g.print("return") + } + } + + let lines = p.contents.split(separator: "\n", omittingEmptySubsequences: false) + let returnLine = lines.first { $0.contains("return") } + #expect( + returnLine?.hasPrefix(" ") == true, + "guard body should be indented two levels under the enclosing func" + ) + } +} From 3c55373a524add5f80723bd81ddf528d0dc87521 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Sat, 20 Jun 2026 18:23:02 +0900 Subject: [PATCH 2/2] Delete Tests/CodePrintingTests/PrintGuardBlockTests.swift --- .../PrintGuardBlockTests.swift | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 Tests/CodePrintingTests/PrintGuardBlockTests.swift diff --git a/Tests/CodePrintingTests/PrintGuardBlockTests.swift b/Tests/CodePrintingTests/PrintGuardBlockTests.swift deleted file mode 100644 index ae3593434..000000000 --- a/Tests/CodePrintingTests/PrintGuardBlockTests.swift +++ /dev/null @@ -1,51 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2026 Apple Inc. and the Swift.org project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of Swift.org project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -import CodePrinting -import Testing - -@Suite("CodePrinter.printGuardBlock") -struct PrintGuardBlockSuite { - - @Test func emitsGuardElseBlockWithoutCondParens() { - var p = CodePrinter() - p.emitSourceLocations = false - p.printGuardBlock("let x = optional") { inner in - inner.print("return nil") - } - - let out = p.contents - #expect(out.contains("guard let x = optional else {")) - #expect(out.contains(" return nil")) - #expect(out.contains("}")) - #expect(!out.contains("guard (let x = optional) else")) - } - - @Test func nestsAndIndentsBody() { - var p = CodePrinter() - p.emitSourceLocations = false - p.printBraceBlock("func foo()") { fn in - fn.printGuardBlock("let v = maybe") { g in - g.print("return") - } - } - - let lines = p.contents.split(separator: "\n", omittingEmptySubsequences: false) - let returnLine = lines.first { $0.contains("return") } - #expect( - returnLine?.hasPrefix(" ") == true, - "guard body should be indented two levels under the enclosing func" - ) - } -}