Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 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
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-ambiguity.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refuse, as the OCaml compiler does, a value whose type is known only through a GADT equation when it leaves the case of that equation, for example `if true { y } else { 0 }` with `y: a` under `| Int =>` without a result annotation ("This instance of int is ambiguous").
354 changes: 354 additions & 0 deletions compiler-rust/docs/CHECKER_DEPARTURES.md

Large diffs are not rendered by default.

294 changes: 283 additions & 11 deletions compiler-rust/src/types/context.rs

Large diffs are not rendered by default.

463 changes: 447 additions & 16 deletions compiler-rust/src/types/ctype.rs

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions compiler-rust/src/types/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,10 @@ struct EnvData {
/// Local constraints (for GADTs).
local_constraints: PersistentHashMap<Path, Arc<TypeDeclaration>>,

/// The innermost GADT instance scope (OCaml `gadt_instances`,
/// compiler/ml/env.ml:368); its sets live in the [`TypeContext`].
gadt_instances: super::context::GadtScopeRef,

/// The label array `store_type` described with each local record type
/// (compiler/ml/env.ml:1561-1569 stores `(decl, (constructors, labels))`
/// in `env.types`): what `find_type_descrs (Pident id)` returns.
Expand Down Expand Up @@ -1657,6 +1661,7 @@ impl Env {
scope_caches: std::rc::Rc::default(),
implicit_open_roots: PersistentHashSet::default(),
local_constraints: PersistentHashMap::default(),
gadt_instances: super::context::GadtScopeRef::NONE,
type_label_families: PersistentHashMap::default(),
flags: EnvFlags::default(),
}),
Expand Down Expand Up @@ -2797,11 +2802,21 @@ impl Env {
.map(|(p, d)| (p, d.as_ref()))
}

/// `Env.add_gadt_instance_level` (env.ml) returns a fresh Env record.
/// Rust does not track GADT instances, so only the fresh scope identity
/// that the scope-keyed caches observe is kept.
pub fn add_gadt_instance_level(&mut self) {
/// OCaml `Env.add_gadt_instance_level lv` (compiler/ml/env.ml:1307-1308):
/// enter a new, empty GADT instance set at `level`. The set lives in
/// `ctx`; the environment keeps its handle, so clones share it as OCaml's
/// share the `ref`. O(1) amortized, plus the copy of the shared bindings
/// record if another environment holds it.
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 innermost GADT instance scope, `NONE` outside every one. O(1).
#[inline]
pub fn gadt_instance_scope(&self) -> super::context::GadtScopeRef {
self.data.gadt_instances
}

// ========================================================================
Expand Down Expand Up @@ -3481,7 +3496,7 @@ mod tests {
env,
env.add_local_constraint(Path::pident(id), declaration.clone())
);
extends!(env, env.add_gadt_instance_level());
extends!(env, env.add_gadt_instance_level(&ctx, 1));
extends!(env, env.mark_implicit_open_root("Stdlib"));
let coerced = retained.implicit_coercion();
assert_ne!(coerced.id(), original_id);
Expand Down
24 changes: 24 additions & 0 deletions compiler-rust/src/types/type_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,30 @@ impl<T> TypeExprRefMap<T> {
self.entry_count += 1;
}

/// Call `f` on every entry, in no particular order. Cost: one step per
/// entry, or per handle of the span in dense storage.
pub(crate) fn for_each(&self, mut f: impl FnMut(TypeExprRef, &T)) {
match &self.storage {
TypeExprRefMapStorage::Inline { entries, len } => {
for (key, value) in entries[..*len].iter().flatten() {
f(*key, value);
}
}
TypeExprRefMapStorage::Dense { offset, values } => {
for (index, value) in values.iter().enumerate() {
if let Some(value) = value {
f(TypeExprRef(offset + index as u32), value);
}
}
}
TypeExprRefMapStorage::Sparse(values) => {
for (key, value) in values {
f(*key, value);
}
}
}
}

#[inline]
pub(crate) fn get(&self, key: TypeExprRef) -> Option<&T> {
match &self.storage {
Expand Down
Loading
Loading