Skip to content

Fix a stack overflow on option of a recursive @unboxed variant - #9019

Merged
cknitt merged 1 commit into
masterfrom
fix-recursive-unboxed-option
Oct 24, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-recursive-unboxed-option

Conversation

@cknitt

@cknitt cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member

Fixes #9018.

Deciding whether option of a type can be unboxed (Typeopt.type_cannot_contain_undefined) checks the payload of every unboxed case. For @unboxed type rec t = Recur(t) | End it went from t into t forever, and the compiler crashed with a stack overflow.

The walk now remembers the variant declarations it is inside. A variant it meets again adds no way to be undefined: a value reaches undefined through finitely many unboxed payloads, so the variant's other cases decide. Some(End) now compiles to "End".

New test: option_recursive_unboxed_test.res. It covers a self-recursive variant, a mutually recursive pair (both unboxed in option), and a recursive variant with an @as(undefined) case, which stays boxed.

Found by Codex on #9017, where the new check for @notUndefined on abbreviations reaches the same walk.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t

Why:
    Deciding whether `option` of a recursive `@unboxed` variant can be
    unboxed walked the payload types without remembering the variants it
    was in, so `Recur(t)` sent it into `t` forever and the compiler died
    with a stack overflow (#9018).

The walk remembers the variant declarations it is in. A variant met again
adds no way to be undefined, since a value reaches `undefined` through
finitely many unboxed payloads, so the answer comes from its other cases.

JS modified (option of a recursive unboxed variant):
Example:
    @unboxed type rec t = Recur(t) | End
    let x: option<t> = Some(End)
Before (js): Fatal error: exception Stack overflow
After (js): let x = "End";

Fixes #9018

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a stack overflow when compiling option of a recursive @unboxed variant.

Semantic Diff

1 commit · Full page

Stop at recursion when checking whether a variant can be undefined

fbd72f7 · cknitt + Claude · Oct 24, 2026, 12:09 UTC

Why

Deciding whether option of a recursive @unboxed variant can be unboxed walked the payload types without remembering the variants it was in, so Recur(t) sent it into t forever and the compiler died with a stack overflow (#9018).

The walk remembers the variant declarations it is in. A variant met again adds no way to be undefined, since a value reaches undefined through finitely many unboxed payloads, so the answer comes from its other cases.

JS modified (option of a recursive unboxed variant)

@unboxed type rec t = Recur(t) | End
let x: option<t> = Some(End)

js

-Fatal error: exception Stack overflow
+let x = "End";

Fixes #9018

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.31%. Comparing base (d53b8ae) to head (fbd72f7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9019   +/-   ##
=======================================
  Coverage   81.31%   81.31%           
=======================================
  Files         456      456           
  Lines       64003    64006    +3     
=======================================
+ Hits        52044    52047    +3     
  Misses      11959    11959           
Files with missing lines Coverage Δ
compiler/ml/typeopt.ml 83.63% <100.00%> (+0.94%) ⬆️
🚀 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 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: fbd72f7

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt marked this pull request as ready for review October 24, 2026 12:25
@cknitt

cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T12:27:55.448853Z fbd72f7 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. Nice work!

Reviewed commit: fbd72f7e6d

ℹ️ 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 requested a review from cristianoc October 24, 2026 12:32
@cknitt
cknitt merged commit 154c49e into master Oct 24, 2026
26 checks passed
@cknitt
cknitt deleted the fix-recursive-unboxed-option branch October 24, 2026 16:21
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.

Stack overflow on option of a recursive @unboxed variant

2 participants