[SwiftExtract] Preserve source file path for deferred (out-of-order) extensions #924
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 480 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.369
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #924"
general
weight: 0.35
ktoso
Problem
jextract's symbol table builder processes source files in a single pass, inwhatever order
FileManagerenumerates the input directory. If anextension Outer.Inner { ... }is visited before the file that declaresOuter(orInner), its extended type can't be resolved yet, so it's queuedin
unresolvedExtensionsand retried infinalize().When
finalize()resolves a deferred extension, it currently hardcodes thesource file path as
"FIXME_MISSING_FILEPATH.swift"(
SwiftParsedModuleSymbolTableBuilder.swift, with a// FIXMEcommentalready marking this).
Because the JNI thunk printer groups generated thunks by source file
(
writeSwiftThunkSources), everything with this fake path lands in a singleFIXME_MISSING_FILEPATH+SwiftJava.swift. The JExtractSwiftPlugin onlydeclares the real per-source-file
+SwiftJava.swiftoutputs to SwiftPM, sothis file is never compiled. The generated Java bindings are correct (the
Java class exists with all its methods, including
nativedeclarations), butthe corresponding Swift/JNI implementations are silently missing from the
built library — this fails at runtime with
UnsatisfiedLinkError/"No implementation found for ..." instead of at build time.
This is order-dependent, so it's easy to hit by accident: on a case-insensitive
APFS volume,
FileManager's directory enumeration order is filename-hashbased, not alphabetical, so whether an extension's file is read before or
after its extended type's declaring file depends on unrelated filenames
elsewhere in the module. For example,
extension Outer.Inner { struct X {} }in
Feature.swiftandenum Outer { enum Inner {} }inCore.swiftworkfine as long as
Core.swifthappens to be enumerated first — flip thatorder (as can happen simply by adding or renaming unrelated files in the
same directory) and
X's JNI thunks silently disappear from the build.Fix
1.
SwiftParsedModuleSymbolTableBuilder.unresolvedExtensionsnow stores thesourceFilePathalongside each deferredExtensionDeclSyntax, andfinalize()passes that real path through totryHandle(extension:sourceFilePath:)instead of the placeholder string. Deferred extensions are now attributed
to their actual source file like any other declaration, so their JNI thunks
land in the correct, plugin-declared output file and actually get compiled.
2.
collectAllFilesnow sorts the files gathered from each input path beforereturning them. Enumeration order was previously whatever the filesystem
handed back (non-deterministic across machines/filesystems for the reasons
above); sorting makes extraction output reproducible. This alone doesn't
fix the bug (many still-valid orderings would still defer extensions
declared in files before their type's declaring file), but it removes
filesystem/OS as a source of flakiness on top of fix (1).
Testing
Added
deferredExtensionKeepsSourceFilePathtoTests/SwiftExtractTests/SwiftSymbolTableTests.swift: it feeds the symboltable an extension file before the file declaring the extended type (mirroring
the real bug), then asserts the nested type's
sourceFilePathmatches theextension's actual file rather than the fixed fallback.
Ran the full
SwiftExtractTestssuite (swift test --filter SwiftExtractTests):91 tests, 11 suites, all passing.
Verified end-to-end against a real multi-target Swift package with a
JExtractSwiftPlugin-driven Android build: before the fix,HomeFeed'sJNI thunks (and everything else in the affected files) were generated into
FIXME_MISSING_FILEPATH+SwiftJava.swiftand silently dropped from the build;after the fix they're written to their real per-file output and the resulting
.soexports the expected JNI symbols.