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>
Why:
With the previous commit `WebAPI.focus((el :> WebAPI.element))` compiles
for an `el: ReactDOM.element`, but each call into another package's
bindings still needs the written coercion, also for every JSX prop.
Where the compiler knows the type a site expects, it can make the
coercion itself: the two types name one class, and the value is the
same.
Typing modified (an argument at a site that declares its type, `Typecore.unify_exp_at_argument`):
Before (rule):
Γ ⊢ e : τ τ = τ'
──────────────────── (Argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
After (rule):
Γ ⊢ e : τ τ = τ'
──────────────────── (Argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
Γ ⊢ e : τ τ ≠ τ' τ ≤class τ'
──────────────────── (Class argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
τ ≤class τ': τ is a class type of the class of τ' or of a subclass, or τ and τ' are equal up to two declarations of one class inside them
Diagnostic removed (another declaration of the parameter's class):
Example:
module WebAPI = {
@instanceof("Element") type element
@instanceof("HTMLInputElement") type input = private element
@send external focus: element => unit = "focus"
}
module ReactDOM = {
@instanceof("Element") type element
type props = {container: element}
}
let a = (el: ReactDOM.element) => WebAPI.focus(el)
let b = (input: WebAPI.input): ReactDOM.props => {container: input}
Before (diagnostic): each line compiled alone
This has type: ReactDOM.element
But this function argument is expecting: WebAPI.element
This has type: WebAPI.input
But the record field container is expected to have type: ReactDOM.element
After (diagnostic): none
After (js):
function a(el) {
el.focus();
}
function b(input) {
return {
container: input
};
}
Diagnostic modified (where `:>` stays written):
Example:
let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : b
let take = (_: option<ReactDOM.element>) => ()
let d = (input: option<WebAPI.input>) => take(input)
Before (diagnostic): each compiled alone, the second with the parent commit's compiler
This has type: WebAPI.element
But this ternary is expected to return: ReactDOM.element
This has type: option<WebAPI.input>
But this function argument is expecting: option<ReactDOM.element>
The incompatible parts:
WebAPI.input vs ReactDOM.element
Possible solutions:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)
After (diagnostic): unchanged
The sites are the parameters of a function (labelled, optional and piped
arguments too), the fields of a record being built, which includes JSX
props, and constructor payloads: their expected type is declared. In
`first ? a : b` the expected type of `b` is the type of `a`, so accepting
it would make the type of the result, and with a subclass whether it
compiles, depend on the order of the branches; an assignment to a mutable
field keeps `:>` too. Inside another type only the same class is accepted
(`Same_class` steps of `subtype_rec`): `option<input>` for
`option<element>` is a subclass coercion that `:>` states. The argument
gets the expected type, as `(e :> t)` would give it.
Review: Does a program that compiled before get other types? No: the rule is only tried where the argument's type failed to unify with the expected one, which was an error.
Review: Are other errors printed differently? No: when the class check fails, it restores the type state it changed and the original clash is raised. The super_errors expectations are 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>
The rules for `@instanceOf` types in `UntaggedVariants.md` and the `@instanceOf` completion doc now say what a class type coerces to and where no `:>` is needed, with the errors and their fixtures. The changelog fragment describes the feature of the two commits before. 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 Diff3 commits · Full page Coerce a class type to every declaration of its class and its superclassesbfce750 · 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
Accept another declaration of a class at an argument without
|
| Before | After |
|---|---|
|
|
|
Diagnostic removed (another declaration of the parameter's class)
module WebAPI = {
@instanceOf("Element") type element
@instanceOf("HTMLInputElement") type input = private element
@send external focus: element => unit = "focus"
}
module ReactDOM = {
@instanceOf("Element") type element
type props = {container: element}
}
let a = (el: ReactDOM.element) => WebAPI.focus(el)
let b = (input: WebAPI.input): ReactDOM.props => {container: input}diagnostic · Before: each line compiled alone
-This has type: ReactDOM.element
-But this function argument is expecting: WebAPI.element
-
-This has type: WebAPI.input
-But the record field container is expected to have type: ReactDOM.element
+nonejs · After
function a(el) {
el.focus();
}
function b(input) {
return {
container: input
};
}Diagnostic modified (where :> stays written)
let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : b
let take = (_: option<ReactDOM.element>) => ()
let d = (input: option<WebAPI.input>) => take(input)diagnostic · Before: each compiled alone, the second with the parent commit's compiler
-This has type: WebAPI.element
-But this ternary is expected to return: ReactDOM.element
-
-This has type: option<WebAPI.input>
-But this function argument is expecting: option<ReactDOM.element>
-The incompatible parts:
- WebAPI.input vs ReactDOM.element
-Possible solutions:
-- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)
+unchangedThe sites are the parameters of a function (labelled, optional and piped arguments too), the fields of a record being built, which includes JSX props, and constructor payloads: their expected type is declared. In first ? a : b the expected type of b is the type of a, so accepting it would make the type of the result, and with a subclass whether it compiles, depend on the order of the branches; an assignment to a mutable field keeps :> too. Inside another type only the same class is accepted (Same_class steps of subtype_rec): option<input> for option<element> is a subclass coercion that :> states. The argument gets the expected type, as (e :> t) would give it.
Review
- Does a program that compiled before get other types? No: the rule is only tried where the argument's type failed to unify with the expected one, which was an error.
- Are other errors printed differently? No: when the class check fails, it restores the type state it changed and the original clash is raised. The super_errors expectations are unchanged.
Document that class types coerce by their class
db3017f · jfrolich + Jaap Frolich · Nov 16, 2026, 20:24 UTC
The rules for @instanceOf types in UntaggedVariants.md and the @instanceOf completion doc now say what a class type coerces to and where no :> is needed, with the errors and their fixtures. The changelog fragment describes the feature of the two commits before.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9413 +/- ##
==========================================
- Coverage 82.22% 82.22% -0.01%
==========================================
Files 462 462
Lines 65578 65662 +84
==========================================
+ Hits 53921 53990 +69
- Misses 11657 11672 +15
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
Superseded by #9432 (explicit class coercions) and #9433 (automatic argument coercions), recreated on branches in
rescript-lang/rescriptand linked as stack #9434.Builds on #9309, which made a class its path and its
@module.Two packages that bind the same JavaScript class each declare their own type for it, and today those types are unrelated:
:>only follows the abbreviations a type is declared with. A package that binds DOM APIs therefore has to depend on the one package that owns the DOM types, or its users convert withObj.magic. With this PR the class that@instanceOfnames decides what a class type coerces to, so every package can declare the part of a class it uses.:>. A class type coerces to any type whose own declaration states its class or one of its declared superclasses, in any module or package. Where a coercion needs equal types (array<_>,ref<_>, a mutable field), two declarations of one class are equal and a subclass is not. A private abbreviation without its own@instanceOf(type mine = private element) is a refinement, and nothing coerces into it. A globalMeshand theMeshthat"three"exports stay unrelated.:>at an argument. At a site that declares the type it expects (a function parameter, including labelled, optional and piped arguments; a field of a record being built, which includes JSX props in both theprop=xandprop={x}forms; a constructor payload), an argument of another declaration of the class, or of a subclass, is accepted and gets the expected type. Inside another type only the same class is accepted that way (option<input>foroption<element>still needs:>). It is not applied where the expected type comes from a neighbouring expression, such as the other branch of aniforswitch, because the result would then depend on the order of the branches. Assignments to a mutable field also keep:>.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.Implementation: a class arm in
Ctype.subtype_rec(class_step), aSame_classmode of it for the implicit rule (Ctype.coerces_between_class_declarations), applied byTypecore.unify_exp_at_argument. No.cmichange: classes come fromtype_shape.Docs and tests:
compiler/typing/UntaggedVariants.md, the@instanceOfcompletion doc.tests/tests/src/class_identity_test.res(withclass_identity_dom.resas the other package, read from its.cmi) runs explicit coercions, invariant positions, arguments, record fields, constructor payloads and a listener of one declaration passed for another.class_identity_not_an_ancestor,class_identity_branches,class_identity_nested_subclassandclass_identity_set_fieldcover the new message and the places where:>stays written.Known limitation: the expected type of an argument can itself come from an earlier argument through a type variable. For
let same: ('a, 'a) => bool,same(element, input)compiles becauseinputis accepted as anelement, whilesame(input, element)does not.Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.CONTRIBUTING.md).git commit -s).Started by Jaap Frolich in this Assistant session