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..0e0eaef1e4 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,57 @@ 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 + +(* 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. 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 + | () -> 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 +1483,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 +1714,48 @@ 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. 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 = + match deep_occur_budget t0 walk_budget 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 +1771,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/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. *) 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}