Skip to content

Commit 897e404

Browse files
An instance of a type that is ambiguous outside its equation is refused, as in OCaml (#350)
* Refuse a GADT instance that would escape its equation, as OCaml does 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> * A copy of a GADT instance joins its set, as in OCaml's copy 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> * Record A1, the GADT instance port, with its 64 programs 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> * Keep the GADT instance tests off the default path of expansion and copy 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> * Add no GADT instance where no instance scope is at the equation's level 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> * Re-record the checker coverage ratchet for the new tests/tests unit 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> * Record the cost of the GADT instance port and its later commits in A1 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> * Copy for GADT instances only where the environment has an instance scope `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> * Expand heads with the GADT instance test only where the environment has 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> * Scan the memo for the GADT instance chain on its own 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> * Format the lines of the GADT instance expansion 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> * Record the measured cost of A1 split by function, and the chain's guard 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> * Guard the GADT instance tests by a type having joined an instance set 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> * Test a GADT instance before trunk's single expansion, not in a second 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> * Make trunk's expansion calls at the unifier's sites while no instance 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> * Record A1's guard, its two programs and the cost split of fe245f2 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> * Keep the newtype arm out of unify's body 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> * State the cost of the GADT instance lookup against OCaml'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> * Revert "Keep the newtype arm out of unify's body" (+4,521,654; unify +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> * Record A1's accepted cost and the reverted inline(never) attempt 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> * Name the copy with GADT instances in the doc of instance_in without a 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> * Accept the instruction count of the GADT instance port 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> * Make typecore's copies add to an instance set exactly where OCaml's do 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> * Record the copy sites corrected in de5bef1 and the programs aimed at 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> * Document A1's costs and scope creation, and test the chain's steps 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> * Give the second copy of a record's base type trunk's call 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> * Copy the expected type of assert(false) as OCaml does 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> * Record the copy sites of typecore.rs by form under A1 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> * Count the assert(false) copy among A1's copy sites 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> * Count the accepted programs of A1's copy sites 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> Co-authored-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
1 parent 459b87c commit 897e404

70 files changed

Lines changed: 2686 additions & 134 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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").

‎compiler-rust/docs/CHECKER_DEPARTURES.md‎

Lines changed: 354 additions & 0 deletions
Large diffs are not rendered by default.

‎compiler-rust/src/types/context.rs‎

Lines changed: 283 additions & 11 deletions
Large diffs are not rendered by default.

‎compiler-rust/src/types/ctype.rs‎

Lines changed: 447 additions & 16 deletions
Large diffs are not rendered by default.

‎compiler-rust/src/types/env.rs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,10 @@ struct EnvData {
15611561
/// Local constraints (for GADTs).
15621562
local_constraints: PersistentHashMap<Path, Arc<TypeDeclaration>>,
15631563

1564+
/// The innermost GADT instance scope (OCaml `gadt_instances`,
1565+
/// compiler/ml/env.ml:368); its sets live in the [`TypeContext`].
1566+
gadt_instances: super::context::GadtScopeRef,
1567+
15641568
/// The label array `store_type` described with each local record type
15651569
/// (compiler/ml/env.ml:1561-1569 stores `(decl, (constructors, labels))`
15661570
/// in `env.types`): what `find_type_descrs (Pident id)` returns.
@@ -1657,6 +1661,7 @@ impl Env {
16571661
scope_caches: std::rc::Rc::default(),
16581662
implicit_open_roots: PersistentHashSet::default(),
16591663
local_constraints: PersistentHashMap::default(),
1664+
gadt_instances: super::context::GadtScopeRef::NONE,
16601665
type_label_families: PersistentHashMap::default(),
16611666
flags: EnvFlags::default(),
16621667
}),
@@ -2797,11 +2802,21 @@ impl Env {
27972802
.map(|(p, d)| (p, d.as_ref()))
27982803
}
27992804

2800-
/// `Env.add_gadt_instance_level` (env.ml) returns a fresh Env record.
2801-
/// Rust does not track GADT instances, so only the fresh scope identity
2802-
/// that the scope-keyed caches observe is kept.
2803-
pub fn add_gadt_instance_level(&mut self) {
2805+
/// OCaml `Env.add_gadt_instance_level lv` (compiler/ml/env.ml:1307-1308):
2806+
/// enter a new, empty GADT instance set at `level`. The set lives in
2807+
/// `ctx`; the environment keeps its handle, so clones share it as OCaml's
2808+
/// share the `ref`. O(1) amortized, plus the copy of the shared bindings
2809+
/// record if another environment holds it.
2810+
pub fn add_gadt_instance_level(&mut self, ctx: &TypeContext, level: i32) {
28042811
self.bump_scope();
2812+
let parent = self.data.gadt_instances;
2813+
self.data_mut().gadt_instances = ctx.new_gadt_instance_scope(level, parent);
2814+
}
2815+
2816+
/// The innermost GADT instance scope, `NONE` outside every one. O(1).
2817+
#[inline]
2818+
pub fn gadt_instance_scope(&self) -> super::context::GadtScopeRef {
2819+
self.data.gadt_instances
28052820
}
28062821

28072822
// ========================================================================
@@ -3481,7 +3496,7 @@ mod tests {
34813496
env,
34823497
env.add_local_constraint(Path::pident(id), declaration.clone())
34833498
);
3484-
extends!(env, env.add_gadt_instance_level());
3499+
extends!(env, env.add_gadt_instance_level(&ctx, 1));
34853500
extends!(env, env.mark_implicit_open_root("Stdlib"));
34863501
let coerced = retained.implicit_coercion();
34873502
assert_ne!(coerced.id(), original_id);

‎compiler-rust/src/types/type_expr.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,30 @@ impl<T> TypeExprRefMap<T> {
11441144
self.entry_count += 1;
11451145
}
11461146

1147+
/// Call `f` on every entry, in no particular order. Cost: one step per
1148+
/// entry, or per handle of the span in dense storage.
1149+
pub(crate) fn for_each(&self, mut f: impl FnMut(TypeExprRef, &T)) {
1150+
match &self.storage {
1151+
TypeExprRefMapStorage::Inline { entries, len } => {
1152+
for (key, value) in entries[..*len].iter().flatten() {
1153+
f(*key, value);
1154+
}
1155+
}
1156+
TypeExprRefMapStorage::Dense { offset, values } => {
1157+
for (index, value) in values.iter().enumerate() {
1158+
if let Some(value) = value {
1159+
f(TypeExprRef(offset + index as u32), value);
1160+
}
1161+
}
1162+
}
1163+
TypeExprRefMapStorage::Sparse(values) => {
1164+
for (key, value) in values {
1165+
f(*key, value);
1166+
}
1167+
}
1168+
}
1169+
}
1170+
11471171
#[inline]
11481172
pub(crate) fn get(&self, key: TypeExprRef) -> Option<&T> {
11491173
match &self.storage {

0 commit comments

Comments
 (0)