Address Swift 6 concurrency-safety warnings #8

Closed
opened 2025-10-14 15:33:23 -06:00 by navan · 0 comments
Owner

Originally created by @navanchauhan on 9/15/2025

Summary

Swift 6 stricter concurrency checks now flag all of our precomputed tables and cached values in SwiftChessCore, so the engine emits dozens of #MutableGlobalVariable warnings during swift build. We need to harden the module for Swift 6 compatibility.

Affected Diagnostics

  • Global lookup tables in Sources/SwiftChessCore/Tables.swift (_whitePawnAttackTable, _blackPawnAttackTable, _kingAttackTable, _knightAttackTable, _whiteBishopAttackTable, _blackBishopAttackTable, _whiteRookAttackTable, _blackRookAttackTable, _whiteQueenAttackTable, _blackQueenAttackTable, _whiteKingRayTable, _blackKingRayTable, _lineTable).
  • Square.all in Sources/SwiftChessCore/Square.swift:226.
  • Variant caches _standard / _upsideDown in Sources/SwiftChessCore/Variant.swift.
  • Repeated note that Bitboard and other supporting types lack Sendable.

Goals

  1. Eliminate all Swift 6 concurrency warnings in the core target.
  2. Preserve the existing public API and performance characteristics of the lookup tables.
  3. Ensure the new Sendable/actor annotations keep the module portable across macOS, Linux, and Windows.

Proposed Plan

  1. Audit Sendability Requirements
    • Catalog types referenced by the global tables (Bitboard, Variant, etc.).
    • Add Sendable or @unchecked Sendable conformances where the representation is value-typed (e.g. Bitboard, Square, Variant).
  2. Refactor Lookup Tables
    • Move each global let into a dedicated struct Tables namespace as static let.
    • Wrap initialization inside @usableFromInline static let computed via @Sendable closures.
    • Alternatively mark the namespace as @MainActor if we cannot make the stored type Sendable.
    • Verify no mutable state leaks from these tables.
  3. Square Cache
    • Convert Square.all into a computed property or lazily initialized static let within a Sendable extension.
    • Ensure Square enums conform to Sendable to satisfy compiler checks.
  4. Variant Caches
    • Replace _standard / _upsideDown with computed properties or static let inside a Sendable container.
    • Use @MainActor only if true thread-safety cannot be proven.
  5. Testing & Validation
    • Run swift build and swift test under Swift 5.9 and Swift 6 toolchains (on macOS and Linux CI) to ensure no regressions.
    • Add a CI job that builds with --swift-version 6 to keep the warnings from reappearing.
  6. Documentation
    • Note the concurrency changes in the changelog/README so downstream apps know the engine is Swift 6-ready.

Acceptance Criteria

  • swift build with --swift-version 6 completes without concurrency warnings.
  • All unit tests pass on supported platforms.
  • Documentation reflects the new Swift 6 compatibility.
*Originally created by @navanchauhan on 9/15/2025* ## Summary Swift 6 stricter concurrency checks now flag all of our precomputed tables and cached values in `SwiftChessCore`, so the engine emits dozens of `#MutableGlobalVariable` warnings during `swift build`. We need to harden the module for Swift 6 compatibility. ## Affected Diagnostics - Global lookup tables in `Sources/SwiftChessCore/Tables.swift` (`_whitePawnAttackTable`, `_blackPawnAttackTable`, `_kingAttackTable`, `_knightAttackTable`, `_whiteBishopAttackTable`, `_blackBishopAttackTable`, `_whiteRookAttackTable`, `_blackRookAttackTable`, `_whiteQueenAttackTable`, `_blackQueenAttackTable`, `_whiteKingRayTable`, `_blackKingRayTable`, `_lineTable`). - `Square.all` in `Sources/SwiftChessCore/Square.swift:226`. - Variant caches `_standard` / `_upsideDown` in `Sources/SwiftChessCore/Variant.swift`. - Repeated note that `Bitboard` and other supporting types lack `Sendable`. ## Goals 1. Eliminate all Swift 6 concurrency warnings in the core target. 2. Preserve the existing public API and performance characteristics of the lookup tables. 3. Ensure the new Sendable/actor annotations keep the module portable across macOS, Linux, and Windows. ## Proposed Plan 1. **Audit Sendability Requirements** - Catalog types referenced by the global tables (`Bitboard`, `Variant`, etc.). - Add `Sendable` or `@unchecked Sendable` conformances where the representation is value-typed (e.g. `Bitboard`, `Square`, `Variant`). 2. **Refactor Lookup Tables** - Move each global `let` into a dedicated `struct Tables` namespace as `static let`. - Wrap initialization inside `@usableFromInline static let` computed via `@Sendable` closures. - Alternatively mark the namespace as `@MainActor` if we cannot make the stored type `Sendable`. - Verify no mutable state leaks from these tables. 3. **Square Cache** - Convert `Square.all` into a computed property or lazily initialized `static let` within a `Sendable` extension. - Ensure `Square` enums conform to `Sendable` to satisfy compiler checks. 4. **Variant Caches** - Replace `_standard` / `_upsideDown` with computed properties or `static let` inside a `Sendable` container. - Use `@MainActor` only if true thread-safety cannot be proven. 5. **Testing & Validation** - Run `swift build` and `swift test` under Swift 5.9 and Swift 6 toolchains (on macOS and Linux CI) to ensure no regressions. - Add a CI job that builds with `--swift-version 6` to keep the warnings from reappearing. 6. **Documentation** - Note the concurrency changes in the changelog/README so downstream apps know the engine is Swift 6-ready. ## Acceptance Criteria - `swift build` with `--swift-version 6` completes without concurrency warnings. - All unit tests pass on supported platforms. - Documentation reflects the new Swift 6 compatibility.
navan closed this issue 2025-10-14 15:33:23 -06:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: navan/swift-chess-neo#8
No description provided.