Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/recursive-unboxed-option.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix a stack overflow when compiling `option` of a recursive `@unboxed` variant.
16 changes: 13 additions & 3 deletions compiler/ml/typeopt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ let scrape env ty = (scrape_ty env ty).desc
records the type at the definition type so for ['a option]
it will always be [Tvar]
*)
let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
let rec cannot_contain_undefined ~visiting (typ : Types.type_expr) (env : Env.t)
=
match scrape env typ with
| Tconstr (p, _, _) -> (
(* all built in types could not inhabit none-like values:
Expand Down Expand Up @@ -72,7 +73,13 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
_ );
} ->
false (* conservative *)
| {type_kind = Type_variant _} when List.exists (Path.same p) visiting ->
(* A value reaches [undefined] through finitely many unboxed
payloads, so the cases met again add none: whether the variant
can be [undefined] is decided by its other cases *)
true
| {type_kind = Type_variant (cdecls, layout_ref)} ->
let visiting = p :: visiting in
let layout = Variant_runtime.get_layout layout_ref in
let rec all_cases_cannot_contain_undefined position = function
| [] -> true
Expand All @@ -90,13 +97,13 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
match cd.cd_args with
| Cstr_tuple [t] ->
Ast_untagged_variants.type_is_builtin_object t
|| type_cannot_contain_undefined t env
|| cannot_contain_undefined ~visiting t env
| Cstr_tuple [] -> true
| Cstr_tuple (_ :: _ :: _) ->
true (* Not actually possible for an unboxed payload. *)
| Cstr_record [{ld_type = t}] ->
Ast_untagged_variants.type_is_builtin_object t
|| type_cannot_contain_undefined t env
|| cannot_contain_undefined ~visiting t env
| Cstr_record ([] | _ :: _ :: _) -> true)
&& all_cases_cannot_contain_undefined (position + 1) rest
in
Expand All @@ -106,6 +113,9 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
| Tobject _ ->
false

let type_cannot_contain_undefined typ env =
cannot_contain_undefined ~visiting:[] typ env

let is_function_type env ty =
match scrape env ty with
| Tarrow (params, ret) -> Some ((List.hd params).typ, ret)
Expand Down
35 changes: 35 additions & 0 deletions tests/tests/src/option_recursive_unboxed_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Mocha from "mocha";
import * as Test_utils from "./test_utils.mjs";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";

function someT(x) {
return x;
}

function someA(x) {
return x;
}

function someU(x) {
return Primitive_option.some(x);
}

Mocha.describe("Option_recursive_unboxed_test", () => {
Mocha.test("a recursive variant that cannot be undefined", () => {
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 20, characters 7-14", "End", "End");
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 21, characters 7-14", "AEnd", "AEnd");
});
Mocha.test("a recursive variant that can be undefined", () => {
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 25, characters 7-14", false, false);
Test_utils.eq("File \"option_recursive_unboxed_test.res\", line 26, characters 7-14", false, false);
});
});

export {
someT,
someA,
someU,
}
/* Not a pure module */
28 changes: 28 additions & 0 deletions tests/tests/src/option_recursive_unboxed_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
open Mocha
open Test_utils

// Whether option of a recursive @unboxed variant can be unboxed (#9018)

@unboxed type rec t = Recur(t) | End

@unboxed type rec a = A(b) | AEnd
@unboxed and b = B(a) | BEnd

// Can be undefined through its own case, so option of it stays boxed
@unboxed type rec u = Wrap(u) | @as(undefined) Missing

let someT = (x: t) => Some(x)
let someA = (x: a) => Some(x)
let someU = (x: u) => Some(x)

describe(__MODULE__, () => {
test("a recursive variant that cannot be undefined", () => {
eq(__LOC__, Obj.magic(someT(End)), "End")
eq(__LOC__, Obj.magic(someA(A(B(AEnd)))), "AEnd")
})

test("a recursive variant that can be undefined", () => {
eq(__LOC__, someU(Missing) == None, false)
eq(__LOC__, someU(Wrap(Missing)) == None, false)
})
})
Loading