Skip to content

Suppress warning for "inferred to have type '()',"#559

Merged
ktoso merged 1 commit intoswiftlang:mainfrom
sidepelican:reduce_warning2
Feb 10, 2026
Merged

Suppress warning for "inferred to have type '()',"#559
ktoso merged 1 commit intoswiftlang:mainfrom
sidepelican:reduce_warning2

Conversation

@sidepelican
Copy link
Contributor

This is a minor improvement to silence a compiler warning in the generated code like below.

/Users/iceman/github/swift-java/Samples/SwiftJavaExtractJNISampleApp/.build/plugins/outputs/swiftjavaextractjnisampleapp/MySwiftLibrary/destination/JExtractSwiftPlugin/Sources/MySwiftLibraryModule+SwiftJava.swift:864:13: warning: constant 'swiftResult$' inferred to have type '()', which may be unexpected
 862 |       } // printTaskBody(printer:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:1151
 863 |       do {
 864 |         let swiftResult$ = try await MySwiftLibrary.asyncThrows()
     |             |- warning: constant 'swiftResult$' inferred to have type '()', which may be unexpected
     |             `- note: add an explicit type annotation to silence this warning
 865 |         environment = try! JavaVirtualMachine.shared().environment()
 866 |         _ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])

Since the generator already checks for .isVoid at this location, I have slightly adjusted the logic to avoid assigning the result.

@sidepelican sidepelican requested a review from ktoso as a code owner February 10, 2026 01:00
printer.print("_ = environment.interface.CallBooleanMethodA(environment, globalFuture, \(completeMethodID), [jvalue(l: nil)])")
} else {
printer.print("let swiftResult$ = \(tryAwaitString) \(placeholderWithoutTry)")
printer.print("environment = try! JavaVirtualMachine.shared().environment()")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to move the environment initialization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference is whether we write the if statement twice or not.
I was okay with a single line of duplication, but we could also write the if statement twice if preferred.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, you can keep the printer.print("environment = try! JavaVirtualMachine.shared().environment()") before the if entirely so we don't duplicate the string in both branches

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better not to change the timing of fetching environment as it might span across a suspention point (await).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... let's go with this, though arguably before might be safer tbh...

@ktoso ktoso merged commit 07f959e into swiftlang:main Feb 10, 2026
49 checks passed
@sidepelican sidepelican deleted the reduce_warning2 branch February 10, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants