Repository navigation
Conversation
Fixes skiptools#657. The Swift generated for a transpiled module with `bridging: true` dropped `@MainActor` from types and members, so Skip Fuse callers saw non-isolated, non-Sendable types. Swift 6 code that compiles on iOS then failed on Android, e.g. `Task { await counter.increment() }` reported "sending 'self.counter' risks causing data races". KotlinBridgeToSwiftVisitor now emits `@MainActor` on generated classes, structs, enums, protocols, and protocol bridge implementations that are main actor-isolated (explicitly or by inheritance), and on main actor-isolated members, globals, literal constants, and extension members of unbridged types. The JNI plumbing stays nonisolated, as before. Generated `==`, `hash(into:)`, and `<` in main actor-isolated types are nonisolated, since they only use the Java peer and must satisfy nonisolated protocol requirements. Likewise, a main actor-isolated protocol's bridge implementation uses nonisolated witnesses for requirements inherited from protocols that are not main actor-isolated. An explicit `nonisolated` on a type is preserved. Codebase info adds the `.mainActor` API flag to every member of a main actor-isolated type, which made the function info lookups fail for those members. For a `@MainActor` protocol, that silently dropped function requirements from the generated protocol. The lookups now ignore that flag. Main actor-isolated types are now treated as known Sendable for async results, avoiding a redundant `nonisolated(unsafe)` warning.
When bridging native Swift to Kotlin, protocol extension members were matched against codebase info with a signature comparison that included the inferred `@MainActor` flag. For main actor-isolated protocols the match always failed, so Kotlin saw extension defaults as abstract requirements and couldn't reach the Swift implementations. Match members ignoring `@MainActor`, and generate main actor-safe JNI entry points for them: resolve isolation from codebase info (using the requirement's isolation, including requirements inherited from parent protocols, since calls go through `any P`), wrap synchronous calls in `assumeMainActorUnchecked`, run async calls in a main actor task, and box the non-Sendable protocol existential. Keep the Kotlin callback for async requirements that have defaults so Swift can still call Kotlin overrides. Also ignore isolation when merging protocol members with their extension implementations, so an isolated requirement and a nonisolated default no longer produce duplicate Kotlin declarations.
Don't add an inferred @mainactor to unannotated overrides, which inherit the isolation of the declaration they override (it may be nonisolated). Match class members by static-ness as well as by name and signature, so a @mainactor static function doesn't lend its isolation to an instance function of the same name. Keep `nonisolated` on bridged protocols that opt out of an inherited @mainactor. Isolate the Kotlin-to-Swift setter of a writable protocol requirement by the requirement's isolation rather than the extension's.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preserve @mainactor when bridging between Skip Lite and Skip Fuse
Fixes #657.
The Swift generated for a transpiled module with
bridging: truedropped@MainActorfrom types and members. Skip Fuse callers therefore saw non-isolated, non-Sendable types, so Swift 6 code that compiles on iOS failed on Android. For example,Task { await counter.increment() }on a@MainActorclass reported "sending 'self.counter' risks causing data races".Transpiled → native (KotlinBridgeToSwiftVisitor)
@MainActoron generated classes, structs, enums, protocols, and protocol bridge implementations that are main actor-isolated, whether explicitly or by inheritance. Also emit it on main actor-isolated members, globals, literal constants, and extension members of unbridged types. The JNI plumbing stays nonisolated.==,hash(into:)and<nonisolated. They only touch the Java peer and must satisfy nonisolated protocol requirements. Likewise, a bridge implementation uses nonisolated witnesses for requirements inherited from protocols that aren't main actor-isolated.nonisolatedon types and protocols, including a protocol that opts out of an inherited@MainActor.@MainActorto unannotated overrides. They inherit the isolation of the declaration they override, which may be nonisolated..mainActorflag to every member of a main actor-isolated type, but a declared function type has it only when the member is explicitly annotated. Function lookups compared the full signatures, so they failed for those members. For a@MainActorprotocol, that silently dropped function requirements from the generated protocol. Lookups now ignore that flag, and they also match on static-ness, so a static function and an instance function with the same name don't share isolation.nonisolated(unsafe)warning.Native → transpiled (KotlinBridgeToKotlinVisitor)
The same signature comparison broke protocol extension defaults in native modules with
bridging: true. For main actor-isolated protocols, extension members never matched codebase info. Kotlin therefore saw the defaults as abstract requirements and couldn't reach the Swift implementations.@MainActor.any P, and from the extension otherwise.assumeMainActorUnchecked.Task { @MainActor in }.Testing
BridgeToSwiftTests,BridgeToKotlinTestsandBridgeSendabilityTests. All 896SkipSyntaxTestspass.SKIPLOCAL. It has a transpiled bridged module and a native bridged module, and it covers@MainActorclasses, protocols, protocol extensions, async members and overrides. It builds with no data-race errors and runs on an Android 14 emulator.Known limitations (pre-existing, filed separately)