Skip to content

Commit 154c49e

Browse files
cknittclaude
andauthored
Fix a stack overflow on option of a recursive @unboxed variant (#9019)
Stop at recursion when checking whether a variant can be undefined 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 Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d53b8ae commit 154c49e

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix a stack overflow when compiling `option` of a recursive `@unboxed` variant.

‎compiler/ml/typeopt.ml‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ let scrape env ty = (scrape_ty env ty).desc
3737
records the type at the definition type so for ['a option]
3838
it will always be [Tvar]
3939
*)
40-
let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
40+
let rec cannot_contain_undefined ~visiting (typ : Types.type_expr) (env : Env.t)
41+
=
4142
match scrape env typ with
4243
| Tconstr (p, _, _) -> (
4344
(* all built in types could not inhabit none-like values:
@@ -72,7 +73,13 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
7273
_ );
7374
} ->
7475
false (* conservative *)
76+
| {type_kind = Type_variant _} when List.exists (Path.same p) visiting ->
77+
(* A value reaches [undefined] through finitely many unboxed
78+
payloads, so the cases met again add none: whether the variant
79+
can be [undefined] is decided by its other cases *)
80+
true
7581
| {type_kind = Type_variant (cdecls, layout_ref)} ->
82+
let visiting = p :: visiting in
7683
let layout = Variant_runtime.get_layout layout_ref in
7784
let rec all_cases_cannot_contain_undefined position = function
7885
| [] -> true
@@ -90,13 +97,13 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
9097
match cd.cd_args with
9198
| Cstr_tuple [t] ->
9299
Ast_untagged_variants.type_is_builtin_object t
93-
|| type_cannot_contain_undefined t env
100+
|| cannot_contain_undefined ~visiting t env
94101
| Cstr_tuple [] -> true
95102
| Cstr_tuple (_ :: _ :: _) ->
96103
true (* Not actually possible for an unboxed payload. *)
97104
| Cstr_record [{ld_type = t}] ->
98105
Ast_untagged_variants.type_is_builtin_object t
99-
|| type_cannot_contain_undefined t env
106+
|| cannot_contain_undefined ~visiting t env
100107
| Cstr_record ([] | _ :: _ :: _) -> true)
101108
&& all_cases_cannot_contain_undefined (position + 1) rest
102109
in
@@ -106,6 +113,9 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
106113
| Tobject _ ->
107114
false
108115

116+
let type_cannot_contain_undefined typ env =
117+
cannot_contain_undefined ~visiting:[] typ env
118+
109119
let is_function_type env ty =
110120
match scrape env ty with
111121
| Tarrow (params, ret) -> Some ((List.hd params).typ, ret)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";
6+
7+
function someT(x) {
8+
return x;
9+
}
10+
11+
function someA(x) {
12+
return x;
13+
}
14+
15+
function someU(x) {
16+
return Primitive_option.some(x);
17+
}
18+
19+
Mocha.describe("Option_recursive_unboxed_test", () => {
20+
Mocha.test("a recursive variant that cannot be undefined", () => {
21+
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 20, characters 7-14", "End", "End");
22+
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 21, characters 7-14", "AEnd", "AEnd");
23+
});
24+
Mocha.test("a recursive variant that can be undefined", () => {
25+
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 25, characters 7-14", false, false);
26+
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 26, characters 7-14", false, false);
27+
});
28+
});
29+
30+
export {
31+
someT,
32+
someA,
33+
someU,
34+
}
35+
/* Not a pure module */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
open Mocha
2+
open Test_utils
3+
4+
// Whether option of a recursive @unboxed variant can be unboxed (#9018)
5+
6+
@unboxed type rec t = Recur(t) | End
7+
8+
@unboxed type rec a = A(b) | AEnd
9+
@unboxed and b = B(a) | BEnd
10+
11+
// Can be undefined through its own case, so option of it stays boxed
12+
@unboxed type rec u = Wrap(u) | @as(undefined) Missing
13+
14+
let someT = (x: t) => Some(x)
15+
let someA = (x: a) => Some(x)
16+
let someU = (x: u) => Some(x)
17+
18+
describe(__MODULE__, () => {
19+
test("a recursive variant that cannot be undefined", () => {
20+
eq(__LOC__, Obj.magic(someT(End)), "End")
21+
eq(__LOC__, Obj.magic(someA(A(B(AEnd)))), "AEnd")
22+
})
23+
24+
test("a recursive variant that can be undefined", () => {
25+
eq(__LOC__, someU(Missing) == None, false)
26+
eq(__LOC__, someU(Wrap(Missing)) == None, false)
27+
})
28+
})

0 commit comments

Comments
 (0)