Skip to content

Type a let destructuring a GADT without switch diagnostics - #9081

Open
cknitt wants to merge 1 commit into
fix-9068-existential-namesfrom
fix-9069-existential-let-context
Open

cknitt wants to merge 1 commit into
fix-9068-existential-namesfrom
fix-9069-existential-let-context

Conversation

@cknitt

@cknitt cknitt commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Fixes #9069. Stacked on #9080, whose message appears in this fixture.

A let destructuring a GADT constructor is typed as a one-case match, so a type error in the rest of the block said "But this switch is expected to return" and added the hint about switch branches. The synthesized match is now marked like the one for optional argument defaults and gets function-style diagnostics.

New fixture: gadt_existential_let.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gLMawVgtmiKy3TMBc3

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • A type error in the body after a let that destructures a GADT constructor no longer talks about switch branches.

Semantic Diff

1 commit · Full page

Type a let destructuring a GADT without switch diagnostics

829f98b · cknitt + Claude · Oct 27, 2026, 12:45 UTC

Why

A let whose pattern contains a GADT constructor is typed as a one-case match. Its body was then checked as a switch branch, so a type error there said "But this switch is expected to return" and added the hint about switch branches, although the source has no switch. The synthesized match is now marked like the one for optional argument defaults and gets function-style diagnostics.

Diagnostic modified (let destructuring a GADT)

type rec any = Any('a, 'a => string): any

let get = value => {
  let Any(x, _) = value
  x
}

diagnostic

 This has type: $a
-But this switch is expected to return: 'a
+But it's expected to have type: 'a
 $a is an existential type bound by the constructor Any.
 It is only known in the scope of the pattern that matches Any.
-
-All branches in a switch must return the same type.
-To fix this, change your branch to return the expected type.

@cknitt
cknitt added this pull request to stack #9087 October 27, 2026 13:14
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.52%. Comparing base (f56b61c) to head (829f98b).

Additional details and impacted files
@@                     Coverage Diff                     @@
##           fix-9068-existential-names    #9081   +/-   ##
===========================================================
  Coverage                       81.52%   81.52%           
===========================================================
  Files                             460      460           
  Lines                           64659    64661    +2     
===========================================================
+ Hits                            52713    52717    +4     
+ Misses                          11946    11944    -2     
Files with missing lines Coverage Δ
compiler/ml/typecore.ml 88.18% <100.00%> (+0.09%) ⬆️
🚀 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 Oct 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 829f98b

@cknitt
cknitt marked this pull request as ready for review October 27, 2026 14:20
@cknitt

cknitt commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T14:21:34.326563Z adfceb2 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: adfceb2084

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

Why:
    A `let` whose pattern contains a GADT constructor is typed as a
    one-case match. Its body was then checked as a switch branch, so a
    type error there said "But this switch is expected to return" and
    added the hint about switch branches, although the source has no
    switch. The synthesized match is now marked like the one for optional
    argument defaults and gets function-style diagnostics.

Diagnostic modified (let destructuring a GADT):
Example:
    type rec any = Any('a, 'a => string): any

    let get = value => {
      let Any(x, _) = value
      x
    }
Before (diagnostic):
    This has type: $a
    But this switch is expected to return: 'a
    $a is an existential type bound by the constructor Any.
    It is only known in the scope of the pattern that matches Any.

    All branches in a switch must return the same type.
    To fix this, change your branch to return the expected type.
After (diagnostic):
    This has type: $a
    But it's expected to have type: 'a
    $a is an existential type bound by the constructor Any.
    It is only known in the scope of the pattern that matches Any.

Fixes: #9069
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLMawVgtmiKy3TMBc3
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the fix-9069-existential-let-context branch from adfceb2 to 829f98b Compare October 28, 2026 08:10

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.

Destructuring an existential with let reports "this switch"

1 participant