Repository navigation
Explain why a variant's inline record cannot be coerced - #9205
Conversation
Changelog💅 Polish
Semantic Diff1 commit · Full page Explain why a variant's inline record cannot be coercedb07ae0f · cknitt + Claude · Nov 03, 2026, 18:34 UTC
Diagnostic modified (inline record coercion) type v1 = A({x: float})
type v2 = A({mutable x: float})
let toV2 = (a: v1) => (a :> v2)
Type v1 is not a subtype of v2
+
+The record v1.A cannot be coerced to the record v2.A because:
+- The field x is mutable in record v2.A, but not in record v1.A. A coercion can't make a field writable.Diagnostic modified (missing record field) type x = {x: int}
type y = {x: int, z: int}
let toY = (a: x) => (a :> y)
-- The field z is missing in record y, but present in record x
+- The field z is missing in record x, but present in record yReview
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9205 +/- ##
==========================================
+ Coverage 81.73% 81.74% +0.01%
==========================================
Files 458 458
Lines 65292 65301 +9
==========================================
+ Hits 53364 53380 +16
+ Misses 11928 11921 -7
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9205 |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Why:
A coercion that fails on an inline record field said only that one
variant is not a subtype of the other; since #9086 that includes an
immutable field coerced to a mutable one. Plain records name the
field. The record messages for a missing field and an @as mismatch
also named the two records the wrong way round.
Diagnostic modified (inline record coercion):
Example:
type v1 = A({x: float})
type v2 = A({mutable x: float})
let toV2 = (a: v1) => (a :> v2)
Before (diagnostic):
Type v1 is not a subtype of v2
After (diagnostic):
Type v1 is not a subtype of v2
The record v1.A cannot be coerced to the record v2.A because:
- The field x is mutable in record v2.A, but not in record v1.A. A coercion can't make a field writable.
Diagnostic modified (missing record field):
Example:
type x = {x: int}
type y = {x: int, z: int}
let toY = (a: x) => (a :> y)
Before (diagnostic):
- The field z is missing in record y, but present in record x
After (diagnostic):
- The field z is missing in record x, but present in record y
Review: Why only the first failing constructor? The error has one context; the first constructor whose fields fail is reported.
Review: A different field type in an inline record still gives no reason; acceptance there is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb
Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
31651b0 to
b07ae0f
Compare
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Part of #9185 (first item).
Since #9086, coercing a variant whose constructor has an inline record into one whose inline record field is
mutableis rejected, correctly, but the error says onlyType v1 is not a subtype of v2. A plain record in the same situation explains that the field is mutable in the target and a coercion can't make a field writable. The missing reason for the mutable case is an unreleased regression from #9086, since every release accepted that coercion.The other inline record failures that
Record_coercion.check_record_fieldsdetects (a missing field, an optional mismatch, a different@asname) were already rejected in released versions, also without a reason. The variant branch ofCtype.subtype_reccomputed those violations and then threw them away, reporting the failure with no context.Changes
Record_fields_mismatchcontext. The inline record types are namedv1.Aandv2.A, as the compiler prints inline record types elsewhere. Which coercions are accepted does not change.@asmismatch named the two records the wrong way round ("z is missing in record y, but present in record x" whenzis missing inx). This affected plain records too, and the inline record path would have shown the same wrong text, so both messages now name the records in the right order.A different field type inside an inline record (
A({x: float})intoA({x: int})) still gives no reason. The inline record path only checks whether the field subtyping produced extra constraints and rejects if so, rather than passing them on as plain records do. Giving the inner trace would mean changing that, and with it which coercions are accepted, so it is left out of this PR.The changelog fragment covers the cases that were already rejected in released versions.
Tests
New
build_tests/super_errorsfixtures:variant_coercion_inline_record_immutable_to_mutable,variant_coercion_inline_record_missing_field,variant_coercion_inline_record_optional_mismatch,variant_coercion_inline_record_as_mismatch(types in a module, so the names readM.v1.A), andvariant_coercion_inline_record_second_constructor(the failing constructor is not the first). Updated snapshots:record_coercion_missing_fieldand the threerecord_coercion_as_payload_mismatch_*, for the corrected record order.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb