← Back to Pull requests

JNI: fix existential result boxing in Swift 6 language mode #930

Merged
Om-singhaI merged 5 files (106 lines) on Oct 15, 2026 04:08 UTC
View on GitHub
Om-singhaI Om-singhaI commented on Oct 06, 2026 00:13 UTC
Author

When a JNI function returns any P or some P, the thunk boxes the result with this:

#if hasFeature(ImplicitOpenExistentials)
let resultBoxed$: (Int64, Int64) = {
  let value = resultExistential$
  let pointer = UnsafeMutablePointer<type(of: value)>.allocate(capacity: 1)
  ...

type(of: value) is a value, not a type, so it can't be a generic argument. Swift 5 mode never takes that branch, and the JNI sample app is the only package in CI that extracts any P or some P returns (ReturnProtocol.swift and others). It builds with .swiftLanguageMode(.v5), so CI doesn't compile it. In Swift 6 mode, or with the ImplicitOpenExistentials upcoming feature on, the generated file fails to build:

error: '>' is not a postfix unary operator
error: generic parameter 'Pointee' could not be inferred

This is the Swift 6 failure I mentioned at the end of #925, and one of the cases behind #472.

The resultBox$<T> helper now sits above the #if, and the feature branch calls it directly. With implicit opening that call opens the existential, so T is the concrete type, the same as _openExistential gives in Swift 5. The existential property setter already does it this way. Swift 5 mode compiles the same code as before.

I added returnsExistentialProtocol_boxing_swift to JNIProtocolTests. It fails without the source change, and JNIProtocolTests and JNIAsyncTests pass with it.

I also typechecked the generated thunks with swiftc (Swift 6.4) for the JNI sample's ReturnProtocol.swift, plus a computed property and a class method returning any P. On main they fail with the errors above in Swift 6 mode and with -enable-upcoming-feature ImplicitOpenExistentials. With this change they are clean in both of those and in Swift 5 mode. A small runtime check confirmed that in Swift 6 the helper gets the concrete type, not any P.

Diff: 106 lines (+96 / -10) across 5 files Reviewer: ktoso Review duration: 13194m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.37) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 106 lines diff across 5 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.94 · 2 substantive comment(s)
0.94
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.280
Real PR Conversation Timeline
4 items
ktoso ktoso commented on Oct 06, 2026 00:25 UTC
Reviewer

Can you please add a runtime test exercising this path so we're confident it functions
Runtime tests are located inside the samples basically writing the s swift and Java sides as if you were to use this normally

Om-singhaI Om-singhaI commented on Oct 06, 2026 01:22 UTC
Author

I turned on the ImplicitOpenExistentials upcoming feature for MySwiftLibrary in the JNI sample, so ReturnProtocolTest and the other protocol tests now run the hasFeature(ImplicitOpenExistentials) code at runtime. I also added a GreeterFactory class with tests for a method and a computed property returning any Greeter and a method returning some Greeter, and they check that the returned value keeps its concrete Swift type. With the generator change reverted, the sample fails to build with the two errors from the description. One tradeoff is that the sample no longer runs the _openExistential path, so if you would rather keep both covered I can move the new code into a separate target.

ktoso ktoso commented on Oct 15, 2026 04:08 UTC
Contributor

Looks good, thank you!

Reviewers
Assignees
Om-singhaI Om-singhaI
Labels
risk: low confidence: 87.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 36.8% (MEDIUM) · Higher = larger blast radius
36.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 87.2% · Lower = rushed or shallow review
87.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 4.7% · Safe to merge
4.7%