From d79dbc5f81a83414a7e814fcc23e601d4fee8891 Mon Sep 17 00:00:00 2001 From: Aman Date: Sat, 20 Dec 2025 23:32:20 +0530 Subject: [PATCH 1/2] Fixs warning of try await --- .../JNI/JNISwift2JavaGenerator+NativeTranslation.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift index d8b0a1d1b..914e09cd4 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift @@ -1033,7 +1033,15 @@ extension JNISwift2JavaGenerator { ) func printDo(printer: inout CodePrinter) { - printer.print("let swiftResult$ = await \(placeholder)") + // Generate proper 'try await' order: if placeholder already has 'try', remove it and add 'try await', otherwise just add 'try await' + let modifiersAndPlaceholder: String + if placeholder.hasPrefix("try ") { + let placeholderWithoutTry = placeholder.replacingOccurrences(of: "^try ", with: "", options: .regularExpression) + modifiersAndPlaceholder = "try await \(placeholderWithoutTry)" + } else { + modifiersAndPlaceholder = "try await \(placeholder)" + } + printer.print("let swiftResult$ = \(modifiersAndPlaceholder)") printer.print("environment = try! JavaVirtualMachine.shared().environment()") let inner = nativeFunctionSignature.result.conversion.render(&printer, "swiftResult$") if swiftFunctionResultType.isVoid { From 19a81fb3937761b2860c43cfdd04422d3d977a50 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Wed, 7 Jan 2026 21:18:38 +0900 Subject: [PATCH 2/2] correct implementation, verify tests pass --- ...wift2JavaGenerator+NativeTranslation.swift | 24 ++++++++++++------- .../JNI/JNIAsyncTests.swift | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift index 914e09cd4..9ffa000cc 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift @@ -1033,15 +1033,21 @@ extension JNISwift2JavaGenerator { ) func printDo(printer: inout CodePrinter) { - // Generate proper 'try await' order: if placeholder already has 'try', remove it and add 'try await', otherwise just add 'try await' - let modifiersAndPlaceholder: String - if placeholder.hasPrefix("try ") { - let placeholderWithoutTry = placeholder.replacingOccurrences(of: "^try ", with: "", options: .regularExpression) - modifiersAndPlaceholder = "try await \(placeholderWithoutTry)" - } else { - modifiersAndPlaceholder = "try await \(placeholder)" - } - printer.print("let swiftResult$ = \(modifiersAndPlaceholder)") + // Make sure try/await are printed when necessary and avoid duplicate, or wrong-order, keywords (which would cause warnings) + let placeholderWithoutTry = + if placeholder.hasPrefix("try ") { + String(placeholder.dropFirst("try ".count)) + } else { + placeholder + } + + let tryAwaitString: String = + if isThrowing { + "try await" + } else { + "await" + } + printer.print("let swiftResult$ = \(tryAwaitString) \(placeholderWithoutTry)") printer.print("environment = try! JavaVirtualMachine.shared().environment()") let inner = nativeFunctionSignature.result.conversion.render(&printer, "swiftResult$") if swiftFunctionResultType.isVoid { diff --git a/Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift b/Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift index 67fe2e7a4..c600e8f5d 100644 --- a/Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift +++ b/Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift @@ -144,7 +144,7 @@ struct JNIAsyncTests { deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture) } do { - let swiftResult$ = await try SwiftModule.async() + let swiftResult$ = try await SwiftModule.async() environment = try! JavaVirtualMachine.shared().environment() _ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)]) } @@ -164,7 +164,7 @@ struct JNIAsyncTests { deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture) } do { - let swiftResult$ = await try SwiftModule.async() + let swiftResult$ = try await SwiftModule.async() environment = try! JavaVirtualMachine.shared().environment() _ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)]) }