Semantic Diff

#9509 Explain coercion errors with variants and unknown correctly

1 commit

0a686a9a9b6787ea4fcdb1e08dc70fd9d5760a66..pr-9509

cbb39481d · cknitt + Claude · Dec 03, 2026, 16:12 UTC

Explain coercion errors with variants and unknown correctly

Why

Fixes #9358. Coercing unknown claimed it is not unboxed, coercing a string to a variant without a string case suggested @unboxed (with the direction reversed), and coercions with bool, unit or an abstract type, with a mismatched unboxed payload, or between variants whose constructors differ explained nothing. The "Fix this by adding a catch-all" advice could suggest a variant the compiler rejects, so the messages now state the facts about the concrete types. An unboxed variant with an int payload also did not coerce to int, unlike string, float and bigint.

modifiedDiagnostic unknown

let f = (u: unknown) => (u :> string)
diagnostic
Before
After
Type unknown is not a subtype of stringType unknown is not a subtype of stringThe constructor Unknown of variant unknown has a payload, but the variant itself is not unboxed.This means that the constructor Unknown will be encoded as an object at runtime, which is not compatible with string.A value of type unknown cannot be coerced to another type. Check its type at runtime first, for example with Type.Classify.classify.

modifiedDiagnostic variant without a catch-all case

type a = One | Twolet f = (s: string) => (s :> a)
diagnostic
Before
After
Type string is not a subtype of aType string is not a subtype of aThe variant a is not unboxed, so it cannot be coerced to a string.Fix this by adding the @unboxed attribute to the variant a.The variant a is not unboxed and has no catch-all case for string, like String(string).

modifiedDiagnostic unboxed variant without a catch-all case

@unboxed type t = String(string) | Int(int)let f = (v: float) => (v :> t)
diagnostic
Before
After
Type float is not a subtype of tType float is not a subtype of tThe variant t is unboxed, but has no catch-all case for the primitive float, and therefore does not cover all values of type float.Fix this by adding a catch-all for float to t, like Float(float).The variant t has no catch-all case for float, like Float(float).

modifiedDiagnostic catch-all case in a variant that is not unboxed

type t = String(string) | Alet f = (s: string) => (s :> t)
diagnostic
Before
After
Type string is not a subtype of tType string is not a subtype of tThe variant t is not unboxed, so it cannot be coerced to a string.Fix this by adding the @unboxed attribute to the variant t.The variant t has a catch-all case for string, but is not unboxed.

modifiedDiagnostic bool or unit

@unboxed type t = String(string) | Int(int)let g = (b: bool) => (b :> t)
diagnostic
Before
After
Type bool is not a subtype of tType bool is not a subtype of t bool cannot be coerced to or from another type.

modifiedDiagnostic abstract type and variant

@unboxed type t = String(string) | Int(int)let g = (d: Date.t) => (d :> t)
diagnostic
Before
After
Type Date.t is not a subtype of tType Date.t is not a subtype of t The types of Date.t and t are different:- Date.t is an abstract type- t is a variant

modifiedDiagnostic constructor missing in the target variant

type t = A | Btype u = B | Clet f = (v: u) => (v :> t)
diagnostic
Before
After
Type u is not a subtype of tType u is not a subtype of t u has 1 constructor that t does not have: CTherefore, it is not possible for t to represent u.

modifiedDiagnostic constructor payload mismatch

type t = A | B(int)type u = B(string)let f = (v: u) => (v :> t)
diagnostic
Before
After
Type u is not a subtype of tType u is not a subtype of t The constructor B(string) of u cannot be coerced to B(int) of t.

modifiedDiagnostic constructor runtime tag mismatch

type t = A | @as("x") Btype u = Blet f = (v: u) => (v :> t)
diagnostic
Before
After
Type u is not a subtype of tType u is not a subtype of t The constructor B is "B" at runtime in u, but "x" in t.

modifiedDiagnostic variant to float, fix said once

type t = A | Blet f = (v: t) => (v :> float)
diagnostic
Before
After
Type t is not a subtype of floatType t is not a subtype of floatThe constructor A of variant t has no @as payload, which makes it a string at runtime.The constructor A of variant t has no @as payload, which makes it a string at runtime.That runtime representation is not compatible with the expected runtime representation of float.That runtime representation is not compatible with the expected runtime representation of float.Fix this by making sure all constructors in variant t has a runtime representation of float. The constructor B of variant t has no @as payload, which makes it a string at runtime.The constructor B of variant t has no @as payload, which makes it a string at runtime.That runtime representation is not compatible with the expected runtime representation of float.That runtime representation is not compatible with the expected runtime representation of float.Fix this by making sure all constructors in variant t has a runtime representation of float.Fix this by making sure all constructors in variant t have a runtime representation of float.

modifiedDiagnostic unboxed payload mismatch

@unboxed type t = String(string) | Int(int)let g = (v: t) => (v :> string)
diagnostic
Before
After
Type t is not a subtype of stringType t is not a subtype of string The constructor Int of unboxed variant t has a payload of type int, which is not compatible with string.

modifiedTyping unboxed int payload

@unboxed type t = Int(int) | @as(1) Firstlet f = (v: t) => (v :> int)
diagnostic
Before
Type t is not a subtype of int
js
After
function f(v) {  return v;}

Review

  • Can a coercion to or from unknown succeed through the new branch? No: it only fires after abbreviations (also private ones) are expanded and exactly one side is unknown, a pair that never unifies.