Repository navigation
Conversation
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page ✏️ Explain coercion errors with variants and unknown correctlycbb3948 · cknitt + Claude · Dec 03, 2026, 16:12 UTC · edited
Diagnostic modified (unknown) let f = (u: unknown) => (u :> string)
Type unknown is not a subtype of string
-The 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.Diagnostic modified (variant without a catch-all case) type a = One | Two
let f = (s: string) => (s :> a)
Type string is not a subtype of a
-The 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).Diagnostic modified (unboxed variant without a catch-all case) @unboxed type t = String(string) | Int(int)
let f = (v: float) => (v :> t)
Type float is not a subtype of t
-The 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).Diagnostic modified (catch-all case in a variant that is not unboxed) type t = String(string) | A
let f = (s: string) => (s :> t)
Type string is not a subtype of t
-The 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.Diagnostic modified (bool or unit) @unboxed type t = String(string) | Int(int)
let g = (b: bool) => (b :> t)
Type bool is not a subtype of t
+bool cannot be coerced to or from another type.Diagnostic modified (abstract type and variant) @unboxed type t = String(string) | Int(int)
let g = (d: Date.t) => (d :> t)
Type 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 variantDiagnostic modified (constructor missing in the target variant) type t = A | B
type u = B | C
let f = (v: u) => (v :> t)
Type u is not a subtype of t
+u has 1 constructor that t does not have: C
+Therefore, it is not possible for t to represent u.Diagnostic modified (constructor payload mismatch) type t = A | B(int)
type u = B(string)
let f = (v: u) => (v :> t)
Type u is not a subtype of t
+The constructor B(string) of u cannot be coerced to B(int) of t.Diagnostic modified (constructor runtime tag mismatch) type t = A | @as("x") B
type u = B
let f = (v: u) => (v :> t)
Type u is not a subtype of t
+The constructor B is "B" at runtime in u, but "x" in t.Diagnostic modified (variant to float, fix said once) type t = A | B
let f = (v: t) => (v :> float)
Type t is not a subtype of float
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.
-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.
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.Diagnostic modified (unboxed payload mismatch) @unboxed type t = String(string) | Int(int)
let g = (v: t) => (v :> string)
Type 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.Typing modified (unboxed int payload) @unboxed type t = Int(int) | @as(1) First
let f = (v: t) => (v :> int)
function f(v) {
return v;
}Review
|
✅ Compiler benchmarksAllocated wordsIn total: 653,177,263 → 653,178,569 (±0.00%). Generated projects: 627,295,279 → 627,295,575 (±0.00%). None of the 265 benchmarks changed by 0.1% or more. InstructionsIn total: 8,966,821,071 → 8,966,921,840 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 27,449,936 → 27,452,857 (+0.01%).
Pull request cbb3948 compared with base 7968a96 in this run. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9509 +/- ##
==========================================
+ Coverage 82.54% 82.56% +0.01%
==========================================
Files 469 468 -1
Lines 66224 66266 +42
==========================================
+ Hits 54666 54710 +44
+ Misses 11558 11556 -2
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@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. |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9509 |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
c67650e to
0c469f2
Compare
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.
Diagnostic modified (unknown):
Example:
let f = (u: unknown) => (u :> string)
Before (diagnostic):
Type unknown is not a subtype of string
The 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.
After (diagnostic):
Type unknown is not a subtype of string
A value of type unknown cannot be coerced to another type. Check its type at runtime first, for example with Type.Classify.classify.
Diagnostic modified (variant without a catch-all case):
Example:
type a = One | Two
let f = (s: string) => (s :> a)
Before (diagnostic):
Type string is not a subtype of a
The variant a is not unboxed, so it cannot be coerced to a string.
Fix this by adding the @unboxed attribute to the variant a.
After (diagnostic):
Type string is not a subtype of a
The variant a is not unboxed and has no catch-all case for string, like String(string).
Diagnostic modified (unboxed variant without a catch-all case):
Example:
@unboxed type t = String(string) | Int(int)
let f = (v: float) => (v :> t)
Before (diagnostic):
Type float is not a subtype of t
The 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).
After (diagnostic):
Type float is not a subtype of t
The variant t has no catch-all case for float, like Float(float).
Diagnostic modified (catch-all case in a variant that is not unboxed):
Example:
type t = String(string) | A
let f = (s: string) => (s :> t)
Before (diagnostic):
Type string is not a subtype of t
The variant t is not unboxed, so it cannot be coerced to a string.
Fix this by adding the @unboxed attribute to the variant t.
After (diagnostic):
Type string is not a subtype of t
The variant t has a catch-all case for string, but is not unboxed.
Diagnostic modified (bool or unit):
Example:
@unboxed type t = String(string) | Int(int)
let g = (b: bool) => (b :> t)
Before (diagnostic):
Type bool is not a subtype of t
After (diagnostic):
Type bool is not a subtype of t
bool cannot be coerced to or from another type.
Diagnostic modified (abstract type and variant):
Example:
@unboxed type t = String(string) | Int(int)
let g = (d: Date.t) => (d :> t)
Before (diagnostic):
Type Date.t is not a subtype of t
After (diagnostic):
Type 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
Diagnostic modified (constructor missing in the target variant):
Example:
type t = A | B
type u = B | C
let f = (v: u) => (v :> t)
Before (diagnostic):
Type u is not a subtype of t
After (diagnostic):
Type u is not a subtype of t
u has 1 constructor that t does not have: C
Therefore, it is not possible for t to represent u.
Diagnostic modified (constructor payload mismatch):
Example:
type t = A | B(int)
type u = B(string)
let f = (v: u) => (v :> t)
Before (diagnostic):
Type u is not a subtype of t
After (diagnostic):
Type u is not a subtype of t
The constructor B(string) of u cannot be coerced to B(int) of t.
Diagnostic modified (constructor runtime tag mismatch):
Example:
type t = A | @as("x") B
type u = B
let f = (v: u) => (v :> t)
Before (diagnostic):
Type u is not a subtype of t
After (diagnostic):
Type u is not a subtype of t
The constructor B is "B" at runtime in u, but "x" in t.
Diagnostic modified (variant to float, fix said once):
Example:
type t = A | B
let f = (v: t) => (v :> float)
Before (diagnostic):
Type t is not a subtype of float
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.
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.
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.
After (diagnostic):
Type t is not a subtype of float
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.
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.
Fix this by making sure all constructors in variant t have a runtime representation of float.
Diagnostic modified (unboxed payload mismatch):
Example:
@unboxed type t = String(string) | Int(int)
let g = (v: t) => (v :> string)
Before (diagnostic):
Type t is not a subtype of string
After (diagnostic):
Type 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.
Typing modified (unboxed int payload):
Example:
@unboxed type t = Int(int) | @as(1) First
let f = (v: t) => (v :> int)
Before (diagnostic):
Type t is not a subtype of int
After (js):
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.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHXackTvKnQgew6Uhg
Claude-Session: https://claude.ai/code/session_01Kbf3gHCB4MFQHtieYtxxU9
Signed-off-by: Christoph Knittel <ck@cca.io>
0c469f2 to
cbb3948
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Fixes #9358.
Several coercion errors gave wrong advice or no explanation. Each case now says why the coercion fails:
unknown: nothing butunknownitself coerces to or from it. The error used to sayunknownis not unboxed (it is) or suggest changing the predefined type. It now says so directly, and for coercions fromunknownpoints toType.Classify.classify.string :> awithtype a = One | Two): the hint had the direction reversed and suggested@unboxed, which does not help. Now the error says what the variant lacks: "The variant a is not unboxed and has no catch-all case for string, like String(string)." The old "Fix this by adding a catch-all" advice is gone, because it could suggest a variant the compiler rejects (a second number case, or@unboxedon a multi-argument constructor).boolorunit: no explanation before. Now: "bool cannot be coerced to or from another type."Date.t :> t): no explanation before. Now the existing "The types of Date.t and t are different" message, which already covers records vs variants.@asmismatches explained nothing. Now each gets a line naming the constructor, like "The constructor B(string) of u cannot be coerced to B(int) of t." or "The constructor B is "B" at runtime in u, but "x" in t."type t = A | Btofloat): the "Fix this by…" line was repeated for every constructor and said "has". It is now printed once, with "have".Int(int)when coercing tostring): the explanation was empty, and type-variable or other payloads (e.g.array<string>) were reported as "not unboxed". Now the error names the constructor and its payload type.intpayload toint: was rejected whilestring,floatandbigintpayloads coerced. It now coerces too; the check reduces to "the payload is the target type".Predef.path_unkonwnis renamed topath_unknownand exported for the new check.literal_to_stringmoves fromParmatchtoVariant_runtimeso the tag message can use it.New error fixtures cover each case;
VariantCoercion.rescovers theintcoercion.Not part of this PR:
Unknown(5)compiles to{TAG: "Unknown", _0: 5}althoughunknownis declared unboxed (the predefined declaration's layout is not unboxed), filed as #9510.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gHXackTvKnQgew6Uhg