Skip to content

Refuse coercing another type into a private record or variant - #9125

Merged
cknitt merged 1 commit into
masterfrom
refuse-private-record-coercion
Oct 29, 2026
Merged

cknitt merged 1 commit into
masterfrom
refuse-private-record-coercion

Conversation

@cknitt

@cknitt cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member

Fixes #9124.

A private type's values can only be built in the module that defines it, but structural coercion built them anywhere. Each of these compiled before:

module Blob: { type t = private {size: float} } = { type t = {size: float} }
type plain = {size: float}
let fake = (p: plain) => (p :> Blob.t)            // a plain record
let forged = (Fake.make(1.) :> Blob.t)            // another module's private record

module Color: { type t = private Red | Green } = { type t = Red | Green }
let c1 = (Red: plain :> Color.t)                  // a plain variant
let c2 = (#Red: [#Red] :> Color.t)                // a polymorphic variant
let i = ("a" :> Id.t)                             // a string, into a private @unboxed variant

subtype_rec now refuses a coercion whose target's definition is a private record or variant, unless the source is that same type. One guard in front of the structural branches covers all routes. The new error says:

Blob.t is private: its values can only be built in the module that defines it, so another type cannot be coerced to it.

Coercing out of a private type is unchanged ((b :> plainBlob), (c :> string)).

This is a prerequisite for @instanceOf on private records (#9058), where a forged value would be dispatched as an instance of a class it isn't. It also means the upcast between two private records, e.g. (file :> blob), is refused until the RFC's @extends declares that relationship.

New tests:

  • super_errors/fixtures/coercion_into_private_*.res: one per route above.
  • RecordCoercion.res and VariantCoercion.res: coercions out of a private record and variant.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Changelog

💥 Breaking Change

  • Coercing another type into a private record or variant is an error: only the module defining a private type can build its values. Coercing out of a private type still works.

💥 A changelog entry is a breaking change, but no commit says what it breaks (Breaking:).

Semantic Diff

1 commit · Full page

✏️ Refuse coercing another type into a private record or variant

052d3cc · cknitt + Claude · Oct 29, 2026, 10:11 UTC · edited

Why

A private type's values can only be built in the module that defines it, but structural coercion built them anywhere: a plain record, another module's private record with the same fields, a variant, a polymorphic variant or a string coerced into a private record or variant (#9124).

subtype_rec refuses a coercion whose target's definition is a private record or variant unless the source is that same type. Coercing out of a private type is unchanged.

Typing modified (coercion into a private type)

module Blob: { type t = private {size: float} } = { type t = {size: float} }
type plain = {size: float}
let fake = (p: plain) => (p :> Blob.t)

rule · Before: a record coerces into a record with compatible fields, · After: a record or variant coerces into a private record or variant

BeforeAfter
$$\begin{array}{l}\color{red}{\mathrm{private}\ \mathrm{or}\ \mathrm{not}}\end{array}$$ $$\begin{array}{l}\color{green}{\mathrm{only}\ \mathrm{from}\ \mathrm{that}\ \mathrm{same}\ \mathrm{type}}\end{array}$$

Diagnostic added (coercion into a private type)

let fake = (p: plain) => (p :> Blob.t)

diagnostic

-none
+Type plain is not a subtype of Blob.t
+Blob.t is private: its values can only be built in the module that
+defines it, so another type cannot be coerced to it.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.48%. Comparing base (799a82f) to head (052d3cc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9125   +/-   ##
=======================================
  Coverage   81.48%   81.48%           
=======================================
  Files         460      460           
  Lines       64728    64740   +12     
=======================================
+ Hits        52744    52756   +12     
  Misses      11984    11984           
Files with missing lines Coverage Δ
compiler/ml/ctype.ml 67.52% <100.00%> (+0.14%) ⬆️
compiler/ml/printtyp.ml 71.94% <100.00%> (+0.05%) ⬆️
🚀 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.

@cknitt

cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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-10-29T13:56:39.148822Z 052d3cc 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.

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 052d3cc

@github-actions

Copy link
Copy Markdown

Why:
    A private type's values can only be built in the module that defines
    it, but structural coercion built them anywhere: a plain record, another
    module's private record with the same fields, a variant, a polymorphic
    variant or a string coerced into a private record or variant (#9124).

`subtype_rec` refuses a coercion whose target's definition is a private
record or variant unless the source is that same type. Coercing out of a
private type is unchanged.

Typing modified (coercion into a private type):
Example:
    module Blob: { type t = private {size: float} } = { type t = {size: float} }
    type plain = {size: float}
    let fake = (p: plain) => (p :> Blob.t)
Before (rule): a record coerces into a record with compatible fields,
    private or not
After (rule): a record or variant coerces into a private record or variant
    only from that same type

Diagnostic added (coercion into a private type):
Example:
    let fake = (p: plain) => (p :> Blob.t)
Before (diagnostic): none
After (diagnostic):
    Type plain is not a subtype of Blob.t
    Blob.t is private: its values can only be built in the module that
    defines it, so another type cannot be coerced to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t
Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the refuse-private-record-coercion branch from 3f75b5f to 052d3cc Compare October 29, 2026 13:20
@cknitt

cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 052d3cca9e

ℹ️ 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 requested a review from cristianoc October 29, 2026 13:57
@cknitt
cknitt marked this pull request as ready for review October 29, 2026 13:57

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the changes.

@cknitt
cknitt merged commit 6a68ee2 into master Oct 29, 2026
26 checks passed
@cknitt
cknitt deleted the refuse-private-record-coercion branch October 29, 2026 17:37
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.

Record coercion can construct a private record from outside its module

2 participants