Semantic Diff

#9433 Coerce class types automatically at arguments

1 commit

7964eeafecf53a150740408cb91424cbb7f7fb2e..pr-9433

181b0bbbd · jfrolich + Jaap Frolich · Nov 16, 2026, 20:24 UTC

Accept another declaration of a class at an argument without :>

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.

modifiedTyping an argument at a site that declares its type, Typecore.unify_exp_at_argument

rule
Before
Γ ⊢ e : ττ = τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Argument)
After
Γ ⊢ e : ττ = τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Argument)
Γ ⊢ e : ττ ≠ τ'τ ≤class τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Class argument)
τ ≤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

removedDiagnostic 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
After
This has type: ReactDOM.elementBut this function argument is expecting: WebAPI.element This has type: WebAPI.inputBut the record field container is expected to have type: ReactDOM.elementnone
js
After
function a(el) {  el.focus();}function b(input) {  return {    container: input  };}

modifiedDiagnostic where :> stays written

let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : blet 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
After
This has type: WebAPI.elementBut 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.elementPossible solutions:- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)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.
  • 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.