Skip to content

Explain why a variant's inline record cannot be coerced - #9205

Merged
cknitt merged 1 commit into
masterfrom
drift-9185a-inline-record-mutable-reason
Nov 4, 2026
Merged

cknitt merged 1 commit into
masterfrom
drift-9185a-inline-record-mutable-reason

Conversation

@cknitt

@cknitt cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member

Part of #9185 (first item).

Since #9086, coercing a variant whose constructor has an inline record into one whose inline record field is mutable is rejected, correctly, but the error says only Type 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_fields detects (a missing field, an optional mismatch, a different @as name) were already rejected in released versions, also without a reason. The variant branch of Ctype.subtype_rec computed those violations and then threw them away, reporting the failure with no context.

Changes

  • The variant branch now keeps the violations of the first constructor whose inline record fields fail and reports them with the existing Record_fields_mismatch context. The inline record types are named v1.A and v2.A, as the compiler prints inline record types elsewhere. Which coercions are accepted does not change.
  • The messages for a missing field and for an @as mismatch named the two records the wrong way round ("z is missing in record y, but present in record x" when z is missing in x). 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}) into A({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_errors fixtures: 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 read M.v1.A), and variant_coercion_inline_record_second_constructor (the failing constructor is not the first). Updated snapshots: record_coercion_missing_field and the three record_coercion_as_payload_mismatch_*, for the corrected record order.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • A variant coercion that fails on a missing, optional or @as-renamed inline record field now says which field is wrong, and record coercion errors no longer swap the two record names for a missing field or a different @as name.

Semantic Diff

1 commit · Full page

Explain why a variant's inline record cannot be coerced

b07ae0f · cknitt + Claude · Nov 03, 2026, 18:34 UTC

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)

type v1 = A({x: float})
type v2 = A({mutable x: float})
let toV2 = (a: v1) => (a :> v2)

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)

type x = {x: int}
type y = {x: int, z: int}
let toY = (a: x) => (a :> y)

diagnostic

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

Review

  • Why only the first failing constructor? The error has one context; the first constructor whose fields fail is reported.
  • A different field type in an inline record still gives no reason; acceptance there is unchanged.

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.74%. Comparing base (ac73578) to head (b07ae0f).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
compiler/ml/ctype.ml 86.36% 3 Missing ⚠️
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     
Files with missing lines Coverage Δ
compiler/ml/printtyp.ml 72.77% <100.00%> (+0.30%) ⬆️
compiler/ml/ctype.ml 70.00% <86.36%> (+0.16%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9205

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9205

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9205

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9205

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9205

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9205

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9205

commit: b07ae0f

@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T19:50:12.236952Z b07ae0f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 31651b0069

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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>
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt force-pushed the drift-9185a-inline-record-mutable-reason branch from 31651b0 to b07ae0f Compare November 3, 2026 19:48
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: b07ae0fa88

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

@cknitt
cknitt marked this pull request as ready for review November 4, 2026 05:49
@cknitt
cknitt requested a review from cristianoc November 4, 2026 05:49
@cknitt
cknitt merged commit f1a0476 into master Nov 4, 2026
26 checks passed
@cknitt
cknitt deleted the drift-9185a-inline-record-mutable-reason branch November 4, 2026 08:25
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.

2 participants