Skip to content

The result types of an application are lowered with GADT instances traced, as in OCaml - #352

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

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

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

OCaml runs an application's lower_args inside wrap_trace_gadt_instances (compiler/ml/typecore.ml:2828, compiler/ml/ctype.ml:188-192). Rust ran it untraced, so it accepted programs OCaml refuses. For example, with type rec t<_> = Int: t<int>:

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

OCaml reports "This instance of int is ambiguous: it would escape the scope of its equation" at g(y). Trunk accepted it and emitted JavaScript.

Cause, as traced

  • Same levels on both sides. I printed the levels in both compilers. get_level of a after the equation is 1004 in both. The last lowering is also the same, the node a from 2006 to 1004, so the level update's expansion arm (ctype.ml:591-596) never fires in either compiler.
  • Why OCaml refuses. By then the node is a member of the instance set at level 1006, and ctype.ml:587-589 refuses to lower it below that. In Rust, no type had joined a set in that unit.
  • Where OCaml adds the member. In expand_abbrev_gen (ctype.ml:1227), from expand_head ← Typecore.lower_args (typecore.ml:2504) ← wrap_trace_gadt_instances (ctype.ml:190) ← Pexp_apply (typecore.ml:2828). Expanding the function's result type a under tracing adds a and int to the equation's set.

Change

  • ctype::wrap_trace_gadt_instances is OCaml's helper. It turns tracing on under local constraints and turns it off only if it turned it on.
  • The application calls lower_args through it.
  • A unit test checks both properties of the wrapper.
  • Cost: one local-constraint test and one branch per application, which is what OCaml's wrapper costs.

Tests

  • 14 super_errors fixtures gadt_instance_application_*: the 13 programs OCaml refuses and trunk accepted, plus the tuple-pattern parameter described below. Their expected files are the OCaml compiler's output.
  • tests/tests/src/gadt_instance_application_accepted.res: 14 neighbours that both compilers accept. compiler-rust/tc_coverage.txt is re-recorded for the new unit.
  • 48 programs ran through both compilers: 27 new ones and the 21 earlier programs aimed at the copy sites. Trunk agreed with OCaml on 35; with this change all 48 agree, including diagnostics, warnings and JavaScript.

The three other tracing wrappers

These are still open under A1: unify_var's wrapper (ctype.ml:2759-2766), expand_head_trace (ctype.ml:2781-2784, used by filter_arrow_n and the object filters), and local_non_recursive_abbrev (ctype.ml:1533-1534). I aimed 18 programs at them; all 18 agree with OCaml on this head. On trunk, one differed: a tuple-pattern parameter applied in the case. Its cause is the same as above, so it is the 14th fixture, gadt_instance_application_parameter_pattern. All three sites stay open, and the programs are recorded under A1.

Gates (on 7dbeec670; the code is that of 4234cb251)

  • Diagnostics parity --release: 2281/2281.
  • JavaScript parity --release: 784/784.
  • Programs: 67/67 agree with OCaml (48 + 18 + the accepted unit).
  • cargo clippy --all-targets -- -D warnings: clean.
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps: clean.
  • tc::differential: 65 passed.
  • CI Instruction count on 7dbeec670: base 4,164,037,467, head 4,161,515,688, −2,521,779 (−0.061%), within the threshold. The total is allocator and IdTbl::find_by_ident movement. The one checker function that grew is type_application_parts, by +177,750, where the wrapper is inlined.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 30 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>
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>
… 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>
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>
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>
open-session-submodule Bot and others added 9 commits November 18, 2026 04:06
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>
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
OCaml runs the application's `lower_args` inside
`wrap_trace_gadt_instances` (compiler/ml/typecore.ml:2828,
compiler/ml/ctype.ml:188-192). Under local constraints its `expand_head`
of the function's result type then makes the instance test of
`expand_abbrev_gen` (ctype.ml:1215-1227): an equation's type and its
expansion join the equation's instance set, and a later level update
refuses to lower them below that set's level (ctype.ml:587-589), the
"ambiguous instance" error. Rust's `lower_args` ran untraced, added
nothing, and accepted `switch x { | Int => { let g = (z: a) => z; g(y) } }`
with an unannotated result.

`ctype::wrap_trace_gadt_instances` is OCaml's helper; the application
calls `lower_args` through it. Cost: one local-constraint test and one
branch per application.

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>
Thirteen super_errors fixtures `gadt_instance_application_*`, the
programs OCaml refuses and trunk accepted: an applied function literal,
a function bound in the case and applied, the same with the result
annotated in the function, with two parameters, and behind an `if`; an
equality witness, applied and as a literal; two nested equations; an
object parameter; a field of a record declared in the case; a tuple
component; a unit whose first function has no generalized constructor;
a polymorphic variant payload. Their expected files are the OCaml
compiler's output.

`tests/tests/src/gadt_instance_application_accepted.res` holds the
fourteen neighbours both compilers accept; `tc_coverage.txt` is
re-recorded for the new unit.

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>
A further program refused by OCaml and accepted by trunk, for the same
cause: the application's lower_args ran untraced. Expected file from the
OCaml compiler.

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:57
@jfrolich

Copy link
Copy Markdown
Owner Author

Coordinator review at 7dbeec6 (the author did not write this review). Merging.

The change is one wrapper: the application's lower_args runs with GADT instances traced, as OCaml's does (wrap_trace_gadt_instances, compiler/ml/ctype.ml 188-192, at typecore.ml 2828). Its expansion of a type that has an equation then adds the type and its expansion to the equation's instance set, and a later level update refuses to lower them below that set. I read both hunks beside the OCaml lines; the unit test checks that tracing is turned on only under local constraints and turned off only by the call that turned it on.

The cause I gave with the task (the expansion arm of the level update) was wrong. The builder traced both compilers before editing, found the levels equal on both sides and the arm never taken, found where OCaml adds the member, and stopped to ask instead of fitting a fix into the functions the task named. The record under A1 says so.

What I ran on this head

  • Output gate against trunk 897e404 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.71 → 65.49 G instructions (per-run sd 0.18 G); ten workers 65.67 → 65.61 G, 1.130 → 1.140 s (minima 1.13 and 1.12), peak memory 1,029 → 1,032 MB; cycles 31.92 → 31.90 G.
  • 40 programs of my own (16 that OCaml refuses) 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 all 40; trunk on 35. The five are the witnesses of this task: a locally annotated function applied in the case, as a literal, bound by let, annotated on its body, with a second parameter, and the one both refused at different places.
  • Instruction count: −0.061% on the minima (4,164.0 → 4,161.5 M), medians level (4,164.3 → 4,164.0 M); the one checker function that grew is type_application_parts (+0.18 M), where the wrapper is inlined: one test and one branch an application.
  • No existing fixture is modified or deleted; 14 new gadt_instance_application_* fixtures and one tests/tests unit.

On record, open without a program (18 programs aimed at them by the builder agree): the wrappers of unify_var, expand_head_trace and the site in local_non_recursive_abbrev; unify2's chain after expansion, unify_fields, unify_row; instance ~partial; the level updates without an environment; the scope OCaml adds for a switch without a generalized constructor; the three OCaml copy sites without a Rust copy.

Not rerun by me: the builder's 67 programs and the parity suites (CI ran the suites: 23 of 23 checks pass).

@jfrolich
jfrolich merged commit cfd8f20 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