Skip to content

Fix Embedded Swift support for Entity decoding#24

Merged
colemancda merged 4 commits into
masterfrom
feature/embedded
Jul 18, 2026
Merged

Fix Embedded Swift support for Entity decoding#24
colemancda merged 4 commits into
masterfrom
feature/embedded

Conversation

@colemancda

Copy link
Copy Markdown
Member

Summary

Adds proper Embedded Swift support for the generic Entity-based decode path. The library target already compiled standalone under Embedded and the swift-wasm CI job passed — but neither ever instantiated the generic Entity.init(from:)/ModelData.decode path for a concrete conforming type (those types live in the test target, which the embedded job doesn't build). So the bug was latent: the moment a downstream package declares a concrete Entity and calls init(from:) under Embedded, it fails with:

error: cannot use a value of protocol type 'any Error' in embedded Swift [#EmbeddedRestrictions]

Root cause

Two places throw a concrete CoreModelError from a function declared as untyped throws (sugar for throws(any Error)). Under Embedded Swift, boxing a concrete error into any Error is disallowed, so the compiler rejects it once the path is specialized for a real type:

  1. Entity.init(from:) — the protocol requirement itself is untyped throws, forcing a boxing thunk in every throwing witness's table entry.
  2. ModelData.decode/decodeRelationship — plain throws, but under Embedded they call factories returning concrete CoreModelError.

Error.swift already returned the concrete type conditionally under Embedded — it just never propagated that into the throws clauses of its callers.

Fix

Introduce one typealias — ModelDataDecodingError — resolving to CoreModelError under Embedded and any Swift.Error otherwise, and use it as the throws(...) type on the requirement and the four generic decode helpers. On non-Embedded platforms throws(ModelDataDecodingError) is byte-for-byte identical to the old plain throws: zero behavior change, no API break for existing conformers on Darwin/Linux/Windows.

One spot needed more than a signature swap: the objectIDs.map { throw … } in decodeRelationship(_:forKey:) for to-many goes through the rethrows overload, which erases the thrown type back to any Error. It's rewritten as an explicit loop so the typed error propagates.

encode() is left untyped — every witness is non-throwing today, so there's nothing to box.

Regression coverage

Adds an always-built, Embedded-gated concrete Entity fixture (EmbeddedFixture.swift) that forces the compiler to specialize the generic decode path — the exact instantiation CI was missing. Without it, this regression can silently reappear.

Out of scope

The other Embedded caveat — ModelStorage's generic fetch/insert/delete/count and ViewContext — is a separate upstream compiler bug (swiftlang/swift#78811), unrelated to error-boxing, and is not touched here.

Verification

  • swift build --target CoreModel
  • swift build --target CoreModel --swift-sdk swift-6.3.3-RELEASE_wasm-embedded ✅ (fails without the loop fix)
  • swift build (full package) ✅
  • swift test ✅ 22 tests, 7 suites

Resolves to the concrete CoreModelError under Embedded Swift (where
any Error existentials are disallowed) and to any Swift.Error
otherwise, so callers can adopt throws(ModelDataDecodingError) with no
behavior change off-Embedded.
The generic decode/decodeRelationship helpers threw untyped (any Error),
forcing a boxing thunk the compiler rejects under Embedded Swift once
specialized for a concrete type. Adopt the typed throws clause and
replace the throwing objectIDs.map with an explicit loop, since the
rethrows overload erases the thrown type back to any Error.
The untyped throws requirement forced every throwing witness to
synthesize an any Error boxing thunk in the witness table, which
Embedded Swift rejects. encode() is left untyped since no witness
throws.
An always-built (Embedded-gated) concrete Entity conformance that
specializes the generic init(from:)/decode path the wasm-embedded CI job
never reaches through the test target, guarding against error-boxing
regressions.
@colemancda colemancda changed the title Proper Embedded Swift support for Entity decode path Fix Embedded Swift support for Entity decoding Jul 18, 2026
@github-code-quality

Copy link
Copy Markdown

Code Coverage Overview

Languages: Swift

Swift / code-coverage/llvm-cov

The overall coverage remains at 96%, unchanged from the branch.


Code Coverage is in Public Preview. Learn more and provide us with your feedback.

@colemancda
colemancda merged commit 960636d into master Jul 18, 2026
32 checks passed
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.

1 participant