Skip to content

An instance of a type that is ambiguous outside its equation is refused, as in OCaml - #350

Merged
jfrolich merged 31 commits into
ocaml-to-rust-rewritefrom
fix-typechecker-ocaml-parity-10
Nov 18, 2026
Merged

jfrolich merged 31 commits into
ocaml-to-rust-rewritefrom
fix-typechecker-ocaml-parity-10

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Note

Cost accepted. CI Instruction count on 5cfe8a23f (fifth turn): base 4,136,194,490, head 4,140,293,342, +4,098,852 (+0.099%), within the job's +0.1% threshold. The split below is from 8cd9bdec6. Since then, the copies that now take the environment add guard sites: type_identifier +357,632 (was +130,678, as rue's copy tests the flag), type_application_parts +431,847 (was +398,277) and type_expect_with_recarg_unscoped +67,765. Part (a) and part (b) are otherwise unchanged.

Cost

Guard. Every read of an instance set answers nothing while all sets are empty. A set gets its first member only from expand_abbrev_gen's test (ctype.ml:1217-1227), which runs while tracing. So the type context's flag now means "a type has been added to an instance set in this unit" (has_gadt_instances). It is set in push_gadt_instance, not where a scope is created, and shares one byte with the tracing flag.

  • Copies (instance_in, instance_list_in) and level updates test the flag first and then the environment's scope handle. While the flag is false they run trunk's instance and the plain level update.
  • The expansion sites (try_expand_head, expand_head_with_mode, the unifier's expand_heads_unif and expand_head_in_unify) test the byte (expands_gadt_instances: tracing or a member), so the test that adds the first member still runs. Otherwise they run trunk's expansion and loop.
  • The newtype arm of unify runs under local constraints, which also turn tracing on.

Counts.

  • Units that add an instance scope: 434 of 788 tests/tests units, 2,169 scopes in all. Rust adds a scope in two places:
    • type_function_nary's init_env, for a function whose parameters contain a generalized constructor, and for one with a parameter that is not a plain variable (such as ()), whose patterns get an exhaustiveness check (typecore.ml:4196-4197). Most of the 434 are this second kind.
    • type_match, for a switch with a generalized constructor only (typecore.ml:4819-4828).
    • Rust adds none where OCaml's type_cases adds one for a switch without a generalized constructor that gets an exhaustiveness check (typecore.ml:4913-4914). This is recorded under A1 as a difference without a program. It can't be seen: a member is only ever added at the expansion level of an equation or at the level of a set that already holds the type, so OCaml's inner set stays empty.
  • Units that add an instance: 7 of 788.
  • Both counts come from a temporary print that is not committed.

Split of the final CI table (8cd9bdec6). "site" is the one load and one branch per site; "a" is the rest of what a unit that adds no instance pays; "b" is work while tracing or after a member.

Function Change
site the unifier's pairs (expand_heads_unif) +497,792
site instance_in / instance_list_in at typecore's sites: type_application_parts +398,277, type_identifier +130,678, type_function_nary +96,633, instance_in +65,613, instance_list_in +44,386 +735,587
site update_level_checked_above +140,409
site expand_head_with_mode's guard, inlined into moregen at its two expand_head calls (placement) +129,160
site try_expand_head +56,221
a unify: the newtype arm is inlined into it, frame 0x1e8 → 0x238 bytes +1,130,683
a expand_head_plain +12,100,981 against expand_head_with_mode −12,250,409 −149,428
a scope creation: Env::data_mut +190,067, Env::add_gadt_instance_level +89,597, make_mut +122,560 (from the fe245f217 run; this run's list is cut off before it) +402,224
a restore_type_state (the journal's new entry) +97,242
a unify_types −166,879, lower_plain_levels::lower −54,768 −221,647
b lookups: gadt_instance_level +679,023, repr_chain +642,364 +1,321,387
b try_expand_head_traced +242,590, expand_head_traced::<true> +108,040, ::<false> +64,273, Env::find_local_constraint +43,608 +458,511
b update_level_pass +89,441, instance_with_gadt_instances +45,419 +134,860
Part Change
sites +1,559,169
(a) +1,259,074 (+0.0304%)
(b) +1,914,758
rest +1,019,625 (IdTbl::find_by_ident +706,988 and the allocator; the run on fe245f217, with the same checker code, had find_by_ident at −501,294 and +5,138,728 in all)

Cost of a lookup.

  • gadt_instance_level costs one step per scope visited, plus one repr per member of those scopes, counted as added: a type added twice is two members.
  • That is linear in the members read, because repr compresses the links it follows (repr_chain).
  • OCaml's costs, per scope: a scan of the set for links, a rebuild when one is found, then a set lookup logarithmic in the distinct members.
  • Since Rust keeps duplicates, its lookup is linear in additions, not in distinct members.

Attempts (third and fourth turns). Measured locally with fat LTO, one codegen unit and five runs a side. Base is trunk 459b87c; the previous head bcc2a4ef4 measured +4,401,645.

Commit Change Total Kept
82e309f01 1. flag "a type has joined a set" +6,529,629 yes, though it is not lower: see below
cbaf8d70d 2. one instantiation of the expansion; the traced path wraps trunk's try_expand_head_with_mode, testing the ambiguity before the call +5,694,179 yes
fe245f217 3. trunk's calls at the unifier's sites while the byte is false +5,213,073 yes
89b7ae1e8 4. #[inline(never)] on unify_newtypes +4,521,654 no: reverted in 8cd9bdec6
  • Commit 1 did not lower the total, so the measuring rule says revert it. I kept it because reverting would set the flag where a scope is created, which the task counts as a failure, and the flag defines (a).
  • Its figure against bcc2a4ef4 is within the noise of the earlier measurement (CI had bcc2a4ef4's code at +7,466,613), but it also caused the unify inlining.
  • Commit 2 removed find_abbrev_expansion +8.18M and find_pdot_expansion +1.41M from the table.
  • Attempt 4's lower total came only from functions it doesn't touch (find_by_ident −802,026, the path iterator). unify itself rose from +1,130,695 to +1,914,955 because its frame grew to 0x258 bytes, so I reverted it.
  • No #[inline(always)] or #[cold] remains. Two #[inline(never)] do, on instance_with_gadt_instances and instance_list_with_gadt_instances. They come from the copy-path attempt ae16a975d of the second turn, which measured +9,118,887 against +11,678,261 before it.

What and why

OCaml keeps, per switch (or function) whose patterns may add GADT equations, the set of types known only through an equation (Env.gadt_instances, compiler/ml/env.ml:1305-1345). It refuses to expand such a type, or lower its level, below that set's level: "This instance of int is ambiguous: it would escape the scope of its equation". The Rust checker tracked none of this, so it accepted and compiled programs OCaml refuses:

type rec t<_> = Int: t<int>
let f = (type a, x: t<a>, y: a) =>
  switch x {
  | Int => if true { y } else { 0 }
  }

Both compilers now refuse it at 4:33 with the same text. The witness [y, 0] (refused at 4:16) and 28 more programs that differed agree as well. Programs that must stay accepted still are: a result annotation, (y: int), y + 1, h(y), an existential that doesn't escape, eq<a, b>, and others.

Commits

  • 92862d207 stores the instance sets and makes the expansion and level tests:
    • The sets live in the type context, one per scope; each scope holds its enclosing scope's u32 handle, and the environment's shared bindings record holds the innermost handle. Both callers of add_gadt_instance_level build the case environment as a clone of the outer one, so this form copies a u32 per clone and never a set.
    • Additions are journaled and undone on backtrack, as OCaml trails set_typeset.
    • Ported from OCaml:
      • the equation's expansion level (env.ml:1805-1810);
      • cleanup_abbrev when tracing starts (ctype.ml:182);
      • the test and the additions of expand_abbrev_gen (ctype.ml:1215-1227);
      • the chain of try_expand_head (ctype.ml:1270-1276);
      • the refusal through unify2's expansions (ctype.ml:2289-2292);
      • the update_level test (ctype.ml:587-589);
      • unify's arm for two locally abstract types (ctype.ml:2255-2268). Without it the accepted eq<a, b> program became refused, and a trace showed why: Rust lowered the copy of b to the outer level before expanding it.
  • 22a53a612 makes a copy of an instance join its set (copy ?env, ctype.ml:770-774): instance_in/instance_list_in take the environment, and the typecore sites of OCaml's instance env use them. Without this, a function generalized inside the case (let k = () => ...; k()) let the instance out.
  • 7d08707fd adds the document entry A1 in compiler-rust/docs/CHECKER_DEPARTURES.md (all 64 programs, before and after) and 12 more fixtures.
  • 752db1e00 keeps the tests off the default path: the expansion is again trunk's function returning Option; a refused node goes to an out-slot only the unifier reads; the traced end of expand_abbrev_gen is a cold function; the head-expansion loop is generic over propagating the refusal; the chain test is made only while tracing; instance and instance_in share one body.
  • c61856dea merges trunk. b46a3af77 makes a missing instance scope at an equation's level add nothing. Trunk's new checker tests build that state, which the type checker never does and where OCaml asserts; three tc::differential tests now pass unchanged.
  • c0daf094f re-records compiler-rust/tc_coverage.txt (tests/tests NotBuilt 832 -> 833: the new accepted-programs unit), with scripts/tc_coverage.py --write.
  • c8346e661 records the cost in the document.
  • Second turn: ae16a975d, c3cd63b5d, bcc2a4ef4 (copy path, head loop, chain memo), 4679c1d94 (format only), 8d0abc3bb (document).
  • Third turn: 82e309f01, cbaf8d70d, fe245f217 (above), d1faed4e5 (document: the guard, the two programs, the split).
  • Fourth turn: 89b7ae1e8 and its revert 8cd9bdec6, 43e204bef (the lookup's cost in its doc comment), f3af82e02 (document).
  • Fifth turn:
    • de5bef189: the copy sites of typecore.rs are OCaml's in both directions.
    • 66dfd8330: a record base's second copy gets trunk's call back.
    • 5f8d821f9: assert(false) copies its expected type.
    • 0684ebd56, 848fc888a, 5cfe8a23f: document.
    • 702f4bd66: cost notes, the chain's step test, the scope difference.

Decisions this PR makes

  • Where the sets live. In the type context, keyed by a scope handle stored in the environment's shared record, not in the environment itself: an Env clone then copies nothing more.
  • Set representation. A Vec per scope; a lookup compares each member's representative, as OCaml normalizes the set by repr before testing. No hash map: membership must follow links made after insertion, which a table keyed by node can't see. An addition is O(1); the chain keeps OCaml's membership test, which ends it.
  • Expansion test position. It runs before the substitution rather than after. The test reads only the expanded node, which the substitution leaves alone, so an ambiguous expansion changes nothing and the safe expansion needs no backtrack.
  • Copy site. It walks the finished copy's original-to-copy table instead of testing each node during the copy, so the default path pays no per-node branch. This adds TypeExprRefMap::for_each in type_expr.rs, a file outside the listed functions.
  • try_expand_head chain gate. Since c3cd63b5d the chain runs whenever the environment at hand has an instance scope, as OCaml runs it whatever the tracing flag (for example when the result of a switch is lowered in the outer environment). Five more programs aimed at that moment agree; all five are accepted by OCaml.

Cost

  • A unit where no type has joined an instance set pays one load and one branch per level update and per instance_in call (has_gadt_instances). Expansions pay the same, testing expands_gadt_instances.
  • Every other read and write runs only while tracing is on (under local constraints) or after a type has joined a set, in an environment with an instance scope.
  • Step tests at 10,000:
    • gadt_instance_level_is_one_step_per_member_at_ten_thousand: a lookup is one step per member visited.
    • instance_in_tests_each_copied_node_once_at_ten_thousand: one lookup per copied node; 0 steps outside local constraints.
    • gadt_instance_chain_is_linear_in_the_members_of_its_set: a chain of one type against 1,000 and 10,000 members; ten times the members is ten times the steps.
  • TypeExprRefMap::for_each has no step counter in type_expr.rs; its cost is in its doc comment.

Instruction count: see the warning above.

Copy sites of typecore.rs

OCaml's copy adds to an instance set only when it is given the environment: instance env and instance_list env (ctype.ml:877-894). instance_def and typetexp's instance_list (Ctype.instance_list Env.empty, typetexp.ml:61) add nothing. Every copy of typecore.rs, matched against its OCaml line:

Kind Sites
with the environment (instance_in, instance_list_in, instance_if_generic_in), as OCaml's instance env / instance_list env / typetexp's instance env 61
without it (instance, instance_list), as OCaml's instance_def / typetexp's instance_list 19
no OCaml counterpart, trunk's call kept 4
  • rue is one site (2607, rue_unify), shared by its 21 callers.
  • instance_if_generic_in copies only a generic type. OCaml's copy returns any other type as is and adds nothing for it (ctype.ml:750).
  • Three OCaml instance env sites have no Rust copy:
    • is_ignore and the ignore case of type_application (typecore.ml:2498, 4496): Rust tests the primitive by name and copies nothing.
    • The let with locally abstract types (typecore.ml:5092): Rust types the rewritten expression, whose rue makes that copy.
All 84 sites
Rust line Rust call Construct OCaml line OCaml form
2376 instance constant typecore.ml:340 instance_def
2377 instance constant 341 instance_def
2378 instance constant 342 instance_def
2379 instance constant 343 instance_def
2380 instance constant 344 instance_def
3191 instance_in type variable: univar typetexp.ml:308 instance env
3193 instance_in type variable typetexp.ml:310 instance env
3329 instance_list type constructor parameters typetexp.ml:373 typetexp's instance_list
3407 instance_in alias typetexp.ml:400 instance env
3445 instance_in alias typetexp.ml:417 instance env
7540 instance_in pattern constraint typecore.ml:1869 instance env
7541 instance_in pattern constraint 1869 instance env
8504 instance_if_generic Pexp_tuple none: 2940 unifies ty_expected itself —
8743 instance_in assert(false) 3780 instance env
8792 instance_in Pexp_array 3242 instance env
8835 instance_in Pexp_dict 3264 instance env
8880 instance_in Pexp_spread 3288 instance env
8916 instance Pexp_template 2769 instance_def
8951 instance_in Pexp_tagged_template 2782 instance env
9375 instance_in Pexp_ident 2660 instance env
9533 instance_in rue 2607 instance env
10330 instance_in lowered apply: expected 2825 instance env
10340 instance_in lowered apply: function 2826 instance env
10415 instance_in application: the function's type, once (OCaml copies each argument and the result) 4575, 4591, 4659-4660 instance env
10528 instance_in omitted optional argument 4598 instance env
10829 instance_in switch result, unused check 4937 instance env
11069 instance_in unused check 4211, 4927 instance env
11598 instance_in case pattern 4848 instance env
11600 instance_in case pattern 4848 instance env
11688 instance_in unify_pats 4869 instance env
11690 instance_in unify_pats 4869 instance env
11779 instance_in case body 4906 instance env
12076 instance_in switch with GADTs: shared result 4911 instance env
12112 instance_in switch result 4937 instance env
12144 instance_in switch result 4937 instance env
12177 instance switch exception cases none: Predef.type_exn is passed as is —
12210 instance_in Pexp_match result 2905 instance env
12982 instance_in case pattern 4848 instance env
13101 instance_in case body 4906 instance env
13146 instance try handler none: Predef.type_exn is passed as is —
13596 instance_in function 3992 instance env
13710 instance_in function parameter 4065 instance env
13794 instance_in function parameter 4076 instance env
13904 instance_in function body 4169 instance env
13909 instance_in function result 4172 instance env
14011 instance_in function unused check 4233 instance env
14016 instance_in function unused check 4235 instance env
14628 instance constructor 4739 instance_def
14629 instance_in constructor 4740 instance env
14643 instance_list_in constructor 4745 instance_list env
14648 instance_in constructor 4750 instance env
14728 instance_in polymorphic variant 2968 instance env
15389 instance_if_generic_in record: expected type 3044, 3148 instance env
15446 instance_list_in record: declaration parameters 3126 instance_list env
15874 instance record label 4315 instance_def
15875 instance_in record label 4315 instance env
15893 instance record label 4319 instance_def
15987 instance_in record label 4338 instance env
16040 instance_in record label 4359 instance env
16058 instance_in record: expected type (the copy above, not generic, is returned) 3044, 3148 instance env
16130 instance_in record base 3153 instance env
16171 instance_in record base, kept labels 3161 instance env
16194 instance record base, second copy none: 3165 stores the 3153 copy —
16218 instance_in record result 3102, 3189 instance env
16888 instance field assignment label 4315 instance_def
16889 instance_in field assignment label 4315 instance env
16902 instance field assignment label 4319 instance_def
16956 instance_in field assignment label 4338 instance env
16980 instance_in field assignment label 4359 instance env
17068 instance_in Pexp_constraint 3551 instance env
17069 instance_in Pexp_constraint 3552 instance env
17185 instance_if_generic_in Pexp_coerce (rue) 2607 instance env
17541 instance_in object field read 2553 instance env
17716 instance_in object field value 4293 instance env
18728 instance_in Pexp_pack 3794 instance env
19183 instance_in wrap_unpacks identifier 2660 instance env
21279 instance_in let result 5102 instance env
22612 instance operator result 4381-4483 instance_def
22617 instance operator result 4381-4483 instance_def
22622 instance operator result 4381-4483 instance_def
22627 instance operator result 4381-4483 instance_def
22632 instance operator result 4381-4483 instance_def
22637 instance operator result 4381-4483 instance_def
22642 instance operator result 4381-4483 instance_def

Programs for the corrected sites. I tried 21 programs, each in a case with Int: t<int>, through both compilers on the head before de5bef189 and on the final head:

  • (a) rue and the other instance env sites, 15 programs: a function generalized in the case, passed through a constructor, a record field, a coercion, an object field, a tuple, Some(g), a polymorphic field and a destructuring let; Box(y, 0); nested constructors; labels {p: y, q: 0}; and assert(false) in a branch and in a generalized record field.
  • (b) the label sites, 6 programs: a record declared in a local module inside the case with a field v: a, built with 0 or y, assigned m.v = 0, read after the case and behind an if, beside a second field w: int, and beside a type parameter.
OCaml Rust before Rust after
(a), 15 programs 5 refused ("This instance of int is ambiguous"), 10 accepted same, byte for byte same, byte for byte
(b), 6 programs 3 refused, 3 accepted same same

None is a witness, so none became a fixture. The sites are corrected because the copy must do what OCaml's does.

Open

No program of the 64 shows a difference at these OCaml sites, which are not ported (the port stopped there, at the task's size limit):

  • the chain of unify2 after expansion (ctype.ml:2293-2298);
  • the level tests in unify_fields (ctype.ml:2490) and on unify_row's row variable (ctype.ml:2609-2611);
  • the tracing wrappers of unify_var (2759-2766), expand_head_trace (2782-2784), the application's lower_args (typecore.ml:2828) and local_non_recursive_abbrev (1533-1534);
  • instance ~partial env.
  • The scope OCaml's type_cases adds at typecore.ml:4913-4914 (see Counts), a difference without a program.
  • The three OCaml instance env sites with no Rust copy (see the copy-site table).

The probes written for each of them agree; they are listed in the document. The printer arm was not touched: its condition is OCaml's.

Verification

All 64 programs ran through both compilers, Rust before (trunk 109b2bd4c) and after. Summary (the full table is in the document):

  • 30 programs differed before, all of them Rust accepting what OCaml refuses. All 30 agree after, byte for byte: diagnostic, location, exit status.
  • 34 agreed before and still agree, 31 of them accepted with the same JavaScript and no warnings.

Fixtures: 30 super_errors gadt_instance_ambiguous_* (snapshots produced by the OCaml bsc) and tests/tests/src/gadt_instance_ambiguous_accepted.res (31 accepted programs).

Gates (on 5f8d821f9, the last code commit; the commits after it change only the document)

Programs: 26/26 agree with OCaml. They are the 21 above, three exhaustiveness checks of a switch with a generalized type under _, and t01/t02 aimed at the guard.

Note

The sandbox was reset before this turn, which deleted the scratch copies of the earlier 64 + 5 programs. Of the 64:

  • The 30 refused ones are the super_errors fixtures.
  • 31 of the accepted ones are gadt_instance_ambiguous_accepted.res.
  • Both are compared by the parity suites below.
  • The other 3 of the 64 and the 5 aimed at the chain were not rerun.

./scripts/test_error_output_parity.sh --release:

Error output parity summary
  total:  2267
  pass:   2267
  fail:   0

./scripts/test_js_output_parity.sh --release:

Total: 783 / 783 tests passed (100.0% parity)

cargo clippy --locked --manifest-path compiler-rust/Cargo.toml --all-targets -- -D warnings: exit 0. RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --manifest-path compiler-rust/Cargo.toml: exit 0.

cargo test --manifest-path compiler-rust/Cargo.toml --lib tc::differential:

test result: ok. 65 passed; 0 failed; 1 ignored; 0 measured; 2946 filtered out; finished in 23.19s

cargo test --lib gadt_instance: 2 passed, including the new chain step test.

cargo test --lib types:: has 367 passed and 2-3 failures in types::module_view ("shared type index refers to an unpublished page"). They pass alone, and trunk 109b2bd4c fails the same way in the same run (366 passed; 2 failed, same tests, same message), so this is a pre-existing race between parallel tests.

On a 3,735-module application, scripts/compare_checker_builds.py between trunk and this branch:

signatures: no difference (3735 compared)
diagnostics: no difference (18 compared)
javascript: no difference (3980 compared)
no differences

tests/ERROR_VARIANTS.md is unchanged: no error variant was added or removed.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 13 commits November 18, 2026 00:04
Why:
    OCaml records, per `switch` whose patterns may add equations, the
    types known only through an equation (`Env.gadt_instances`,
    compiler/ml/env.ml:1305-1345), and refuses to expand or lower one
    below that level (ctype.ml:587-589, 1215-1227). Rust did not track
    them (`Env::add_gadt_instance_level` took no level), so it accepted
    and compiled programs OCaml refuses.

Typing modified (expanding an equation's type below its switch):
Example:
    type rec t<_> = Int: t<int>
    let f = (type a, x: t<a>, y: a) =>
      switch x {
      | Int => if true { y } else { 0 }
      }
Before (diagnostic): accepted; `function f(x, y) { return y; }`
After (diagnostic):
      We've found a bug for you!
      Main.res:4:33

      This has type: int
      But this if statement is expected to return: a (defined as int)
      This instance of int is ambiguous:
      it would escape the scope of its equation

      if expressions must return the same type in all branches (if, else if, else).

The instance sets live in the type context, one per scope, each holding
its enclosing scope's handle; the environment holds the innermost
handle (`EnvData::gadt_instances`), so cloning an environment copies a
`u32` and no set. Additions are journaled and undone on backtrack, as
OCaml trails `set_typeset`. Ported: the expansion level of an equation
(env.ml:1805-1810), `cleanup_abbrev` when tracing starts (ctype.ml:182),
the test and the additions of `expand_abbrev_gen` (ctype.ml:1215-1227),
the chain of `try_expand_head` (ctype.ml:1270-1276), `unify2`'s
expansions failing with the pair (ctype.ml:2289-2292), the test of
`update_level` (ctype.ml:587-589), and the arm of `unify` for two
locally abstract types (ctype.ml:2255-2268), without which an accepted
`eq<a, b>` program became refused.

Review: What does a program without GADTs pay? The test in the level
update is one load of `has_gadt_instance_scopes` and a branch; every
other read and write runs only while tracing is on (under local
constraints) or in an environment with an instance scope.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Why:
    OCaml's `copy ?env` (compiler/ml/ctype.ml:770-774), reached through
    `instance env` and `instance_list env` (ctype.ml:879-894), adds the
    copy of a node in an instance set to that set when the environment
    has local constraints. Rust's `instance` takes no environment, so a
    function generalized inside a case gave copies that were no
    instances, and its result left the case.

Typing modified (instantiating a generalized instance below its switch):
Example:
    type rec t<_> = Int: t<int>
    let f = (type a, x: t<a>, y: a) =>
      switch x {
      | Int => {
          let k = () => if true { y } else { 0 }
          k()
        }
      }
Before (diagnostic): accepted; `function f(x, y) { return y; }`
After (diagnostic):
      Main.res:6:7-9

      This has type: a (defined as int)
      But it's expected to have type: 'a
      This instance of int is ambiguous:
      it would escape the scope of its equation

`instance_in` and `instance_list_in` take the environment; the type
checker's `instance env` sites use them (constants and predefined types
keep `instance`, OCaml's `instance_def`). The copy is made as before;
then, only in an environment with local constraints and an instance
scope, the copy table is walked once and the copy of each instance is
added. Without a scope in the unit an `instance` call pays one load and
one branch.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The third round of CHECKER_DEPARTURES.md gets the place A1: the 64
programs run through both compilers before and after the two commits
of the port, the causes on both sides, and the OCaml sites left open
because no program shows them. The twelve probes that differed on trunk
and had no fixture become super_errors fixtures.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Why:
    The instruction count of a clean tests/tests build rose by 0.313%
    with the port: the expansion of a head returned a `Result` through
    two more call layers, which changed what was inlined
    (`try_expand_head_with_mode` -29.4M, `try_expand_head_traced`
    +23.6M, `expand_head_traced` +15.1M, `find_abbrev_expansion` +8.2M
    instructions), and every `instance_in` call tested for an instance
    scope before copying (+2.3M).

The expansion of one head is again trunk's function returning an
`Option`; an ambiguous instance is written to an out-slot that only the
unifier reads, and the traced end of `expand_abbrev_gen` is a cold
function reached only while tracing. The head-expansion loop is one
function, generic over whether it propagates the refusal, so the safe
instantiation is trunk's loop. The test of `try_expand_head`'s chain is
made only while tracing: one load and one branch outside it. `instance`
and `instance_in` share one body; the instance-scope test follows a copy
of a generic root only. `unify` reuses its local-constraints test for the
newtype arm. All 64 programs of A1 agree as before.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Why:
    The checker tests on trunk drive the old checker's pattern
    unification on an environment that has an equation (expansion
    level 999) but no instance scope, which the type checker never
    builds: both callers of `Env::add_gadt_instance_level` add the scope
    before the patterns that add equations. `add_gadt_instances`
    panicked there, where OCaml's `List.assoc` asserts
    (compiler/ml/env.ml:1326-1328). It now adds nothing;
    `tc::differential::tests::old_checker_gives_the_equation_to_the_newer_locally_abstract_type`,
    `generated_cases_find_a_pattern_unification_in_expression_mode` and
    `old_side_of_the_pattern_plan_is_not_vacuous` pass unchanged.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
tests/tests gained gadt_instance_ambiguous_accepted, one more unit the
new checker declines (NotBuilt 832 -> 833); recorded with
`scripts/tc_coverage.py --rescript rewatch/target/release/rescript --write`.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The instruction counts the CI check measured, by function, and the
line numbers after the expansion was moved off the default path.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
`instance` and `instance_list` are trunk's again. The `instance env`
sites test the unit's flag and then the environment at hand; only when
it has an instance scope do they call the copy that walks the finished
copy table (`instance_with_gadt_instances`, not inlined).

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…as an instance scope

Trunk's head expansion runs unchanged outside an instance scope. In an
environment with one, a traced loop calls the traced instantiation of
the expansion, which makes the test of expand_abbrev_gen, and adds
try_expand_head's chain whenever the result is an instance, as OCaml
does whatever the tracing flag. The unifier's expansions take the same
choice.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The chain of try_expand_head (compiler/ml/env.ml:1342) has its own scan
of the memorized expansions, so find_abbrev_expansion keeps trunk's one
caller, the head expansion, and is inlined there as on trunk.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
rustfmt's layout for the lines the three attempts changed; no code
change.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich
jfrolich force-pushed the fix-typechecker-ocaml-parity-10 branch from 383798e to 8d0abc3 Compare November 18, 2026 02:05
open-session-submodule Bot and others added 8 commits November 18, 2026 02:12
Why:
    An instance scope exists inside almost every `switch` of more than
    one case: OCaml's `type_cases` adds the level whenever the
    exhaustiveness check will run (compiler/ml/typecore.ml:4814-4818,
    4913-4914), so 434 of the 788 units of tests/tests add one. Every
    read of a set is a no-op while every set is empty, and a set gets
    its first member only from `expand_abbrev_gen` (ctype.ml:1217-1227)
    under the tracing flag.

The type context's flag is now "a type has been added to an instance
set in this unit", set where a member is pushed, not where a scope is
created, and kept in one byte with the tracing flag. Copies and level
updates test it; the expansion sites test the byte (tracing or a
member), so the test that adds the first member still runs. Without
either, every site runs trunk's code after one load and one branch.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
… copy

The traced expansion was a second instantiation of
`try_expand_head_with_mode`, so `find_abbrev_expansion` and
`find_pdot_expansion` had two callers and were no longer inlined into
trunk's. `try_expand_head_traced` now wraps trunk's function: while
tracing and with no memorized expansion, it looks up the equation and
its expansion level, refuses an ambiguous instance before expanding
(nothing to undo), and otherwise records the type and its expansion
in their instance set after it. A type that is an instance through
another route is tested after the expansion, whose memo is then
forgotten as OCaml's `try_expand_safe` backtracks it. The memo test is
a scan of its own (`has_memorized_expansion`).

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
… can be expanded

`expand_head_for_unify` returned a `Result` from both arms and tested
the guard once per side. The pair sites now test it once
(`expand_heads_unif`) and, while it is false, make trunk's two
expansions directly; the traced pair, which refuses an ambiguous
instance, is a separate function. The fully expanded comparison's
site (`expand_head_in_unify`) does the same for one side.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Corrects why 434 units add an instance scope: type_cases adds one for
every switch whose exhaustiveness is checked.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Inlined, it grew unify's frame from 0x1e8 to 0x238 bytes, so every
call spilled more registers.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…+1,914,955)

This reverts commit 89b7ae1. Measured locally against trunk
459b87c (fat LTO, one codegen unit, five runs a side), the total fell
from +5,213,073 to +4,521,654, but only through functions the commit
does not touch (`IdTbl::find_by_ident` -802,026, the path iterator),
which move by as much between runs. `unify` itself rose from +1,130,695
to +1,914,955: with `#[inline(never)]` on `unify_newtypes` its frame
grows from 0x238 to 0x258 bytes (trunk: 0x1e8), so the attribute adds
to what a unit without an instance pays.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
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 18, 2026 03:23
open-session-submodule Bot and others added 2 commits November 18, 2026 04:25
… link to a private item

The documentation build refused the link from a public function to a
private one. The sentence about the guard now says what the guard is: a
type has joined an instance set in this unit.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
No change to the tree. The port measures about +0.12 to +0.14% on the
tests/tests build: one load and one branch at each copy, expansion and
level-update site while no type has joined an instance set, a larger
frame in unify, and the instance lookups OCaml also makes in the units
that add an instance. The split by function is in the pull request and
under A1 of CHECKER_DEPARTURES.md.

Instruction-count-increase: GADT instance tracking as OCaml has it; guard tests at the ported sites and the instance work in units that add an instance, split by function in the pull request
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich

Copy link
Copy Markdown
Owner Author

First-pass checklist review at ed5a1a8 (a checklist for the final reviewer, not an approval).

First-pass review (checklist, not an approval): pull request 350, fix-typechecker-ocaml-parity-10 into ocaml-to-rust-rewrite. The head ed5a1a8 is an empty commit on top of d5f63f6 (git diff --stat d5f63f64b8 origin/fix-typechecker-ocaml-parity-10 prints nothing). Diff: D=origin/ocaml-to-rust-rewrite...origin/fix-typechecker-ocaml-parity-10; git diff $D > $TMPDIR/pr.diff, 70 files, +2478/−98. Branch files are read with git show origin/fix-typechecker-ocaml-parity-10:<path>. Line numbers marked "new" are the branch's.

1. Scope

The task is to port OCaml's GADT instance sets (env.ml 1305-1345) and the ctype.ml sites that read or add to them, under rule (i)–(iii).

Item Where in the diff
sets (gadt_instances, add_gadt_instance_level, gadt_instance_level, add_gadt_instances, add_gadt_instance_chain) context.rs new 5754-5880; env.rs 2805-2820
update_level 584-590 ctype.rs update_level_pass new 2999-3006, update_level_checked_above 2757-2762
copy 770-774 ctype.rs instance_in/instance_list_in/add_copied_gadt_instances new 12530-12625
expand_abbrev_gen 1215-1227 ctype.rs try_expand_head_traced new 5098-5163
try_expand_head 1270-1276 ctype.rs expand_head_traced new ~7464-7508
unify 2235, 2255-2270 ctype.rs unify 8314-8330, unify_newtypes new 610-644
unify2 expansions expand_heads_unif / expand_head_in_unify at the 4 sites in unify_types
check_trace_gadt_instances + cleanup_abbrev 178-184 context.rs 5693-5699 (clear_abbrev_memos)
expansion level env.ml:1805-1810 ctype.rs add_gadt_equation 1839
flag (i) context.rs GADT_MEMBERS

Not done, as the body states under "Open": unify2's chain (ctype.ml 2289-2298), unify_fields 2490, unify_row 2609-2611, the tracing wrappers (unify_var, expand_head_trace, lower_args, local_non_recursive_abbrev), instance ~partial env, and the update_level calls Rust makes without an environment.

Done but not asked for:

  • TypeExprRefMap::for_each (type_expr.rs).
  • has_memorized_expansion (context.rs).
  • expands_to_datatype now calls the guarded try_expand_head (ctype.rs 872).
  • A new panic! in unify_gadt_equation (ctype.rs 1802-1804).

Line-number note: the body cites "unify2's expansions (ctype.ml:2289-2292)" and leaves 2293-2298 open. In this tree the expansions are 2279-2282, and 2289-2298 is one block, the traced chain. See 14(f).

PASS (every asked item is done or listed as open).

2. Default path

These changed functions run in an ordinary build. "In table" means the body's cost split has a row for it.

Function What changed In the body's table
ctype update_level_checked_above guard before lower_plain_levels yes, +140,409
ctype update_level_pass guarded gadt_instance_level call yes, (b) +89,441
ctype try_expand_head guard, then trunk's call yes, +56,221
ctype expand_head_with_mode guard, then expand_head_plain (trunk's body) yes, ±12.2M move
ctype expands_to_datatype now calls try_expand_head no
ctype unify has_local_constraints hoisted; unify_newtypes call yes, +1,130,683 (inlining)
ctype unify_types 3 expand_heads_unif sites and 1 pair of expand_head_in_unify yes
ctype unify_gadt_equation / add_gadt_equation newtype_level threaded, new panic! no
context check_trace_gadt_instances now clear_abbrev_memos() (clones a Vec of memo refs) each time tracing turns on no
context restore_type_state new journal arm yes, +97,242
env Env::add_gadt_instance_level allocates a scope; data_mut yes, +402,224
ctype instance_in / instance_list_in 63 typecore.rs sites yes

The three "no" rows are in the guard or under local constraints, but no figure is reported for them.

PASS (each is listed; three without a measurement).

3. Engineering standard

The greps run over the 802 added lines of compiler-rust/src ($TMPDIR/added.txt, built from pr.diff).

(a) Cost in the doc comment. Present for is_newtype, unify_newtypes, gadt_instance_level (ctype and context), try_expand_head_traced, expand_head_traced ("At most 20 expansions"), expand_heads_unif, expand_head_in_unify, instance_in, the two guards, instance_with_gadt_instances, add_copied_gadt_instances, new_gadt_instance_scope, gadt_instance_scope_at, push_gadt_instance, add_gadt_instances, add_gadt_instance_chain, has_memorized_expansion, Env::add_gadt_instance_level, gadt_instance_scope and for_each. Missing for:

  • expand_head_plain ("Trunk's head expansion");
  • expand_heads_traced ("The traced expansions of");
  • ambiguous_instance (allocates a vec! and a variable);
  • instance_list_in and instance_list_with_gadt_instances ("as instance_in");
  • GadtScopeRef::is_none / index (trivial).

(b) Step tests at 10,000. Two exist:

  • gadt_instance_level_is_one_step_per_member_at_ten_thousand (context.rs) asserts (None, 10_000) and (Some(5), 10_000), and that a member linked to another type answers for its representative.
  • instance_in_tests_each_copied_node_once_at_ten_thousand (ctype.rs) asserts 1 + 2 * 9_999 steps, and 0 outside local constraints.

No step test for:

  • add_gadt_instance_chain: a members.contains per chain cell, so O(chain × members);
  • gadt_instance_scope_at;
  • has_memorized_expansion;
  • TypeExprRefMap::for_each (the body says so).

The copy test has one member. With k members, a copy of n nodes costs n·k.

(c) No HashMap, HashSet or BTreeMap added (grep: none).

(d) Allocations:

  • context.rs 5763 members: Vec::new(): one per scope, 2,169 in tests/tests.
  • ctype.rs 7457 vec![(ty, new_var…)]: error path.
  • check_trace_gadt_instances → clear_abbrev_memos (context.rs 3889, .borrow().clone() of a Vec): existing code, newly called.

None inside a per-node walk on the default path.

(e) One RefCell added: context.rs 879/1442 gadt_instance_scopes: RefCell<Vec<GadtInstanceScope>>. The state byte gadt_state is a Cell<u8>. No lock, atomic or thread_local.

(f) A second pass: add_copied_gadt_instances walks the finished copy table instead of testing during the copy. The body states this choice ("Copy site"). It runs only behind the member flag, a scope and local constraints.

(g) No size_of test for GadtScopeRef (4 bytes; passed by value, stored in EnvData) or GadtInstanceScope (≈32 bytes; one per scope, not per node). Nothing above 16 bytes is passed by value.

(h) No allow(, unsafe, todo!, unimplemented! or #[ignore] added. Added:

  • unreachable! at ctype.rs 7361 and 16291 (test);
  • panic! at ctype.rs 1803;
  • .expect( at context.rs 5759 (u32 overflow) and ctype.rs 16290 (test).

UNCLEAR: the cost comments listed in (a) are missing, and the chain has no step test.

4. Tests and oracles

git diff --diff-filter=M --name-only $D -- tests prints nothing, and --diff-filter=D prints nothing. The one ratchet change:

-tests/tests NotBuilt 832
+tests/tests NotBuilt 833

This is a count of declined units that may not rise (scripts/tc_coverage.py: "fail if any corpus/reason count rises"). It was raised by one, re-recorded with --write for the new unit gadt_instance_ambiguous_accepted.res. That is a re-recorded expectation, explained in commit c0daf09.

Three src/tc tests are said to "pass unchanged" because of b46a3af; no test file under src/tc is in the diff (section 11). Two existing tests in context.rs and one in env.rs change only the call add_gadt_instance_level() → add_gadt_instance_level(&ctx, 1); no assertion changed.

PASS (one re-recorded ratchet, justified by a new unit).

5. New scripts and fixtures

No new script. The 30 super_errors/fixtures/gadt_instance_ambiguous_*.res files and their expected files are read by scripts/test_error_output_parity.sh (SUPER_ERRORS_DIR=…/super_errors/fixtures, line 102). CI runs that in rust-rewrite-validation.yml:639, and the super_errors build test reads them too. tests/tests/src/gadt_instance_ambiguous_accepted.{res,mjs} is built by the tests/tests build and test_js_output_parity.sh (rust-rewrite-validation.yml:631). Every file is added in the diff, so tracked.

PASS

6. Privacy

grep -n -i -E "tella|submodule\.nl|rescript-bench-corpus|/Users/|opensession|jake" $TMPDIR/pr.diff: no hits. The body's only hit is the attribution footer. "a 3,735-module application" is unnamed.

PASS

7. Changelog

changes/gadt-instance-ambiguity.fix.md starts with "- Refuse, as the OCaml compiler does, …". CHANGELOG.md is not in the diff.

PASS

8. Numbers

Figure Command and output shown?
base 4,135,460,618, head 4,141,213,244, +5,752,626 (+0.139%) yes; matches CI run 44228058456, compare job, "run 3" and the table
the function rows of the split yes; match that run's table, except make_mut +122,560, which the body says comes from the fe245f2 run
part sums (+1,559,169; +1,259,074; +1,914,758; rest +1,019,625) arithmetic checks out
local runs (+4,401,645, +6,529,629, +5,694,179, +5,213,073, +4,521,654) no command or output; only "fat LTO, one codegen unit, five runs a side"
434/788 units, 2,169 scopes, 7/788 no; "a temporary print that is not committed"
2267/2267, 783/783, tc::differential 65 passed, 3,735-module compare output shown
clippy "exit 0", no output
types:: 367 passed, 2-3 failures prose only

The document disagrees with the body by a few thousand on some rows, because it takes the fe245f2 run:

Row Document Body
(a) +1,259,086 +1,259,074
(b) +1,918,108 +1,914,758
find_local_constraint +46,958 +43,608
unify +1,130,695 +1,130,683

UNCLEAR: the local and count figures have no output.

9. Focused tests

Not run (host review; cargo is not run here).

UNCLEAR

10. Where to look hardest

  1. The copy sites of typecore.rs (section 17). rue_unify (typecore.rs new 9494, 22 callers) still calls instance where OCaml's rue (typecore.ml:2607) calls instance env ty_expected, and so does object_field_use_type. Meanwhile five sites that OCaml writes instance_def and one typetexp instance_list without an environment were converted. OCaml's copy adds instance copies only through instance env, so a missed site can accept what OCaml refuses, and an extra one can refuse what it accepts. No fixture targets them.
  2. Set growth. add_gadt_instances appends without testing membership (OCaml's Type_set.add deduplicates). Every traced expansion of the same instance pushes [ty, ty'] again, and lookups are linear in additions. A long case body in one of the 7 instance units can go quadratic. No step test covers repeated additions or the chain's contains.
  3. unify_newtypes and the unify frame. It runs behind has_local_constraints, not the member flag (section 15). Its inlining costs every unit +1.13M in unify (part (a)). The total stays above the job's +0.1% threshold and is accepted by the trailer.

11. Files

git diff --stat=200 $D -- compiler-rust/src compiler-rust/tc_coverage.txt:

compiler-rust/src/types/context.rs   | 269 ++-
compiler-rust/src/types/ctype.rs     | 450 ++-
compiler-rust/src/types/env.rs       |  25 +-
compiler-rust/src/types/type_expr.rs |  24 +
compiler-rust/src/types/typecore.rs  | 131 +-
compiler-rust/tc_coverage.txt        |   2 +-

No file under compiler-rust/src/tc (git diff --stat $D -- compiler-rust/src/tc is empty) and no other source file. b46a3af touches only context.rs: gadt_instance_scope_at returns Option<GadtScopeRef> instead of panic!("Env.add_gadt_instances: no instance scope at level {level}"), and both adders let Some(scope) = … else { return; };. No test assertion was edited.

PASS

12. The sets against env.ml 1305-1345

OCaml (env.ml):

let add_gadt_instance_level lv env =
  {env with gadt_instances = (lv, ref Type_set.empty) :: env.gadt_instances}
let gadt_instance_level env t =
  let rec find_instance = function
    | [] -> None
    | (lv, r) :: rem ->
      if Type_set.exists is_Tlink !r then
        r := Type_set.fold (fun ty -> Type_set.add (repr ty)) !r Type_set.empty;
      if Type_set.mem t !r then Some lv else find_instance rem
  in find_instance env.gadt_instances
let add_gadt_instances env lv tl =
  let r = try List.assoc lv env.gadt_instances with Not_found -> assert false in
  set_typeset r (List.fold_right Type_set.add tl !r)
let add_gadt_instance_chain env lv t = … let rec add_instance t = let t = repr t in
    if not (Type_set.mem t !r) then (set_typeset r (Type_set.add t !r);
      match t.desc with Tconstr (p, _, memo) -> may add_instance (find_expans Private p !memo) | _ -> ())

Rust (context.rs):

pub(crate) fn gadt_instance_level(&self, scope: GadtScopeRef, ty: TypeExprRef) -> Option<i32> {
    … while !current.is_none() { let entry = &scopes[current.index()];
        for &member in &entry.members { … if self.repr(member) == ty { return Some(entry.level); } }
        current = entry.parent; } None }
fn add_gadt_instances(&self, scope, level, types) {
    let Some(scope) = self.gadt_instance_scope_at(scope, level) else { return; };
    for &ty in types { self.push_gadt_instance(scope, ty); } }
pub(crate) fn add_gadt_instance_chain(&self, scope, level, ty) { … loop: let ty = self.repr(ty);
    if members.contains(&ty) { return; } self.push_gadt_instance(scope, ty);
    current = Tconstr path's memo scan `path.same(cached_path).then_some(expansion)` }
OCaml Rust Verdict
add_gadt_instance_level: a new scope at the list head new_gadt_instance_scope(level, parent): a new entry whose parent is the old head SAME
gadt_instance_level: innermost first; a type in two scopes answers the inner level scope → parent; first hit returns SAME
add_gadt_instances: the set of exactly lv; assert false when missing gadt_instance_scope_at (first scope with level == lv, innermost first); missing → adds nothing (b46a3af) SAME except the missing case
chain stop: Type_set.mem t !r on the raw set members.contains(&ty): raw node equality, not repr of members SAME (OCaml does not normalize here either)
chain step: find_expans Private p !memo (any privacy) scan_abbrev_chain(…path.same…), privacy ignored SAME
set vs Vec: duplicates OCaml deduplicates; Rust appends same answers, different cost

Membership after links:

  • A member M later linked to X: OCaml rebuilds the set with repr, so X answers; Rust compares repr(M) == X. Same.
  • A type Y later linked to member M: the callers pass repr(Y) = M (ctype gadt_instance_level does ctx.repr(ty)), which is found in both.

I found no case where the answers differ. Neither side follows Tsubst (is_Tlink only), and Rust tests after the copy has finished.

Cost:

  • One lookup is O(scopes + additions) reprs, as the doc comment says.
  • One chain addition is O(scopes) plus, per chain cell, O(members) for contains and one memo-chain walk. The doc says "per type of the chain, one step per member and one walk of the constructor's memo chain": consistent.

PASS (same answers; duplicate growth noted in section 10).

13. Scope creation and what an environment holds

Old (env.rs):

/// Rust does not track GADT instances, so only the fresh scope identity …
pub fn add_gadt_instance_level(&mut self) { self.bump_scope(); }

New:

pub fn add_gadt_instance_level(&mut self, ctx: &TypeContext, level: i32) {
    self.bump_scope();
    let parent = self.data.gadt_instances;
    self.data_mut().gadt_instances = ctx.new_gadt_instance_scope(level, parent);
}

The handle is pub struct GadtScopeRef(u32) (NONE = u32::MAX), a field of EnvData. An Env clone copies the shared record pointer, and data_mut copies the record (one u32 field more). No set is copied. PASS

Callers:

Rust OCaml Verdict
typecore.rs 13619-13626 init_env in type_function_nary, called for has_gadts (13630) and do_init && !has_gadts (13911-13912) init_env typecore.ml:4034-4042, do_init 4196-4197 SAME level (lev = current_stamp after set_current_time)
typecore.rs 11976 in type_match (has_generalized_value_patterns) typecore.ml:4819-4828 if has_gadts then init_env () SAME
missing typecore.ml:4913-4914 if do_init && not has_gadts then init_env () for a switch DIFFERENT

In Rust's type_match (new 12078-12105) and finish_non_gadt_type_cases (10775-10793), the late needs_exhaust_check branch does begin_def/set_current_time/init_def(lev + 1000) but calls no add_gadt_instance_level. The empty scope is never reached by an equation, so I don't expect a visible difference. But the body and CHECKER_DEPARTURES.md explain the 434 units by "OCaml's type_cases adds an instance level for a switch … (typecore.ml:4814-4818, 4913-4914; Rust typecore.rs at the needs_exhaust_check test)", and Rust does not add one there. The 434 can only come from type_function_nary and the has_gadts switches.

UNCLEAR: the Rust behaviour is likely equivalent; the stated reason for the count is inaccurate.

14. Ported reads and writes in ctype.rs

(a) update_level, ctype.ml:586-589:

if ty.level > level then (
  (match Env.gadt_instance_level env ty with
  | Some lv -> if level < lv then raise (Unify [(ty, newvar2 level)]) | None -> ());
  match ty.desc with …

Rust update_level_pass:

if level < current {
    if let Some(instance_level) = gadt_instance_level(ctx, env, ty) && level < instance_level { return Err(ty); }
    let desc = ctx.borrow_desc(ty); match &*desc { …

The caller maps Err to TypeError::Unify(vec![(escaping, new_var_at_level(ctx, level))]) after OCaml's two-pass retry (ctype.ml:631-638), which Rust also makes. Same node, before the constructor tests, same pair. SAME

(b) The copy, ctype.ml:770-774:

(match env with Some env when Env.has_local_constraints env -> (
   match Env.gadt_instance_level env ty with Some lv -> Env.add_gadt_instances env lv [t] | None -> ()) | _ -> ())

Rust add_copied_gadt_instances does, for each (original, copy) of cache.nodes, gadt_instance_level(scope, repr(original)) and then add_gadt_instances(scope, level, &[copy]). It requires env.has_local_constraints(). Each copied node that is a member joins the set of its own level. SAME (made after the copy, which cannot change membership).

(c) expand_abbrev_gen, ctype.ml:1217-1227:

let ty' = subst env level kind abbrev (Some ty) params args body in
(if !(trace_gadt_instances ()) then
   match Ext_pervasives.max_int_option lv (Env.gadt_instance_level env ty) with
   | None -> () | Some lv -> if level < lv then raise (Unify [(ty, newvar2 level)]);
                             Env.add_gadt_instances env lv [ty; ty']);

Rust try_expand_head_traced:

  • returns trunk's result when not tracing or when the memo holds an expansion;
  • with an equation, expansion_level.max(set_level) and level < instance_level → Err(ty) before try_expand_head_with_mode;
  • otherwise tests after, and on refusal calls forget_abbrev;
  • then add_gadt_instances(scope, instance_level, &[ty, expanded]).

Before-versus-after is observable only if subst can raise: then OCaml reports subst's error and Rust the ambiguity. The equations made by add_gadt_equation have type_params: vec![], and Rust filters type_params.len() == arity, so subst binds no parameters. I found no way to observe it.

Other details:

  • lv is the equation's expansion level (type_newtype_level.1, set from env.ml:1805-1810 at ctype.rs 1839).
  • For a non-equation route, Rust's "expansion level" is None. OCaml's find_type_expansion returns lv from the declaration's newtype level for any declaration. Unchecked whether a non-local declaration can carry one.

SAME (ordering differs, not observable as far as I can tell).

(d) try_expand_head, ctype.ml:1271-1275:

let ty' = try_expand_head try_once env ty in
(match Env.gadt_instance_level env ty' with None -> () | Some lv -> Env.add_gadt_instance_chain env lv ty); ty'

Rust expand_head_traced: if current != start && let Some(level) = gadt_instance_level(ctx, env, current) { add_gadt_instance_chain(scope, level, start) }.

Rust skips the chain when there was no expansion (current == start). OCaml's try_expand_head returns only after at least one try_once succeeds, so ty' != ty there too. SAME

(e) unify.

  • Line 2235 check_trace_gadt_instances !env: Rust 8314-8315 ctx.check_trace_gadt_instances(has_local_constraints). SAME
  • The arm at 2258-2269:
    | Tconstr (p1, [], _), Tconstr (p2, [], _) when Env.has_local_constraints !env && is_newtype !env p1 && is_newtype !env p2 ->
      (try if find_newtype_level !env p1 < find_newtype_level !env p2 then unify env t1 (try_expand_once !env t2)
           else unify env (try_expand_once !env t1) t2 with Cannot_expand -> unify2 env t1 t2)
    Rust unify_newtypes runs when has_local_constraints. It returns None for the arm above it (same path, no cached expansion). Then expand_first = level(p1) >= level(p2) on (i32, i32) tuples, lexicographic like OCaml's local <. Err becomes ambiguous_instance, and Ok(None) (Cannot_expand) falls through to unify_types. SAME

(f) unify2. Ported part, ctype.ml:2279-2282 (the body says 2289-2292):

ignore (expand_head_unif !env t1); ignore (expand_head_unif !env t2);
let t1' = expand_head_unif !env t1 in let t2' = expand_head_unif !env t2 in

Rust expand_heads_unif expands each side once (OCaml twice) and propagates Err. Not ported, ctype.ml:2289-2298:

(if !(trace_gadt_instances ()) then
   let ilevel t = match Env.gadt_instance_level !env t with None -> 0 | Some lv -> lv in
   let lv1 = ilevel t1 and lv2 = ilevel t2 in
   if lv1 > lv2 then Env.add_gadt_instance_chain !env lv1 t2
   else if lv2 > lv1 then Env.add_gadt_instance_chain !env lv2 t1);

That part makes the less ambiguous side's expansion chain join the more ambiguous side's set before unify3, so the later level and expansion tests also refuse it. SAME for the expansions; NOT PORTED for 2289-2298. It is listed under A1 "Open": yes ("the chain of unify2 after expansion (ctype.ml:2293-2298…)").

(g) check_trace_gadt_instances, ctype.ml:178-184 (… trace_gadt_instances () := true; cleanup_abbrev (); true): Rust sets GADT_TRACING and calls self.clear_abbrev_memos(). SAME

PASS (one open site, documented).

15. The guard

Flags (context.rs new 572-574): const GADT_TRACING: u8 = 1; const GADT_MEMBERS: u8 = 2; in gadt_state: Cell<u8>.

Flag Set Cleared
GADT_MEMBERS only push_gadt_instance (5821) never; starts at 0 in TypeContext::new (1441)
GADT_TRACING 5696 (check_trace_gadt_instances) 5707 (reset_trace_gadt_instances)

new_gadt_instance_scope does not touch the flag. PASS on "not set where a scope is created".

Calls and inlined tests:

Site Guard
update_level_checked_above has_gadt_instances() && scope (members)
update_level_pass → ctype gadt_instance_level if !ctx.has_gadt_instances() { return None }
try_expand_head (5165), expand_head_with_mode, expand_heads_unif, expand_head_in_unify expands_gadt_instances(ctx, env): tracing or members, and a scope
inside try_expand_head_traced the row (c) test (tracing); its gadt_instance_level is members-guarded
expand_head_traced chain members-guarded gadt_instance_level
instance_in / instance_list_in (63 typecore sites) has_gadt_instance_scope (members and a scope)
add_copied_gadt_instances additionally has_local_constraints
unify_newtypes (ctype.rs ~8321) has_local_constraints only: runs while the member flag is false

unify_newtypes reads no set itself (its expansion is the row (c) test), but it is the ported 2255-2270 arm, and it runs with the member flag false under local constraints. The rule says a ported test other than row (c) may not run then. It is an arm of OCaml's unify that changes which side is expanded, not a set read. The body places it under "local constraints, which also turn tracing on". The final reviewer has to decide whether it falls under rule (i).

With the flag false, body by body against the base (an awk extraction of each fn and diff):

Function Verdict
instance SAME
instance_list SAME
try_expand_head_with_mode SAME
lower_plain_levels SAME
expand_head_unif SAME
find_abbrev_expansion / scan_abbrev_chain (context.rs) SAME
old expand_head_with_mode body vs new expand_head_plain SAME
update_level_pass DIFFERENT by the one guarded test
update_level_checked_above, try_expand_head DIFFERENT by the guard only

UNCLEAR (only unify_newtypes).

16. Backtracking

Recorded (context.rs): TypeMutation::GadtInstance(GadtScopeRef), pushed by push_gadt_instance through record_mutation, which records only while a full snapshot is active. Restore:

TypeMutation::GadtInstance(scope) => { self.gadt_instance_scopes.borrow_mut()[scope.index()].members.pop(); … }

The journal replays in reverse, so the pops are last-in, first-out per scope. Scope creation is not journaled (a scope outlives a restore, harmless), and the member flag stays set (rule iii).

The task says OCaml's sets are not undone by Btype.backtrack. In this tree they are:

  • btype.ml:80 | Ctypeset of Type_set.t ref * Type_set.t;
  • btype.ml:706 | Ctypeset (r, v) -> r := v;
  • btype.ml:749-751 let set_typeset rs s = log_change (Ctypeset (rs, !rs)); rs := s;
  • env.ml:1329 and 1340 add through set_typeset.

So OCaml undoes every addition on a backtrack (try_expand_safe 1246/1263, update_level 631-638, 4097/4101), and Rust does the same. The only untrailed OCaml write is the repr rebuild at env.ml:1320, which does not change answers.

Remaining risk: a restore_type_state that only Rust makes, a speculative attempt OCaml does not make, would undo additions made inside it. That is equivalent as long as the retry makes them again. I did not list every Rust-only restore site.

UNCLEAR (matches OCaml's trail; Rust-only restore sites not enumerated).

17. Copy sites of typecore.rs

grep -c '^+.*instance_\(list_\)\?in(' $TMPDIR/tc.diff gives 63: 60 instance_in and 3 instance_list_in. In the branch's compiler/ml/typecore.ml, grep -c "instance env" gives 47 lines and grep -c "instance_list env" gives 2. typetexp.ml adds 4 instance env (308, 310, 400, 417) and its instance_list = Ctype.instance_list Env.empty (61).

Converted where OCaml passes no environment:

  • typecore.rs 3325 instance_list_in(ctx, env, &decl.type_params): typetexp.ml:373 instance_list decl.type_params (Env.empty).
  • 14586 result_ty_inst: typecore.ml:4739 instance_def ty_res.
  • 15830 ty_res and 15849 ty_arg0: typecore.ml:4315/4319 instance_def ty_res / instance_def ty_arg (the Rust comment says so itself).
  • 16841 ty_res and 16854 field_ty: the same type_label_exp lines for setfield.

Left on instance where OCaml passes env:

  • rue_unify (typecore.rs new 9494, 22 callers): rue typecore.ml:2607 instance env ty_expected.
  • object_field_use_type (17493): typecore.ml:2553 instance env ty.
  • instance_if_generic (9465; 3 callers): OCaml counterpart not identified.

Correctly left: the constant and predef instance_def sites (2376-2380, 22563-22593, 8895).

The extra conversions copy generic label and declaration nodes, which are unlikely to be set members. The missed rue copies ty_expected, which can hold an instance inside a case. No fixture targets either.

FAIL (per the task's rule: 6 converted where OCaml passes none, 2 to 3 sites left where it passes env).

18. Fixtures

git diff --stat $D -- tests | tail -3:

 .../tests/src/gadt_instance_ambiguous_accepted.mjs | 361 +++++++++++++++++++++
 .../tests/src/gadt_instance_ambiguous_accepted.res | 325 +++++++++++++++++++
 62 files changed, 1405 insertions(+)

git diff --diff-filter=M --name-only $D -- tests prints nothing (every file is new). There are 30 expected files.

Five shapes quoted (expected files, ANSI stripped):

Fixture Diagnostic
_if 4:33 "This has type: int / But this if statement is expected to return: a (defined as int) / This instance of int is ambiguous: / it would escape the scope of its equation / if expressions must return the same type in all branches …"
_eq_witness 4:34, same text
_object_open 6:28-35 "This has type: {"f": a} / But this if statement is expected to return: {.."f": int} / The incompatible parts: a (defined as int) vs int / This instance of int is ambiguous: / it would escape the scope of its equation / if expressions …"
_generalized_copy 6:7-9 "This has type: a (defined as int) / But it's expected to have type: 'a / This instance of int is ambiguous: / it would escape the scope of its equation / Possible solutions: … (k() :> int)"
_polyvariant_nested_if 6:52 "… But it's expected to have type: int / This instance … / it would escape … / Possible solutions: … (y :> int)"

Each has the two lines followed by OCaml's context text.

gadt_instance_ambiguous_accepted.res has 31 modules, including near misses of the refused shapes:

  • ResultAnnotation (_if with : int);
  • BranchAnnotation ((y: int));
  • ArrayResultAnnotation (_array with : array<int>);
  • PlusOne, EqTwoVars, and others.

Fixtures per row of section 14, from shape and the document's mapping (not verified by a run):

  • (c), (d), (f) expansions: the _if/_abbrev*/_array family;
  • (b): _generalized_copy (q01);
  • (e): EqTwoVars (p11, accepted);
  • (a) update_level: no fixture is identified as reaching the level test rather than the expansion test;
  • (g): implied by every refused fixture.

The two "guard" programs of the document (refused at 9:33, and the three-function one) are not committed: no expected file has 9:33.

UNCLEAR: row (a) has no identified fixture, and the guard programs are not committed.

19. CHECKER_DEPARTURES.md, A1

Section "A1 - an instance of a type that is ambiguous outside its equation is refused" is present. Its contents:

  • the programs: "all 64" with a table p01…r16;
  • the 30 refused fixtures listed by id, plus 31 accepted;
  • the guard ("Copies and level updates test it, then the environment's scope handle; the expansion sites test the byte");
  • the cost split (site, (a), (b)) of the fe245f2 run;
  • "434 of the 788 units of tests/tests add one, 2,169 in all … 7 of the 788 units add an instance";
  • the reverted attempt: "#[inline(never)] on unify_newtypes (89b7ae1e8) measured +4,521,654 but raised unify from +1,130,695 to +1,914,955 … and was reverted (8cd9bdec6)".

The open list, quoted:

not ported: the chain of unify2 after expansion (ctype.ml:2293-2298: r01-r04, r12-r16 …), the level test of unify_fields (ctype.ml:2490: q03, r05, r06), of unify_row's row variable (ctype.ml:2609-2611: q05, q06, r07, r08), the tracing wrappers of unify_var (ctype.ml:2759-2766), expand_head_trace (ctype.ml:2782-2784), the application's lower_args (typecore.ml:2828: q09, q10, r09, r10) and local_non_recursive_abbrev (ctype.ml:1533-1534: q11, r11), and copy ?env ~partial through instance ~partial env (Rust's instance_partial). The level updates that Rust makes without an environment (update_level, …) make no level test

Every required item is there. Two caveats:

  • Its explanation of the 434 count cites the type_cases sites that Rust does not port as scopes (section 13).
  • Its split figures are fe245f2's, while the body's are 8cd9bde's (section 8).

PASS

20. Counts as failure

Over the added lines of compiler-rust/src:

  • allow(, unsafe, #[inline(always)], #[cold]: none.
  • #[inline(never)]: 2, at ctype.rs new 12572 (instance_with_gadt_instances) and 12589 (instance_list_with_gadt_instances). The body says "No #[inline(always)], #[cold] or new #[inline(never)] remains" and gives no figure for these two. Finding.
  • #[inline] (plain): context.rs 586, 5740, 5749; ctype.rs 2966; env.rs 2817.
  • HashMap, HashSet, Mutex, thread_local, static : none.
  • RefCell: 1 (context.rs 879).
  • unwrap(): none.
  • todo! / unimplemented!: none.
  • panic!: ctype.rs 1803.
  • unreachable!: ctype.rs 7361.

Functions without a cost in their doc comment are listed in 3(a).

FAIL (two #[inline(never)] without a figure, contrary to the body's statement).

21. TypeExprRefMap::for_each

pub(crate) fn for_each(&self, mut f: impl FnMut(TypeExprRef, &T)) {
    match &self.storage {
        Inline { entries, len } => for (key, value) in entries[..*len].iter().flatten() { f(*key, value); }
        Dense { offset, values } => for (index, value) in values.iter().enumerate() { if let Some(value) = value { f(TypeExprRef(offset + index as u32), value); } }
        Sparse(values) => for (key, value) in values { f(*key, value); }
    } }

Its only caller is add_copied_gadt_instances: cache.nodes.for_each(|original, &copy| { if let Some(level) = ctx.gadt_instance_level(scope, ctx.repr(original)) { ctx.add_gadt_instances(scope, level, &[copy]); } }). That is reached only through instance_with_gadt_instances / instance_list_with_gadt_instances, which means the member flag, a scope, and env.has_local_constraints().

Cost: one step per copied node (Dense: per handle in the span, holes included), times O(members) per gadt_instance_level. The doc comment ("one step per entry, or per handle of the span in dense storage") matches; it has no step test.

PASS

22. Numbers and checks

gh pr checks 350 -R jfrolich/rescript-rust on ed5a1a8, at review time:

Backend bridge coverage: pass
Derived code check: pass
Playground wasm check: pass
Signature equality: pass
Snapshot equivalence: pass
base: pass
build: pass
build-system-tests: pass
build-system-tests (macOS): pass
compare: pass
compiler-warnings: pass
head: pass
ocaml: pass
oxc parity (gentype): pass
oxc parity (js): pass
oxc parity (stdlib): pass
parity (analysis): pass
script-tests: pass
compiler-tests: pending
parity (diagnostics): pass
parity (gentype): pass
parity (javascript): pass
parity (stdlib): pass

22 of 23 pass. compiler-tests is still pending at posting time, so no check has failed.

Earlier heads, from gh run list --branch fix-typechecker-ocaml-parity-10:

  • "Rust rewrite validation" was cancelled on every run from 4679c1d to d5f63f6, and failed on 8d0abc3.
  • No completed green validation run exists on this branch before ed5a1a8.

"Instruction count" on 8cd9bde (run 44228058456, compare job) failed:

run 1: base 4,136,313,333  head 4,142,447,307
run 2: base 4,137,720,970  head 4,141,488,065
run 3: base 4,135,460,618  head 4,141,213,244
run 4: base 4,135,832,174  head 4,142,485,261
run 5: base 4,136,845,194  head 4,143,118,227
| change | +5,752,626 (+0.139%) | | |

The first 30 rows of its function table, and where the body puts each:

# Row Body
1 −12,250,409 expand_head_with_mode (a)
2 12,100,981 expand_head_plain (a)
3 1,130,683 unify (a)
4 706,988 IdTbl::find_by_ident rest
5 679,023 gadt_instance_level (b)
6 642,364 repr_chain (b)
7 497,792 expand_heads_unif site
8 398,277 type_application_parts site
9 242,590 try_expand_head_traced (b)
10 190,067 Env::data_mut (a)
11 −166,879 unify_types (a)
12 −147,336 mi_theap_malloc_zero_aligned_at_generic rest (allocator)
13 140,409 update_level_checked_above site
14 130,678 type_identifier site
15 129,160 moregen site
16 −115,008 mi_…_overalloc rest
17 113,214 mi_free rest
18 108,040 expand_head_traced::<true> (b)
19 97,242 restore_type_state (a)
20 96,633 type_function_nary site
21 89,597 Env::add_gadt_instance_level (a)
22 89,441 update_level_pass (b)
23 −72,110 _mi_theap_malloc_zero rest
24 65,613 instance_in site
25 64,273 expand_head_traced::<false> (b)
26 56,221 try_expand_head site
27 −54,768 lower_plain_levels::lower (a)
28 51,707 mi_page_free_list_extend rest
29 51,117 _mi_page_malloc_zero rest
30 −46,387 mi_bin rest

Every base function in the first 30 is placed. Below row 30, three base functions are not named in the body's split:

  • <bitmaps::bitmap::Iter…> +45,852 (the "path iterator" in prose only);
  • Parmatch::head_path +44,076;
  • typecore::type_construct +42,656 (an instance_in site, not listed among the site rows).

The body's gates, as stated, on 8cd9bde:

  • "All 71 programs agree with OCaml";
  • test_error_output_parity.sh --release: "total: 2267 pass: 2267 fail: 0";
  • test_js_output_parity.sh --release: "Total: 783 / 783 tests passed (100.0% parity)";
  • clippy --all-targets -- -D warnings: "exit 0, no warnings";
  • tc::differential: "65 passed; 0 failed; 1 ignored".

Not rerun here.

The trailer: git show -s --format=%B ed5a1a84ac | git interpret-trailers --parse | sed -n 's/^Instruction-count-increase: *//Ip' prints "GADT instance tracking as OCaml has it; guard tests at the ported sites and the instance work in units that add an instance, split by function in the pull request". This is the parse the job's "Find an accepted increase" step makes (instruction-count.yml 180-196).

The job found the trailer. Its compare-job log for ed5a1a8 (run 44230574575) contains Accepted increase: GADT instance tracking as OCaml has it; …, and its compare step printed:

run 1: base 4,136,102,697  head 4,141,002,098
run 2: base 4,135,442,114  head 4,140,844,178
run 3: base 4,136,596,882  head 4,142,785,288
run 4: base 4,135,865,588  head 4,140,937,567
run 5: base 4,136,512,391  head 4,142,699,271
More instructions than the threshold of +0.1%, accepted by the pull request.
| change | +5,402,064 (+0.131%) | | |

This run measures +5,402,064 (+0.131%), against the body's +5,752,626 (+0.139%) from 8cd9bde. The tree is the same, so the gap is run-to-run noise.

PASS (the trailer is accepted and every completed check passes; compiler-tests is still pending)

@jfrolich

Copy link
Copy Markdown
Owner Author

Coordinator review at ed5a1a8, first round (the author did not write this review). One correction is asked before the merge; the port is otherwise accepted.

What I checked myself

  • Output gate on d5f63f6 (the tree of this head) against trunk 459b87c, on a 3,735-module application: the 3,980 generated files are identical at ten workers and at one, no difference between worker counts in 6,083 entries; ten workers 65.51 G instructions before and after, 1.140 s before and after, peak memory 1,037 → 1,029 MB; one worker 65.46 → 65.55 G (per-run sd 0.20 G).
  • 28 programs of my own through the OCaml compiler, a release build of trunk and a release build of this head, comparing the whole diagnostic text, every warning and the JavaScript: this head agrees with OCaml on all 28; trunk accepts 8 that OCaml refuses (the ambiguity through an if, an array, an option in an array, a function parameter pattern, a returned function, a polymorphic variant, an equality witness, and a unit whose first switch has no generalized constructor). The near misses (a result annotation, an arithmetic use, a record field, a tuple, an abbreviation of int, a nested switch on an option, a ref assigned in the case) are accepted by all three with the same JavaScript.
  • The sets (context.rs) against env.ml 1305-1345: scopes searched from the innermost, a member compared by its representative, additions at the scope of exactly the given level, the chain ending at the first type the set holds. Additions are journaled, as OCaml's set_typeset logs them (btype.ml 80, 706, 749); my checklist for the first pass said OCaml does not undo them, which was wrong.
  • update_level's refusal (ctype.ml 586-589), the copy (770-774), the test of expand_abbrev_gen (1217-1227), try_expand_head's chain (1270-1276) and the arm of unify for two locally abstract types (2255-2268), each read beside the OCaml lines.
  • The guard: the member flag is set only where a type is pushed into a set; while it is clear the level update, the copy sites and the head expansion run trunk's code after one load and one branch.

Asked before the merge (sent to the builder)

  1. The copy sites of typecore.rs must be OCaml's in both directions (section 17 of the first pass, which I verified against typecore.ml 2553, 2607, 4315, 4319, 4739 and typetexp.ml 61, 373): six sites were converted to the copy that adds to a set where OCaml calls instance_def or typetexp's instance_list, which add nothing; rue (instance env ty_expected) and the copy of a polymorphic field's body were left on the copy that adds nothing. The first direction can refuse a program OCaml accepts. With a table of every copy site against its OCaml line, and programs aimed at both directions.
  2. Cost notes for five functions, a step test of the chain addition, the two #[inline(never)] named in the body with their figures, and the sentence about the 434 units corrected: the Rust adds no scope at typecore.ml 4913-4914 (a switch without a generalized constructor), which cannot be seen because that set stays empty.

Accepted as it stands

  • The cost. Instruction count on this head: +0.131% (+5,402,064), with the trailer. By the builder's split: one load and one branch at each ported site (+1.56 M), a larger frame in unify (+1.13 M of the +1.26 M that a unit without an instance pays beyond the guards), and the instance lookups OCaml also makes in the 7 of 788 units that add an instance (+1.91 M). The attempt to keep the arm for two locally abstract types out of unify made unify dearer and was reverted with both figures.
  • The arm for two locally abstract types runs under local constraints whatever the member flag: it decides which side is expanded, as in OCaml, and reads no set.
  • The test of an equation's expansion is made before the expansion where OCaml substitutes first and then refuses, so a refused expansion is not memorized here. No program shows it.
  • The member sets are vectors that keep duplicates; a lookup is linear in the additions of the scopes it reads, where OCaml scans each set for links and then looks up in distinct members.

On record, open without a program: the sites listed under A1 (unify2's chain after expansion, unify_fields, unify_row, the tracing wrappers, instance ~partial, the level updates without an environment).

Not rerun by me: the builder's 71 programs, the parity suites (CI ran them: 22 of 23 checks passed when I read them, compiler-tests still running).

@jfrolich
jfrolich marked this pull request as draft November 18, 2026 03:40
open-session-submodule Bot and others added 2 commits November 18, 2026 03:44
OCaml's copy adds to an instance set only when given the environment
(`instance env`, `instance_list env`; ctype.ml:877-894); `instance_def`
and typetexp's `instance_list` (`Ctype.instance_list Env.empty`,
typetexp.ml:61) add nothing.

Back to `instance` / `instance_list`, where OCaml passes no environment:
the declaration parameters of a type constructor (typetexp.ml:373), the
constructor result under local constraints (typecore.ml:4739
`instance_def ty_res`), and the label result and argument of a record
field and of a field assignment (typecore.ml:4315, 4319).

To `instance_in`, where OCaml passes it: `rue` (typecore.ml:2607
`instance env ty_expected`; `rue_unify`, `finish_expected` and
`finish_expected_at` take the environment), `object_field_use_type`
(typecore.ml:2553 `instance env ty`), and the expected type of a record
(typecore.ml:3044, 3148) and of a coercion's `rue`, through
`instance_if_generic_in`. The tuple's copy has no OCaml counterpart
(typecore.ml:2940 unifies `ty_expected` itself) and keeps trunk's.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…them

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
open-session-submodule Bot and others added 6 commits November 18, 2026 03:48
No behaviour change. Cost notes for `expand_head_plain`,
`expand_heads_traced`, `ambiguous_instance`, `instance_list_in` and
`instance_list_with_gadt_instances`; a step test of
`add_gadt_instance_chain` at 1,000 and 10,000 members (its membership
scan now ticks the test-only step counter). Under A1: where Rust creates
instance scopes, which corrects the reason given for the 434 units; the
scope OCaml adds for a `switch` without a generalized constructor and
Rust does not, as a difference without a program; duplicates in the
member sets; the newtype arm; the journal; and the figures of the two
remaining `#[inline(never)]`.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
OCaml has no copy there: typecore.ml:3165 stores the copy of 3153
(`ty_exp = instance env exp.exp_type`) as the base's type, so this
Rust-only copy adds to no instance set.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
typecore.ml:3780 gives `assert(false)` the type `instance env
ty_expected`; Rust used the expected type uncopied, which adds nothing
to an instance set. A type that is not generic is returned unchanged by
the copy, as before.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-bd5a5126-dea8-7249-82a4-e8fae2447c0b
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 18, 2026 04:06
@jfrolich

Copy link
Copy Markdown
Owner Author

Coordinator review, final, at 4150bc8 (the author did not write this review). Merging: the port agrees with OCaml on every program that differed and that it was aimed at, changes no output, and what it leaves open is listed, with one new witness below that goes to a follow-up.

The correction of the first round

  • The copy sites of typecore.rs are OCaml's in both directions: six back to the copy without the environment (instance_def, typetexp's instance_list), rue, the body of a polymorphic field and the expected type of assert(false) (typecore.ml 3780) with it; the helper that copies a generic expected type decided per caller. The body's table lists 84 sites by their OCaml line: 61 with the environment, 19 without, 4 without a counterpart. I read the changed calls against typecore.ml 2553, 2607, 3780, 4315, 4319, 4739 and typetexp.ml 373.
  • Cost notes, the chain's step test at 1,000 and 10,000, the two #[inline(never)] with their figures, and the sentence about the 434 units are in.

What I ran on this head

  • Output gate against trunk 459b87c on a 3,735-module application: the 3,980 generated files identical at ten workers and at one; no difference between worker counts in 6,083 entries; one worker 65.53 → 65.60 G instructions (per-run sd 0.12 G); ten workers 65.47 → 65.65 G, 1.130 → 1.130 s, peak memory 1,038 → 1,035 MB; cycles 32.00 → 32.04 G. (The first head measured 65.51 → 65.51 G at ten workers: the differences are inside the batch's spread.)
  • 34 programs of my own through the OCaml compiler, a release build of trunk and of this head, whole diagnostic text, warnings and JavaScript: this head agrees with OCaml on 33, trunk on 25.
  • Instruction count: +0.099% (+4,098,852) on 5cfe8a2 and +0.143% (+5,915,013; medians +0.114%) on this head, which differs from it in documents only: the job's spread. Accepted with the trailer, as split in the body (guard tests at the ported sites, the larger frame of unify, the instance work in the 7 of 788 units that add an instance).

A witness the port does not cover yet (trunk and this head both accept; OCaml refuses), found with programs beyond the 34:

type rec t<_> = Int: t<int>
let f = (type a, x: t<a>, y: a) => switch x { | Int => ((z: a) => z)(y) }

OCaml: "This has type: a (defined as int) / But this switch is expected to return: 'a / This instance of int is ambiguous: it would escape the scope of its equation". The same with let g = (z: a) => z; g(y), with let g = z => (z: a), and with a second parameter. With the result annotated : a, or returning [z], both compilers accept. By reading: when the result variable of the switch is unified with the annotation's a, OCaml's update_level (ctype.ml 591-596) lowers a constructor whose path is newer than the target level by expanding it once, which under tracing makes it and its expansion instances, and the lowering of the expansion is then refused (587-589). The one program of my 34 that still differs is this cause seen later: if true { g(y) } else { 0 } is refused by both, by OCaml at g(y) and by this head at 0. This goes to the builder as the next task, with the trace of where the Rust lowering departs.

On record, open without a program: the sites listed under A1 (unify2's chain after expansion, unify_fields, unify_row, the remaining tracing wrappers, instance ~partial, the level updates without an environment), the three OCaml copy sites without a Rust copy, the scope OCaml adds for a switch without a generalized constructor, and the order of test and substitution for an equation's expansion.

Not rerun by me: the builder's programs and the parity suites (CI ran the suites on this head).

@jfrolich
jfrolich merged commit 897e404 into ocaml-to-rust-rewrite Nov 18, 2026
23 checks passed
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