Skip to content

Preserve @MainActor when bridging between Skip Lite and Skip Fuse - #783

Open
dfabulich wants to merge 3 commits into
skiptools:mainfrom
dfabulich:bridge-mainactor-to-swift
Open

dfabulich wants to merge 3 commits into
skiptools:mainfrom
dfabulich:bridge-mainactor-to-swift

Conversation

@dfabulich

Copy link
Copy Markdown
Member

Preserve @mainactor when bridging between Skip Lite and Skip Fuse

Fixes #657.

The Swift generated for a transpiled module with bridging: true dropped @MainActor from 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 @MainActor class reported "sending 'self.counter' risks causing data races".

Transpiled → native (KotlinBridgeToSwiftVisitor)

  • Emit @MainActor on 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.
  • Keep generated ==, 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.
  • Preserve an explicit nonisolated on types and protocols, including a protocol that opts out of an inherited @MainActor.
  • Don't add an inferred @MainActor to unannotated overrides. They inherit the isolation of the declaration they override, which may be nonisolated.
  • Codebase info adds the .mainActor flag 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 @MainActor protocol, 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.
  • Treat main actor-isolated types as known Sendable for async results, which avoids a redundant 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.

  • Match extension members while ignoring @MainActor.
  • Generate main actor-safe JNI entry points:
    • Take isolation from the requirement (including requirements inherited from parent protocols), since calls go through any P, and from the extension otherwise.
    • Wrap synchronous calls in assumeMainActorUnchecked.
    • Run async calls in a Task { @MainActor in }.
    • Box the non-Sendable protocol existential.
    • Isolate the setter of a writable requirement by the requirement's isolation.
  • Keep the Kotlin callback for async requirements that have defaults, so Swift can still call Kotlin overrides.
  • Ignore isolation when merging protocol members with their extension implementations. Before this, an isolated requirement plus a nonisolated default produced duplicate Kotlin declarations.

Testing

  • Added tests to BridgeToSwiftTests, BridgeToKotlinTests and BridgeSendabilityTests. All 896 SkipSyntaxTests pass.
  • Built a Skip Fuse repro app for Android against this branch with SKIPLOCAL. It has a transpiled bridged module and a native bridged module, and it covers @MainActor classes, 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)

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.
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.

#SendingRisksDataRace errors calling async transpiled bridged @MainActor methods in a Task

1 participant