From aa25c9ed865857b6d8e2a667395e49ef40b447b2 Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Tue, 3 Nov 2026 09:27:03 +0000 Subject: [PATCH 1/4] Try a cheap bounded walk before the full occur checks Why: Every variable link in unification runs `occur`, and the Tvar/Tconstr cases also run `deep_occur`. `occur` builds an AVL set of visited nodes along every path and `deep_occur` marks every node and walks the type again to unmark it, although almost all of these walks see a handful of nodes. On a 552 module React app they make 943k and 494k calls, about 0.5 s of 14 s single domain CPU. Both checks now first walk the type with no visited set and no marks, giving up after 256 nodes (which also stops cycles and heavy sharing); when the walk gives up, the unchanged full walk runs. - `occur` in expression mode: a walk over the edges `occur_rec` follows that finishes without meeting the variable proves `occur_rec` would neither raise nor expand anything. Pattern mode, which looks types up, always runs the full walk. - `deep_occur`: the walk only answers "absent". It gives up on meeting the variable and on a node an enclosing walk already marked, so the marks left behind are the same as the full walk's. Review: Can an answer, a diagnostic or an artifact change? No: each fast walk either proves the full walk would do nothing more than it did, or gives up and the full walk runs from scratch. The new fixtures pin the errors, including one past the budget, and the corpus (5,398 artifacts and 1,147 .cmt/.cmti) is byte identical to master. Signed-off-by: Jono Prest --- changes/occur-check-fast-path.polish.md | 1 + compiler/ml/ctype.ml | 66 +- .../occurs_check_function_type.res.expected | 17 + .../occurs_check_large_type.res.expected | 619 ++++++++++++++++++ ...urs_check_through_constructor.res.expected | 11 + .../fixtures/occurs_check_function_type.res | 6 + .../fixtures/occurs_check_large_type.res | 307 +++++++++ .../occurs_check_through_constructor.res | 2 + 8 files changed, 1026 insertions(+), 3 deletions(-) create mode 100644 changes/occur-check-fast-path.polish.md create mode 100644 tests/build_tests/super_errors/expected/occurs_check_function_type.res.expected create mode 100644 tests/build_tests/super_errors/expected/occurs_check_large_type.res.expected create mode 100644 tests/build_tests/super_errors/expected/occurs_check_through_constructor.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/occurs_check_function_type.res create mode 100644 tests/build_tests/super_errors/fixtures/occurs_check_large_type.res create mode 100644 tests/build_tests/super_errors/fixtures/occurs_check_through_constructor.res diff --git a/changes/occur-check-fast-path.polish.md b/changes/occur-check-fast-path.polish.md new file mode 100644 index 0000000000..b43bc6204b --- /dev/null +++ b/changes/occur-check-fast-path.polish.md @@ -0,0 +1 @@ +- Build: skip unneeded work in the type checker's occur checks and when specializing primitives. diff --git a/compiler/ml/ctype.ml b/compiler/ml/ctype.ml index 7b86f96de7..e0a7ff6924 100644 --- a/compiler/ml/ctype.ml +++ b/compiler/ml/ctype.ml @@ -1380,6 +1380,7 @@ let is_contractive env p = exception Occur +(* [occur_reaches] must follow the same edges in expression mode. *) let rec occur_rec env allow_recursive visited ty0 = function | {desc = Tlink ty} -> occur_rec env allow_recursive visited ty0 ty | ty -> ( @@ -1413,7 +1414,38 @@ let type_changed () = Domain.DLS.get type_changed_key let merge r b = if b then r := true -let occur env ty0 ty = +(* Cheap first passes for [occur] and [deep_occur]: walks with no visited + set and no marks that give up (raise [Give_up]) after [walk_budget] + nodes, which also stops cycles and heavy sharing, or when they cannot + decide; the caller then runs the full walk. A pass that finishes has + visited every node the full walk would. *) +exception Give_up + +let walk_budget = 256 + +let spend budget = + decr budget; + if !budget < 0 then raise Give_up + +(* Must follow the same edges as [occur_rec] in expression mode. Finishing + proves [occur_rec] would neither meet [ty0] nor find a [Tconstr] cycle, + its only ways to raise, and it expands types only after raising. *) +let rec occur_reaches budget ty0 ty = + match ty.desc with + | Tlink ty -> occur_reaches budget ty0 ty + | _ -> ( + if ty == ty0 then raise Occur; + spend budget; + match ty.desc with + | Tobject _ | Tvariant _ -> () + | _ -> iter_type_expr (occur_reaches budget ty0) ty) + +let occur_cannot_fail ty0 ty = + match occur_reaches (ref walk_budget) ty0 ty with + | () -> true + | exception (Occur | Give_up) -> false + +let occur_full env ty0 ty = let allow_recursive = !(umode ()) = Pattern in let old = !(type_changed ()) in try @@ -1432,6 +1464,12 @@ let occur env ty0 ty = | Occur -> Unify [] | _ -> exn) +(* In pattern mode [occur_rec] looks types up ([is_contractive]), which can + load interfaces, so only expression mode takes the shortcut. *) +let occur env ty0 ty = + if !(umode ()) = Expression && occur_cannot_fail ty0 ty then () + else occur_full env ty0 ty + let occur_in env ty0 t = try occur env ty0 t; @@ -1657,8 +1695,26 @@ let mkvariant fields closed = (**** Unification ****) -(* Return whether [t0] occurs in [ty]. Objects are also traversed. *) -let deep_occur t0 ty = +(* Must follow the same edges as [deep_occur_marked]. Answers only "absent": + it gives up on meeting [t0] (where the marked walk stops early) and on a + node an enclosing walk marked (which the marked walk skips but then + unmarks in [unmark_type]), so it never leaves marks that differ. *) +let deep_occur_fast_absent t0 ty = + let budget = ref walk_budget in + let rec walk ty = + let ty = repr ty in + if ty.level < lowest_level then raise Give_up + else ( + if ty == t0 then raise Occur; + spend budget; + iter_type_expr walk ty) + in + match walk ty with + | () -> true + | exception (Occur | Give_up) -> false + +(* [deep_occur_fast_absent] must follow the same edges. *) +let deep_occur_marked t0 ty = let rec occur_rec ty = let ty = repr ty in if ty.level >= lowest_level then ( @@ -1674,6 +1730,10 @@ let deep_occur t0 ty = unmark_type ty; true +(* Return whether [t0] occurs in [ty]. Objects are also traversed. *) +let deep_occur t0 ty = + (not (deep_occur_fast_absent t0 ty)) && deep_occur_marked t0 ty + (* 1. When unifying two non-abbreviated types, one type is made a link to the other. When unifying an abbreviated type with a diff --git a/tests/build_tests/super_errors/expected/occurs_check_function_type.res.expected b/tests/build_tests/super_errors/expected/occurs_check_function_type.res.expected new file mode 100644 index 0000000000..1ff8adebf3 --- /dev/null +++ b/tests/build_tests/super_errors/expected/occurs_check_function_type.res.expected @@ -0,0 +1,17 @@ + + We've found a bug for you! + /.../fixtures/occurs_check_function_type.res:4:22 + + 2 │ contains it. */ + 3 │ let f = (x: 'a) => { + 4 │ let y: 'a => int = x + 5 │ y + 6 │ } + + This value might need to be wrapped in a function that takes an extra + parameter of type 'a + + Here's the original error message + This has type: 'a + But it's expected to have type: 'a => int + The type variable 'a occurs inside 'a => int \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/occurs_check_large_type.res.expected b/tests/build_tests/super_errors/expected/occurs_check_large_type.res.expected new file mode 100644 index 0000000000..1479dc9bd1 --- /dev/null +++ b/tests/build_tests/super_errors/expected/occurs_check_large_type.res.expected @@ -0,0 +1,619 @@ + + We've found a bug for you! + /.../fixtures/occurs_check_large_type.res:306:7 + + 304 ┆ 1, + 305 ┆ 1, + 306 ┆ x, + 307 ┆ ) + 308 ┆ + + This has type: + ( + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + 'a, + ) + But it's expected to have type: 'a + The type variable 'a occurs inside + ( + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + int, + 'a, + ) \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/occurs_check_through_constructor.res.expected b/tests/build_tests/super_errors/expected/occurs_check_through_constructor.res.expected new file mode 100644 index 0000000000..f0944f24a0 --- /dev/null +++ b/tests/build_tests/super_errors/expected/occurs_check_through_constructor.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/occurs_check_through_constructor.res:2:24 + + 1 │ /* The variable occurs under a type constructor's argument. */ + 2 │ let f = x => x == list{x} + 3 │ + + This has type: list<'a> + But it's expected to have type: 'a + The type variable 'a occurs inside list<'a> \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/occurs_check_function_type.res b/tests/build_tests/super_errors/fixtures/occurs_check_function_type.res new file mode 100644 index 0000000000..85a44e0a7a --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/occurs_check_function_type.res @@ -0,0 +1,6 @@ +/* The occur check rejects a variable unified with a function type that + contains it. */ +let f = (x: 'a) => { + let y: 'a => int = x + y +} diff --git a/tests/build_tests/super_errors/fixtures/occurs_check_large_type.res b/tests/build_tests/super_errors/fixtures/occurs_check_large_type.res new file mode 100644 index 0000000000..95483a8195 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/occurs_check_large_type.res @@ -0,0 +1,307 @@ +/* The variable occurs after more than 256 other type nodes, beyond the + cheap first pass of the occur check, so the full check finds it. */ +let f = x => + x == + ( + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + x, + ) diff --git a/tests/build_tests/super_errors/fixtures/occurs_check_through_constructor.res b/tests/build_tests/super_errors/fixtures/occurs_check_through_constructor.res new file mode 100644 index 0000000000..53ef07159a --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/occurs_check_through_constructor.res @@ -0,0 +1,2 @@ +/* The variable occurs under a type constructor's argument. */ +let f = x => x == list{x} From f987a7d5200f513e0a61918229b3bd11d56cd864 Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Tue, 3 Nov 2026 09:27:03 +0000 Subject: [PATCH 2/4] Skip scraping a primitive's type when no specialization reads it Why: `specialize_primitive` scraped the primitive's whole function type (a deep copy plus head expansion) on every use, but only unified operators and comparisons look at it. For the other 95% of uses (on a 552 module React app, 21,582 of 22,770) the result was thrown away and `find_builtin` answered. For a function type whose primitive is in neither table, it now calls `find_builtin` directly. Review: Could the skipped scrape have had an effect? Scraping a function type looks nothing up: only a Tconstr head is expanded. What is left is a copy with fresh type ids, made during Lambda translation after the .cmi and .cmt are written, whose result nothing read. The corpus artifacts and .cmt files are byte identical to master. Signed-off-by: Jono Prest --- compiler/ml/translcore.ml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index b0aac24935..a084e9bbd7 100644 --- a/compiler/ml/translcore.ml +++ b/compiler/ml/translcore.ml @@ -412,7 +412,7 @@ let specialize_comparison (* Specialize a primitive from available type information, raise Not_found if primitive is unknown *) -let specialize_primitive p env ty (* ~has_constant_constructor *) = +let specialize_typed_primitive p env ty (* ~has_constant_constructor *) = let fn_expr = is_function_type env ty in let unified = match fn_expr with @@ -429,6 +429,20 @@ let specialize_primitive p env ty (* ~has_constant_constructor *) = | None -> Primitive table.objcomp with Not_found -> find_builtin p.prim_name) +(* Only unified operators and comparisons read the primitive's type, so the + others skip scraping it (a copy of the whole function type) when it is a + function type: scraping one then looks nothing up (only a [Tconstr] head + is expanded, and only a [Tconstr] result is looked up), so the copy is + all the skipped work. *) +let specialize_primitive p env ty = + match (Btype.repr ty).desc with + | Tarrow _ + when not + (Hashtbl.mem Unified_ops.index_by_name p.prim_name + || Hashtbl.mem comparisons_table p.prim_name) -> + find_builtin p.prim_name + | _ -> specialize_typed_primitive p env ty + (* [is_unit] excluded: unit shares the undefined constant but is not a [%null] / [%undefined] literal, and comparing it never suppressed the warning. *) From 296205049e1e2796e428d67744944898438e5904 Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Mon, 9 Nov 2026 13:10:59 +0000 Subject: [PATCH 3/4] Build the occur walk's closure once per call Why: `occur_reaches` passed `occur_reaches budget ty0` to `iter_type_expr` at every node, so the fast walk allocated a partial application per node it visited. `occur_reaches` now defines its walk once per call, capturing the budget and the variable, as `deep_occur_fast_absent` already does. The edges it follows and the exceptions it raises are unchanged. Measured with scripts/compilerBenchmarks.js against the previous commit: allocated words -1.58% in total, stress/TypeAliasChain.res -53%, Belt and runtime files -2% to -2.7%. On a 552 module React app (clean build, one domain) the build allocates 0.05% fewer words. A version that returns a bool instead of raising allocated more than this one (-1.27% in total), because `List.for_all` takes a closure per list. Signed-off-by: Jono Prest --- compiler/ml/ctype.ml | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/compiler/ml/ctype.ml b/compiler/ml/ctype.ml index e0a7ff6924..3eb2adcd6f 100644 --- a/compiler/ml/ctype.ml +++ b/compiler/ml/ctype.ml @@ -1430,18 +1430,23 @@ let spend budget = (* Must follow the same edges as [occur_rec] in expression mode. Finishing proves [occur_rec] would neither meet [ty0] nor find a [Tconstr] cycle, its only ways to raise, and it expands types only after raising. *) -let rec occur_reaches budget ty0 ty = - match ty.desc with - | Tlink ty -> occur_reaches budget ty0 ty - | _ -> ( - if ty == ty0 then raise Occur; - spend budget; +let occur_reaches ty0 ty = + let budget = ref walk_budget in + (* One closure for the whole walk, not a partial application per node. *) + let rec walk ty = match ty.desc with - | Tobject _ | Tvariant _ -> () - | _ -> iter_type_expr (occur_reaches budget ty0) ty) + | Tlink ty -> walk ty + | _ -> ( + if ty == ty0 then raise Occur; + spend budget; + match ty.desc with + | Tobject _ | Tvariant _ -> () + | _ -> iter_type_expr walk ty) + in + walk ty let occur_cannot_fail ty0 ty = - match occur_reaches (ref walk_budget) ty0 ty with + match occur_reaches ty0 ty with | () -> true | exception (Occur | Give_up) -> false From 86cf87a531166c8f774d329e1d265d03f27e3b84 Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Mon, 9 Nov 2026 14:33:03 +0000 Subject: [PATCH 4/4] Walk the occur fast paths without allocating Why: The bounded walks that `occur` and `deep_occur` try first allocated a ref for their budget and a closure for the walk on every call, and `iter_type_expr` allocated another closure at each arrow type they visited. Unification runs `occur` for every variable it links, mostly on small types, so this allocation was a large share of what the fast paths cost. `occur_reaches` and `deep_occur_fast_absent` now walk the type with mutually recursive functions that take the budget left and return what remains, following the edges of `iter_type_expr` written out by constructor. The match on `type_desc` lists every constructor, so a new one is a compile error here rather than a missed edge. Only `deep_occur_fast_absent` at a `Tvariant` still goes through `iter_type_expr` with a closure, for the row's fields. The nodes visited, the order, the budget and the exceptions raised are unchanged. Measured with scripts/compilerBenchmarks.js against the previous commit: allocated words -1.03% in total (-1.14% without the project builds), stress/TypeAliasChain.res -8.3%, Belt map and set modules -2.7% to -3.4%. On a 552 module React app (clean build, one domain) the build allocates 0.99% fewer words. Keeping the closures and only skipping the walk for a leaf type (a variable or a constructor without arguments) gave -0.57% in total and -0.54% on the app. Review: Can a result change? No: a walk that finishes visited exactly the nodes the closure version visited, within the same budget; one that gives up still falls back to the full check. Signed-off-by: Jono Prest --- compiler/ml/ctype.ml | 98 ++++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/compiler/ml/ctype.ml b/compiler/ml/ctype.ml index 3eb2adcd6f..0e0eaef1e4 100644 --- a/compiler/ml/ctype.ml +++ b/compiler/ml/ctype.ml @@ -1423,27 +1423,41 @@ exception Give_up let walk_budget = 256 -let spend budget = - decr budget; - if !budget < 0 then raise Give_up - (* Must follow the same edges as [occur_rec] in expression mode. Finishing proves [occur_rec] would neither meet [ty0] nor find a [Tconstr] cycle, - its only ways to raise, and it expands types only after raising. *) -let occur_reaches ty0 ty = - let budget = ref walk_budget in - (* One closure for the whole walk, not a partial application per node. *) - let rec walk ty = - match ty.desc with - | Tlink ty -> walk ty - | _ -> ( - if ty == ty0 then raise Occur; - spend budget; - match ty.desc with - | Tobject _ | Tvariant _ -> () - | _ -> iter_type_expr walk ty) - in - walk ty + its only ways to raise, and it expands types only after raising. The + edges are those of [iter_type_expr], spelled out so that the walk + allocates nothing: it returns the budget left instead of keeping it in a + ref that a closure captures. *) +let rec occur_reaches_budget ty0 budget ty = + match ty.desc with + | Tlink ty -> occur_reaches_budget ty0 budget ty + | desc -> ( + if ty == ty0 then raise Occur; + let budget = budget - 1 in + if budget < 0 then raise Give_up; + match desc with + | Tobject _ | Tvariant _ | Tvar _ | Tnil | Tunivar _ -> budget + | Tarrow (params, ret) -> + occur_reaches_budget ty0 (occur_reaches_params ty0 budget params) ret + | Ttuple l | Tconstr (_, l, _) | Tpackage (_, _, l) -> + occur_reaches_list ty0 budget l + | Tfield {typ; rest} -> + occur_reaches_budget ty0 (occur_reaches_budget ty0 budget typ) rest + | Tlink ty | Tsubst ty -> occur_reaches_budget ty0 budget ty + | Tpoly (ty, tyl) -> + occur_reaches_list ty0 (occur_reaches_budget ty0 budget ty) tyl) + +and occur_reaches_list ty0 budget = function + | [] -> budget + | ty :: l -> occur_reaches_list ty0 (occur_reaches_budget ty0 budget ty) l + +and occur_reaches_params ty0 budget = function + | [] -> budget + | {typ} :: l -> + occur_reaches_params ty0 (occur_reaches_budget ty0 budget typ) l + +let occur_reaches ty0 ty = ignore (occur_reaches_budget ty0 walk_budget ty) let occur_cannot_fail ty0 ty = match occur_reaches ty0 ty with @@ -1703,19 +1717,41 @@ let mkvariant fields closed = (* Must follow the same edges as [deep_occur_marked]. Answers only "absent": it gives up on meeting [t0] (where the marked walk stops early) and on a node an enclosing walk marked (which the marked walk skips but then - unmarks in [unmark_type]), so it never leaves marks that differ. *) + unmarks in [unmark_type]), so it never leaves marks that differ. Like + [occur_reaches_budget], it spells out the edges of [iter_type_expr] so + that it allocates nothing, except at a [Tvariant]. *) +let rec deep_occur_budget t0 budget ty = + let ty = repr ty in + if ty.level < lowest_level then raise Give_up; + if ty == t0 then raise Occur; + let budget = budget - 1 in + if budget < 0 then raise Give_up; + match ty.desc with + | Tvar _ | Tnil | Tunivar _ -> budget + | Tarrow (params, ret) -> + deep_occur_budget t0 (deep_occur_params t0 budget params) ret + | Ttuple l | Tconstr (_, l, _) | Tpackage (_, _, l) -> + deep_occur_list t0 budget l + | Tobject ty | Tlink ty | Tsubst ty -> deep_occur_budget t0 budget ty + | Tfield {typ; rest} -> + deep_occur_budget t0 (deep_occur_budget t0 budget typ) rest + | Tpoly (ty, tyl) -> deep_occur_list t0 (deep_occur_budget t0 budget ty) tyl + | Tvariant _ -> + let budget = ref budget in + iter_type_expr (fun ty -> budget := deep_occur_budget t0 !budget ty) ty; + !budget + +and deep_occur_list t0 budget = function + | [] -> budget + | ty :: l -> deep_occur_list t0 (deep_occur_budget t0 budget ty) l + +and deep_occur_params t0 budget = function + | [] -> budget + | {typ} :: l -> deep_occur_params t0 (deep_occur_budget t0 budget typ) l + let deep_occur_fast_absent t0 ty = - let budget = ref walk_budget in - let rec walk ty = - let ty = repr ty in - if ty.level < lowest_level then raise Give_up - else ( - if ty == t0 then raise Occur; - spend budget; - iter_type_expr walk ty) - in - match walk ty with - | () -> true + match deep_occur_budget t0 walk_budget ty with + | _ -> true | exception (Occur | Give_up) -> false (* [deep_occur_fast_absent] must follow the same edges. *)