Skip to content

Explain coercion errors with variants and unknown correctly - #9509

Open
cknitt wants to merge 1 commit into
masterfrom
coercion-messages-9358
Open

cknitt wants to merge 1 commit into
masterfrom
coercion-messages-9358

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #9358.

Several coercion errors gave wrong advice or no explanation. Each case now says why the coercion fails:

  • unknown: nothing but unknown itself coerces to or from it. The error used to say unknown is not unboxed (it is) or suggest changing the predefined type. It now says so directly, and for coercions from unknown points to Type.Classify.classify.
  • Primitive to a variant without a matching case (string :> a with type 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 @unboxed on a multi-argument constructor).
  • bool or unit: no explanation before. Now: "bool cannot be coerced to or from another type."
  • A variant and an abstract 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.
  • Variant to variant: a missing constructor was only reported when the source had more constructors than the target, and payload or @as mismatches 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."
  • Variant to a primitive with wrong literals (type t = A | B to float): the "Fix this by…" line was repeated for every constructor and said "has". It is now printed once, with "have".
  • Unboxed variant to a primitive with a mismatched payload (Int(int) when coercing to string): 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.
  • Unboxed int payload to int: was rejected while string, float and bigint payloads coerced. It now coerces too; the check reduces to "the payload is the target type".

Predef.path_unkonwn is renamed to path_unknown and exported for the new check. literal_to_string moves from Parmatch to Variant_runtime so the tag message can use it.

New error fixtures cover each case; VariantCoercion.res covers the int coercion.

Not part of this PR: Unknown(5) compiles to {TAG: "Unknown", _0: 5} although unknown is declared unboxed (the predefined declaration's layout is not unboxed), filed as #9510.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHXackTvKnQgew6Uhg

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix wrong or missing explanations in coercion errors involving variants and unknown.
  • Allow coercing an unboxed variant with an int payload to int.

Semantic Diff

1 commit · Full page

✏️ Explain coercion errors with variants and unknown correctly

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

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)

let f = (u: unknown) => (u :> string)

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.
+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)

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.
+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)

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).
+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)

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.
+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)

diagnostic

 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)

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)

type t = A | B
type u = B | C
let f = (v: u) => (v :> t)

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)

type t = A | B(int)
type u = B(string)
let f = (v: u) => (v :> t)

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)

type t = A | @as("x") B
type u = B
let f = (v: u) => (v :> t)

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)

type t = A | B
let f = (v: t) => (v :> float)

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.
+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)

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)

@unboxed type t = Int(int) | @as(1) First
let 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.

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

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

Instructions

In total: 8,966,821,071 → 8,966,921,840 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,449,936 → 27,452,857 (+0.01%).

Benchmark Base Head Change
output/playground/compiler.js 1,901,187 1,904,108 +0.15%

Pull request cbb3948 compared with base 7968a96 in this run.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.89189% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.56%. Comparing base (0a686a9) to head (cbb3948).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
compiler/typing/ctype.ml 90.00% 6 Missing ⚠️
compiler/typing/variant_runtime.ml 71.42% 2 Missing ⚠️
compiler/typing/printtyp.ml 97.43% 1 Missing ⚠️
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     
Files with missing lines Coverage Δ
compiler/typing/parmatch.ml 77.85% <100.00%> (+0.03%) ⬆️
compiler/typing/predef.ml 100.00% <100.00%> (ø)
compiler/typing/variant_coercion.ml 92.68% <100.00%> (+0.63%) ⬆️
compiler/typing/printtyp.ml 74.40% <97.43%> (+1.13%) ⬆️
compiler/typing/variant_runtime.ml 91.42% <71.42%> (-2.12%) ⬇️
compiler/typing/ctype.ml 70.95% <90.00%> (+0.21%) ⬆️

... and 13 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9509

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9509

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9509

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9509

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9509

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9509

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9509

commit: 0c469f2

@cknitt

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T20:02:46.419989Z cbb3948 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 93b28f384b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

@cknitt
cknitt force-pushed the coercion-messages-9358 branch 2 times, most recently from c67650e to 0c469f2 Compare December 3, 2026 16:17
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>
@cknitt
cknitt force-pushed the coercion-messages-9358 branch from 0c469f2 to cbb3948 Compare December 3, 2026 19:13
@cknitt

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: cbb39481d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

@cknitt
cknitt marked this pull request as ready for review December 3, 2026 20:05
@cknitt
cknitt requested a review from fhammerschmidt December 3, 2026 20:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong or missing explanations in three coercion error messages

1 participant