Skip to content

Checker rewrite R5a-2a: aliases, universal variables and the variables of an item - #438

Open
jfrolich wants to merge 5 commits into
ocaml-to-rust-rewritefrom
rust-type-checker-rewrite
Open

jfrolich wants to merge 5 commits into
ocaml-to-rust-rewritefrom
rust-type-checker-rewrite

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Slice R5a-2a of the type checker rewrite (CHECKER_REWRITE.md 10.12): the alias arm, universal variables and the polymorphic arm, globalize, transl_simple_type, transl_type_scheme, and unify_params with enforce_constraints, all in compiler-rust/src/tc/walk/typetexp.rs. Nothing a default build runs calls this code.

Commits

  1. The variable tables of an item: TypeVars::reset, Annot::univar, Annot::declared, Annot::add_declared, plus the changelog fragment.
  2. The alias arm, and the universal-variable lookup in the alias and variable arms. The ("int as 'a", AliasType) row of r5a_tests::DECLINED goes.
  3. The polymorphic arm and the Err exit: the level is put back and the open binders' universal variables are dropped. Also adds a step test of that drop.
  4. globalize, transl_simple_type, transl_type_scheme.
  5. unify_params and the constructor arm's road for a type without PLAIN_PARAMS. r5a_tests::a_type_without_plain_parameters_is_declined goes, and transl_type's doc comment is rewritten. The walk module keeps only dead_code.

In the second loop of unify_params (enforce_constraints) the body calls Checker::unify_var, not Checker::unify, as OCaml's subst does (!unify', ctype.ml:1129-1130, is unify_var, 2767).

Loops and the step tests that run them 10,000 times

Loop Step test Steps at 1,000 / 10,000
frames of aliases ten_thousand_aliases, aliases_nested_ten_thousand_deep 7,001 / 70,001; 4,003 / 40,003
(uses of one alias, no loop of its own) ten_thousand_uses_of_one_alias 2,015 / 20,015
binder's variables when entered; marking walk of the body; binding loop ten_thousand_universal_variables 7,006 / 70,006
the two loops of globalize; TypeVars::reset over declared rows ten_thousand_variables_globalized 6,001 / 60,001; reset of 10,000: 10,000
copy sessions and the two loops of unify_params ten_thousand_shared_parameters 13,994 / 139,994
TypeVars::drop_univars (reset and the Err exit) walk::typetexp::tests::ten_thousand_universal_variables_dropped (new) 1,000 / 10,000

Two loops count no step: writing a binder's heads when it is entered, and putting them back when it is left. Each is one store per variable of the binder, and both run in ten_thousand_universal_variables.

There is no hash map and no allocation per node. The sort in globalize is sort_unstable_by in place and is skipped for one row or none. No struct was added or grown.

Gates

  • cargo build --manifest-path compiler-rust/Cargo.toml: Finished.
  • cargo test --manifest-path compiler-rust/Cargo.toml --lib tc:::
    • before the first edit: test result: ok. 440 passed; 0 failed; 24 ignored; 0 measured; 2901 filtered out; finished in 954.48s
    • after the last commit: test result: ok. 464 passed; 0 failed; 0 ignored; 0 measured; 2901 filtered out; finished in 902.48s
    • 464 = 440 + 23 (un-ignored in r5a2_tests) + 1 (un-ignored in r5a_tests) − 1 (deleted) + 1 (new drop test).
  • cargo test --lib tc::r5a after each commit: 27/0/24, 35/0/16, 39/0/12, 47/0/4, 50/0/0 (passed/failed/ignored).
  • cargo clippy --manifest-path compiler-rust/Cargo.toml --all-targets -- -D warnings: clean after each commit.
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps: clean after each commit.
  • Rewatch was not touched, and its tests were not run locally.
  • Worker determinism (CI, parity (javascript)): 0 differing entries out of 920.
  • Instruction count (CI): base 4,355,225,329, head 4,356,195,143, +969,814 (+0.022%). Nothing of a default build calls this code, so nothing is saved and the difference is run-to-run noise (the base's five runs span 1.6 million).

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 5 commits November 29, 2026 16:22
TypeVars::reset forgets the declared, used and universal variables of an
item head by head (no table is filled whole) and sets the global level
one above the given level (reset_type_variables, typetexp.ml:213-216;
reset_global_level, ctype.ml:157). Annot::univar, Annot::declared and
Annot::add_declared look up and enter rows by Name through dense head
tables; univar answers at once when no binder is open. No test is
un-ignored: they are reached through later commits.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-e9265965-2056-785e-9365-c3bd2347e914
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Annot::transl_type translates `t as 'a` (typetexp.ml:394-428) as one
frame. When the alias node is entered its variable is found (a
universal variable as it is, a used variable through its instance) or
made and entered in the used variables at the alias node's location,
BEFORE the aliased type, so `array<'b> as 'b` is refused by the
kernel's unify_var as OCaml refuses it. The variable, and for a new
alias its Name (one NameTable::intern), wait on the value stack below
the aliased type's result; when the frame is left the variable is
unified with the aliased type (Alias_type_mismatch is Stop::Error) and
a new alias names its proxy without validate_name. The variable arm
looks up the universal variables before the used ones; the copy of a
generic node is one helper for both arms. The row ("int as 'a",
AliasType) of r5a_tests::DECLINED goes; the eight alias tests of
r5a2_tests are no longer ignored.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-e9265965-2056-785e-9365-c3bd2347e914
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Annot::transl_type translates `'a 'b. t` (typetexp.ml:545-571): when
the node is entered, begin_def and one variable a name (validate_name
with a leading underscore allowed, 548), each a row of
TypeVars::univars, the heads written from the last variable to the
first so that the first of two of one name is found. When the frame is
left the heads are put back from the first to the last, end_def and
generalize of the body; ONE marking walk of the body (fresh_marks,
work, children) answers deep_occur for every variable of the binder;
a marked generic variable becomes a universal variable in place, any
other marked one is Cannot_quantify; the Poly node (body, then the
bound variables in the binder's order) is generic and lowered to the
current level with Checker::lower. validate_name is one helper for the
variable arm and the binder.

On Err, transl_type now also puts the graph's level back and drops the
open binders' universal variables (10.12, decision 21). A new test
counts the steps of that drop at 1,000 and 10,000 variables. Four
tests of r5a2_tests are no longer ignored.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-e9265965-2056-785e-9365-c3bd2347e914
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…heme

Annot::globalize is globalize_used_variables (typetexp.ml:705-734) as
two loops over TypeVars::used, sorted in place by the bytes of the
names when it has two rows or more (decision 8). The first loop, in
that order, unifies a new variable at the global level with each used
variable inside one speculation, kept on success; on failure it is
restored and the unit declines with Declined::PolyType where OCaml
skips silently (10.12, decision 16). Then the declared variable of the
name, or under the fixed policy Unbound_type_variable, or a second
global variable entered as declared. The second loop, from the last
row to the first, unifies each used node with its declared variable.
The used variables are forgotten on every way out.

transl_simple_type empties the universal and used variables, translates
and globalizes; transl_type_scheme resets the item's variables,
translates one level up, gives the level back on Err too and
generalizes. Eight tests of r5a2_tests are no longer ignored.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-e9265965-2056-785e-9365-c3bd2347e914
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…LAIN_PARAMS

A constructor of a type whose row has no PLAIN_PARAMS is no longer
declined: its arguments are translated, its node made, and
Annot::unify_params runs typetexp.ml:372-389. ONE copy session of all
the declared parameters (instance_list, ctype.ml:891-894: copy_begin, a
copy_ref each, copy_drain, then the pass Checker::copy runs after its
own session), unify_var of each instance with its argument; then
enforce_constraints (ctype.ml:1333-1342): a second session at the
node's level and unify_var again, `subst`'s `!unify'` being unify_var
(1129-1130, 2767). The instances leave the value stack on both ways
out.

The doc comment of Annot::transl_type now has the alias, the
polymorphic and the constructor arms as 10.12 builds them, and the
costs of 10.12.3. r5a_tests::a_type_without_plain_parameters_is_declined
goes; arguments_of_shared_parameters_are_unified and three tests of
r5a2_tests are no longer ignored. The walk module keeps only the
dead_code allowance.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-e9265965-2056-785e-9365-c3bd2347e914
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich
jfrolich marked this pull request as ready for review November 29, 2026 16:53

This branch has not been deployed

No deployments
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.

1 participant