Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
92862d2
Refuse a GADT instance that would escape its equation, as OCaml does
open-session-submodule[bot] Nov 18, 2026
22a53a6
A copy of a GADT instance joins its set, as in OCaml's copy
open-session-submodule[bot] Nov 18, 2026
7d08707
Record A1, the GADT instance port, with its 64 programs
open-session-submodule[bot] Nov 18, 2026
752db1e
Keep the GADT instance tests off the default path of expansion and copy
open-session-submodule[bot] Nov 18, 2026
c61856d
Merge origin/ocaml-to-rust-rewrite into fix-typechecker-ocaml-parity-10
open-session-submodule[bot] Nov 18, 2026
b46a3af
Add no GADT instance where no instance scope is at the equation's level
open-session-submodule[bot] Nov 18, 2026
c0daf09
Re-record the checker coverage ratchet for the new tests/tests unit
open-session-submodule[bot] Nov 18, 2026
c8346e6
Record the cost of the GADT instance port and its later commits in A1
open-session-submodule[bot] Nov 18, 2026
ae16a97
Copy for GADT instances only where the environment has an instance scope
open-session-submodule[bot] Nov 18, 2026
c3cd63b
Expand heads with the GADT instance test only where the environment h…
open-session-submodule[bot] Nov 18, 2026
bcc2a4e
Scan the memo for the GADT instance chain on its own
open-session-submodule[bot] Nov 18, 2026
4679c1d
Format the lines of the GADT instance expansion
open-session-submodule[bot] Nov 18, 2026
8d0abc3
Record the measured cost of A1 split by function, and the chain's guard
open-session-submodule[bot] Nov 18, 2026
82e309f
Guard the GADT instance tests by a type having joined an instance set
open-session-submodule[bot] Nov 18, 2026
cbaf8d7
Test a GADT instance before trunk's single expansion, not in a second…
open-session-submodule[bot] Nov 18, 2026
fe245f2
Make trunk's expansion calls at the unifier's sites while no instance…
open-session-submodule[bot] Nov 18, 2026
d1faed4
Record A1's guard, its two programs and the cost split of fe245f217
open-session-submodule[bot] Nov 18, 2026
89b7ae1
Keep the newtype arm out of unify's body
open-session-submodule[bot] Nov 18, 2026
43e204b
State the cost of the GADT instance lookup against OCaml's
open-session-submodule[bot] Nov 18, 2026
8cd9bde
Revert "Keep the newtype arm out of unify's body" (+4,521,654; unify …
open-session-submodule[bot] Nov 18, 2026
f3af82e
Record A1's accepted cost and the reverted inline(never) attempt
open-session-submodule[bot] Nov 18, 2026
d5f63f6
Name the copy with GADT instances in the doc of instance_in without a…
open-session-submodule[bot] Nov 18, 2026
ed5a1a8
Accept the instruction count of the GADT instance port
open-session-submodule[bot] Nov 18, 2026
de5bef1
Make typecore's copies add to an instance set exactly where OCaml's do
open-session-submodule[bot] Nov 18, 2026
0684ebd
Record the copy sites corrected in de5bef189 and the programs aimed a…
open-session-submodule[bot] Nov 18, 2026
702f4bd
Document A1's costs and scope creation, and test the chain's steps
open-session-submodule[bot] Nov 18, 2026
66dfd83
Give the second copy of a record's base type trunk's call
open-session-submodule[bot] Nov 18, 2026
5f8d821
Copy the expected type of assert(false) as OCaml does
open-session-submodule[bot] Nov 18, 2026
848fc88
Record the copy sites of typecore.rs by form under A1
open-session-submodule[bot] Nov 18, 2026
5cfe8a2
Count the assert(false) copy among A1's copy sites
open-session-submodule[bot] Nov 18, 2026
4150bc8
Count the accepted programs of A1's copy sites
open-session-submodule[bot] Nov 18, 2026
b95d044
Merge origin/ocaml-to-rust-rewrite
open-session-submodule[bot] Nov 18, 2026
4234cb2
Trace GADT instances while an application lowers its result types
open-session-submodule[bot] Nov 18, 2026
1ea17a5
Test the application's traced lower_args with OCaml's diagnostics
open-session-submodule[bot] Nov 18, 2026
e51cc6b
Record under A1 that the application's lower_args is traced
open-session-submodule[bot] Nov 18, 2026
34a0385
Add the changelog fragment for the traced lower_args
open-session-submodule[bot] Nov 18, 2026
49fa843
Test a tuple-pattern parameter applied in the case
open-session-submodule[bot] Nov 18, 2026
3ec7ba0
Record the programs aimed at A1's other tracing wrappers
open-session-submodule[bot] Nov 18, 2026
7dbeec6
Count the refused programs aimed at A1's other wrappers
open-session-submodule[bot] Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/gadt-instance-application.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refuse, as the OCaml compiler does, a function applied inside the case of a GADT equation whose result leaves the case through that equation, for example `switch x { | Int => { let g = (z: a) => z; g(y) } }` without a result annotation ("This instance of int is ambiguous").
60 changes: 58 additions & 2 deletions compiler-rust/docs/CHECKER_DEPARTURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8820,8 +8820,7 @@ r01-r04, r12-r16 were written for it and agree), 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:2782-2784`) 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`, for the same
Expand Down Expand Up @@ -8933,6 +8932,63 @@ base 4,135,460,618, head 4,141,213,244, +5,752,626 (+0.139%), every
function of the table above unchanged and `IdTbl::find_by_ident` at
+706,988.

#### Second part: the application's lower_args is traced

Programs (each starts with `type rec t<_> = Int: t<int>`): OCaml refuses
`switch x { | Int => ((z: a) => z)(y) }` and
`switch x { | Int => { let g = (z: a) => z; g(y) } }` with `x: t<a>`,
`y: a` and no result annotation, "This instance of int is ambiguous",
and so on through an equality witness, two nested equations, an object
parameter, a field of a record declared in the case, a tuple component,
a polymorphic variant payload and a unit whose first function has no
generalized constructor. Trunk accepted all of them and emitted
JavaScript; with `if true { g(y) } else { 0 }` both refused, OCaml at
`g(y)` and Rust at `0`. Of 48 programs (27 new, 21 aimed at the copy
sites before), trunk agreed with OCaml on 35 and differed on 13; after
the fix all 48 agree, diagnostics, warnings and JavaScript.

The cause first given, the level update's expansion arm
(`ctype.ml:591-596`), was wrong. A trace of both compilers on the second
program: `get_level` of `a` after the equation is 1004 in both (OCaml
reads the definition level, `ctype.ml:566`, which Rust stores too), and
the last lowering is the same in both, the node `a` from 2006 to 1004,
where `1004 < 1004` is false, so the arm never expands. OCaml refuses
because the node is then a member of the set of 1006
(`ctype.ml:587-589`); in Rust no type ever joined a set in that unit.
OCaml adds it in `expand_abbrev_gen` (`ctype.ml:1227`), reached by
`expand_head` from `Typecore.lower_args` (`typecore.ml:2504`), which the
application runs inside `wrap_trace_gadt_instances` (`ctype.ml:188-192`,
`typecore.ml:2828`): the expansion of the function's result type `a`
under tracing adds `a` and `int` to the equation's set. Rust's
`lower_args` ran untraced. Now `ctype::wrap_trace_gadt_instances` is
OCaml's helper and the application calls `lower_args` through it
(`4234cb251`); its unit test checks that it traces only under local
constraints and turns tracing off only if it turned it on. Fixtures:
super_errors `gadt_instance_application_*` (the 13 programs, expected
files from the OCaml compiler) and
`tests/tests/src/gadt_instance_application_accepted.res` (14 accepted
neighbours). Cost: one local-constraint test and one branch per
application.

The three other tracing wrappers stay open; 18 programs aimed at them
agree with OCaml on this head, and on trunk all but one (a tuple-pattern
parameter applied in the case, refused by OCaml for the same cause as
above and now the fixture `gadt_instance_application_parameter_pattern`).
`unify_var`'s wrapper (`ctype.ml:2759-2766`): a case's pattern variable,
a tuple pattern of the scrutinee, a coercion `(y :> int)`, a `let module`
and a `let` annotated with `a`, each inside the case. `expand_head_trace`
(`ctype.ml:2781-2784`, used by `filter_arrow_n`, `filter_method`,
`filter_method_field` and `filter_object_field_for_write`): with
`Fn: t<int => int>`, a function literal against the result type `a`
annotated and through a `let h: a`, against `y` behind an `if`, `y`
applied, and `y` bound and returned behind an `if`; with
`O: t<{"m": int}>`, a method of `o: a` read and `o` behind an `if`.
`local_non_recursive_abbrev` (`ctype.ml:1533-1534`): an equation whose
right side is an abbreviation `abbr = int` or `box<int>`, another
locally abstract type, `box<b>` and `list<b>`. Six of them are refused
by both compilers, four with the ambiguity lines, and twelve accepted
with the same JavaScript.

## Fourth round: modules, aliases and usage marks

### Scope, commands and calibration
Expand Down
36 changes: 36 additions & 0 deletions compiler-rust/src/types/ctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12465,6 +12465,20 @@ fn compute_variance_rec(
}
}

/// OCaml `wrap_trace_gadt_instances env f x` (compiler/ml/ctype.ml:188-192):
/// run `f` with GADT instance tracing turned on when `env` has local
/// constraints, and turn it off afterwards only if this call turned it on.
/// While tracing, an expansion of an equation adds the type and its
/// expansion to the equation's instance set (ctype.ml:1215-1227). Cost: one
/// local-constraint test and one branch, beside `f`; turning tracing on also
/// forgets the memorized expansions, as OCaml's `cleanup_abbrev`.
pub fn wrap_trace_gadt_instances<T>(ctx: &TypeContext, env: &Env, f: impl FnOnce() -> T) -> T {
let turned_on = ctx.check_trace_gadt_instances(env.has_local_constraints());
let result = f();
ctx.reset_trace_gadt_instances(turned_on);
result
}

/// OCaml's lower_args: walk an arrow type, expanding each component with
/// expand_head. This triggers type alias expansion for function argument/return
/// types, matching OCaml's behavior during Pexp_apply processing.
Expand Down Expand Up @@ -16309,6 +16323,28 @@ mod tests {
assert_eq!(steps, 0);
}

/// `wrap_trace_gadt_instances` (ctype.ml:188-192) traces only under
/// local constraints and turns tracing off only if it turned it on.
#[test]
fn wrap_trace_gadt_instances_traces_under_local_constraints_only() {
let ctx = TypeContext::new(std::sync::Arc::new(IdGenerator::new()));
let plain = Env::empty();
let mut constrained = Env::empty();
let local = Path::pident(crate::ident::Ident::create_persistent("a"));
constrained.add_local_constraint(local, reified_type_decl(7));
assert!(!wrap_trace_gadt_instances(&ctx, &plain, || ctx.trace_gadt_instances()));
assert!(!ctx.trace_gadt_instances());
assert!(wrap_trace_gadt_instances(&ctx, &constrained, || ctx.trace_gadt_instances()));
assert!(!ctx.trace_gadt_instances());
// Already on: the wrapper leaves it on, under constraints or not.
let outer = ctx.check_trace_gadt_instances(true);
assert!(wrap_trace_gadt_instances(&ctx, &constrained, || ctx.trace_gadt_instances()));
assert!(wrap_trace_gadt_instances(&ctx, &plain, || ctx.trace_gadt_instances()));
assert!(ctx.trace_gadt_instances());
ctx.reset_trace_gadt_instances(outer);
assert!(!ctx.trace_gadt_instances());
}

#[test]
fn univar_scope_pairs_ten_thousand_binders_in_one_step_each() {
let ctx = TypeContext::new(std::sync::Arc::new(IdGenerator::new()));
Expand Down
11 changes: 8 additions & 3 deletions compiler-rust/src/types/typecore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10339,9 +10339,14 @@ fn type_application_parts(

let lowered_ty = crate::types::ctype::instance_in(ctx, env, typed_funct.exp_type);
ctx.end_def();
// OCaml compiler/ml/typecore.ml:2579-2582 instances the function type and
// immediately runs `lower_args` (typecore.ml:2335-2342).
crate::types::ctype::lower_args(ctx, env, lowered_ty);
// OCaml compiler/ml/typecore.ml:2826-2828 instances the function type and
// immediately runs `lower_args` (typecore.ml:2503-2513) with GADT
// instances traced: its expansion of an equation's type adds the type to
// the equation's instance set, which a later level update refuses to
// lower below that set's level.
crate::types::ctype::wrap_trace_gadt_instances(ctx, env, || {
crate::types::ctype::lower_args(ctx, env, lowered_ty)
});

ctx.begin_def();

Expand Down
2 changes: 1 addition & 1 deletion compiler-rust/tc_coverage.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
application NotBuilt 3841
runtime NotBuilt 158
tests/tests NotBuilt 833
tests/tests NotBuilt 834
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_after_plain_function.res:3:79-82

1 │ type rec t<_> = Int: t<int>
2 │ let h = (o: option<int>) => switch o { | Some(n) => n | None => 0 }
3 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (z: a)
│ => z; g(y) } }
4 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(y) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_applied_function_literal.res:2:57-71

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

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

All branches in a switch must return the same type.
To fix this, change your branch to return the expected type.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_equality_witness.res:2:86-89

1 │ type rec eq<_, _> = Refl: eq<'a, 'a>
2 │ let f = (type a, w: eq<a, int>, y: a) => switch w { | Refl => { let g =
│ (z: a) => z; g(y) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(y) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_equality_witness_literal.res:2:67-81

1 │ type rec eq<_, _> = Refl: eq<'a, 'a>
2 │ let f = (type a, w: eq<a, string>, y: a) => switch w { | Refl => ((z: a)
│  => z)(y) }
3 │

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

All branches in a switch must return the same type.
To fix this, change your branch to return the expected type.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_if_else_int.res:2:89-92

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (z: a)
│ => z; if true { g(y) } else { 0 } } }
3 │

This has type: a (defined as int)
But this if statement is expected to return: 'a
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).
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_let_function_applied.res:2:79-82

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (z: a)
│ => z; g(y) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(y) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_local_record_field.res:7:5-8

5 ┆ let m: M.r = {M.v: y}
6 ┆ let g = (r: M.r) => r.v
7 ┆ g(m)
8 ┆ }
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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(m) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_nested_two_equations.res:5:55-58

3 │ let f = (type a b, x: t<a>, w: s<b>, y: a, v: b) =>
4 │ switch x {
5 │ | Int => switch w { | Str => { let g = (z: a) => z; g(y) } }
6 │ }
7 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(y) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_object_parameter.res:2:91-101

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (o: {"v
│ ": a}) => o["v"]; g({"v": y}) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g({"v": y}) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_parameter_pattern.res:2:84-92

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = ((z: a,
│  _)) => z; g((y, 1)) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g((y, 1)) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_polyvariant_payload.res:2:109-116

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (p: [#V
│ (a)]) => switch p { | #V(z) => z }; g(#V(y)) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(#V(y)) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_result_annotated_in_function.res:2:79-82

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = z => (z
│ : a); g(y) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g(y) :> int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

We've found a bug for you!
/.../fixtures/gadt_instance_application_tuple_component.res:2:106-114

1 │ type rec t<_> = Int: t<int>
2 │ let f = (type a, x: t<a>, y: a) => switch x { | Int => { let g = (p: (a,
│  int)) => { let (z, _) = p; z }; g((y, 1)) } }
3 │

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:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (g((y, 1)) :> int)

Loading
Loading