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