Repository navigation
Conversation
…asses
Why:
Two packages that bind the same JavaScript class each declare their
own type for it, `@instanceOf("Element") type element` in one and in
the other, and `:>` did not get from one to the other: it only follows
the abbreviations a type is declared with. So every binding package
depends on the one package that owns the DOM types, or its users
convert with `Obj.magic`. The class an `@instanceOf` type names is
what its values are at run time; it now decides what they coerce to.
Typing modified (`:>` between class types, `Ctype.subtype_rec`):
Before (rule):
p1 = p2
──────────────────── (Same path)
`p1` <: `p2`
After (rule):
class(p2) ∈ classes(p1)
──────────────────── (Class)
`p1` <: `p2`
class(p): the class that the declaration of p itself states with @instanceof, with its @module
classes(p): class(p), or the class p abbreviates, with its declared superclasses
Diagnostic removed (coercions between declarations of one class):
Example:
module WebAPI = {
@instanceof("Node") type node
@instanceof("Element") type element = private node
@instanceof("HTMLInputElement") type input = private element
}
module ReactDOM = {
@instanceof("Node") type node
@instanceof("Element") type element = private node
}
let a = (el: ReactDOM.element) => (el :> WebAPI.element)
let b = (input: WebAPI.input) => (input :> ReactDOM.node)
let c = (els: array<ReactDOM.element>) => (els :> array<WebAPI.element>)
Before (diagnostic): each line compiled alone
Type ReactDOM.element is not a subtype of WebAPI.element
Type WebAPI.input is not a subtype of ReactDOM.node
Type array<ReactDOM.element> is not a subtype of array<WebAPI.element>
Type ReactDOM.element is not compatible with type WebAPI.element
After (diagnostic): none
After (js):
function a(el) {
return el;
}
Diagnostic modified (a class that is not a superclass):
Example:
let d = (el: ReactDOM.element) => (el :> WebAPI.input)
Before (diagnostic):
Type ReactDOM.element is not a subtype of WebAPI.input
After (diagnostic):
Type ReactDOM.element is not a subtype of WebAPI.input
ReactDOM.element is the class Element, a subclass of Node. WebAPI.input is the class HTMLInputElement. A class type coerces to its own class and to the superclasses it declares.
The fields or contents of the two declarations are not compared: each
says what its module knows about the class. A class is its path and its
module, so a global `Mesh` and the `Mesh` that `"three"` exports stay
unrelated. Where a coercion needs equal types, an invariant parameter as
in `array<_>` or a mutable field, it is checked both ways as before, which
two declarations of one class pass and a subclass does not.
Review: Can `:>` now reach a type it could not before, other than a class type? No: the arm only matches two nullary type constructors whose target states a class with its own `@instanceOf`, so `type mine = private element`, a refinement without an annotation, still accepts no coercion into it.
Review: Do programs that compiled before change? No: the arm only adds coercions, and `:>` produces the value unchanged.
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-6674cba8-4481-7b1d-882a-9a317373d7c1
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Changelog🚀 New Feature
Semantic Diff1 commit · Full page Coerce a class type to every declaration of its class and its superclasses7964eea · jfrolich + Jaap Frolich · Nov 16, 2026, 20:23 UTC
Typing modified (
Diagnostic removed (coercions between declarations of one class) module WebAPI = {
@instanceOf("Node") type node
@instanceOf("Element") type element = private node
@instanceOf("HTMLInputElement") type input = private element
}
module ReactDOM = {
@instanceOf("Node") type node
@instanceOf("Element") type element = private node
}
let a = (el: ReactDOM.element) => (el :> WebAPI.element)
let b = (input: WebAPI.input) => (input :> ReactDOM.node)
let c = (els: array<ReactDOM.element>) => (els :> array<WebAPI.element>)
-Type ReactDOM.element is not a subtype of WebAPI.element
-Type WebAPI.input is not a subtype of ReactDOM.node
-Type array<ReactDOM.element> is not a subtype of array<WebAPI.element>
-Type ReactDOM.element is not compatible with type WebAPI.element
+none
function a(el) {
return el;
}Diagnostic modified (a class that is not a superclass) let d = (el: ReactDOM.element) => (el :> WebAPI.input)
Type ReactDOM.element is not a subtype of WebAPI.input
+ReactDOM.element is the class Element, a subclass of Node. WebAPI.input is the class HTMLInputElement. A class type coerces to its own class and to the superclasses it declares.The fields or contents of the two declarations are not compared: each says what its module knows about the class. A class is its path and its module, so a global Review
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9432 +/- ##
==========================================
- Coverage 82.22% 82.21% -0.01%
==========================================
Files 462 462
Lines 65582 65628 +46
==========================================
+ Hits 53923 53957 +34
- Misses 11659 11671 +12
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn total: 638,385,473 → 638,386,871 (±0.00%). Generated projects: 629,067,681 → 629,067,670 (±0.00%). None of the 265 benchmarks changed by 0.1% or more. InstructionsIn total: 9,221,423,617 → 9,222,162,913 (+0.01%).
Output sizeIn total: 26,070,038 → 26,072,193 (+0.01%).
Pull request 7964eea compared with base 17874c0 in this run. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9432 |
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7964eeafec
ℹ️ 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".
| match Env.find_type path env with | ||
| | {type_shape = Recognized_as (InstanceType (Class {name}))} -> Some name |
There was a problem hiding this comment.
Check the target's own annotation before granting class identity
For a private refinement annotated only with @notUndefined, Variant_layout.declaration_shape copies the abbreviated type's recognized class into type_shape. Consequently, with @instanceOf("Element") type element and @notUndefined type validated = private element, this branch treats validated as another declaration of Element and accepts (el :> validated) outside its defining module. This bypasses the private refinement even though the target never declares @instanceOf, contrary to the protection described in this change. Check the declaration's actual annotation rather than relying solely on its resolved shape.
Useful? React with 👍 / 👎.
|
Thanks, this is a small, contained change, and it already gives us what we need for the WebAPI work: JSX and ReactDOM can declare their own 1. A class declaration with a record behind it lets any value of the class become that record module W = { @instanceOf("Element") type element }
type shape = {secret: int}
module Mine = { @instanceOf("Element") type el = private shape }
let read = (x: W.element) => ((x :> Mine.el) :> shape).secretThis compiles to 2. Please document that declared superclasses are trusted module W = {
@instanceOf("Node") type node
@instanceOf("Element") type element = private node
}
module Mine = {
@instanceOf("Element") type myEl
@instanceOf("Node") type fakeNode = private myEl
}
let down = (n: W.node) => ((n :> Mine.fakeNode) :> W.element)A wrong superclass in one package lets any Apart from these, invariant positions ( This review was done by Claude. |
Two packages can bind the same JavaScript class with separate
@instanceOftypes, but currently need shared type declarations orObj.magicto exchange their values. This change lets:>coerce between declarations of the same class and to declared superclasses, including across compiled module boundaries.The target must declare its own
@instanceOf: an unannotated private refinement remains protected. In invariant positions such as arrays, refs, and mutable fields, only declarations of the same class coerce to one another. Class identity includes its module, so global and imported classes stay distinct. Refused class coercions now name the classes and their declared ancestry.This is the explicit-coercion part of #9413, recreated on a branch in this repository so benchmark results can be posted. Automatic coercion at arguments is split into #9433, stacked on this one. Includes the explicit-coercion documentation, completion text, and its own changelog fragment.
Validation: built the compiler from this branch; both runtime regression tests and the full
super_errorssuite pass; generated regression JavaScript matches the fixtures; changelog and commit-record checks pass. The local environment lacks dependencies for the complete toolchain build, so full CI remains required. Local runtime dependencies were prepared with the original PR's CI-built tooling; the changed fixtures and diagnostic suite were compiled with this branch's compiler.