From c28eec1e90d04a87f9932dc8d1d0b4603610d13f Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Fri, 20 Nov 2026 08:42:32 -0500 Subject: [PATCH 1/2] Cache type checking per statement (#1301) check_deduce caches each statement's type check per module, keyed by its structural hash and a fingerprint of what it depends on: the statements defining the names it mentions (transitively, and every candidate of an overloaded name) plus every statement that is not a plain named definition (views, models, associative, imports, ...). A statement that only moved reuses its entry with its own locations shifted. Statements whose check reported an error or warning are not cached, and the postulate report bypasses the cache. Co-Authored-By: Claude Opus 5.5 --- checker_cache.py | 184 +++++++++++++++++++++++++++- checker_pipeline.py | 57 +++++++-- docs/lsp-plan.md | 1 + test/lsp/test_check_proofs_cache.py | 8 +- test/lsp/test_snapshot.py | 3 +- test/lsp/test_type_check_cache.py | 183 +++++++++++++++++++++++++++ 6 files changed, 416 insertions(+), 20 deletions(-) create mode 100644 test/lsp/test_type_check_cache.py diff --git a/checker_cache.py b/checker_cache.py index c67ca57a..3aa6fbe4 100644 --- a/checker_cache.py +++ b/checker_cache.py @@ -10,16 +10,30 @@ a Deduce program is valid except by reporting cache hits and misses. """ -from typing import cast +import copy +from typing import TypeVar, cast + +from lark.tree import Meta from abstract_syntax import ( + Assert, Auto, ConceptDecl, + Define, + GenRecFun, Import, ModelDecl, + OverloadedVar, + Postulate, + PostulateFun, + PostulateType, Predicate, + Print, PVar, + RecFun, Statement, + Theorem, + TypeAlias, Union, VarRef, ViewDecl, @@ -75,6 +89,7 @@ def reset_stmt_cache() -> None: global _stmt_cache _stmt_cache.clear() _prelude_imports_cache.clear() + _type_check_cache.clear() _cache_stats["hits"].clear() _cache_stats["misses"].clear() @@ -168,6 +183,11 @@ def _collect_referenced_names( for v in cast(dict[object, object], node).values(): _collect_referenced_names(v, out) return out + if isinstance(node, OverloadedVar): + # Before type checking, any of the candidates may be the one + # chosen, so the statement depends on all of them (#1301). + out.update(node.resolved_names) + return out if isinstance(node, VarRef): out.add(node.get_name()) return out @@ -234,3 +254,165 @@ def _is_global_barrier(stmt: Statement) -> bool: and models (#1264): a where formula names its concepts by string, and a model is found by its concept and type, never by name.""" return isinstance(stmt, (Import, Auto, ConceptDecl, ModelDecl)) + + +# --------------------------------------------------------------------------- +# Per-statement type-check cache (#1301) +# --------------------------------------------------------------------------- +# +# ``type_check_stmt`` checks each statement against the env that +# ``process_declaration`` built from the whole file, and returns the +# checked statement. For each module, ``_type_check_cache`` holds the +# checked statements of its last check, keyed by ``(stmt_hash, +# deps_fingerprint)`` (see ``type_check_fingerprints``), with the +# statement's layout (``statement_layout``) and first position. Each check +# replaces the module's entries with the ones it used or created, so +# the cache holds one file's worth per module. +# +# A checked statement carries its source locations, which later phases +# report and ``goal_at`` matches against. On a hit for a statement that +# only moved, ``_relocated`` shifts the cached statement's locations to +# where it now is. Locations it embeds from other statements (say, the +# type of a function it calls) are left as they were. + +# (layout, (first line, first position), checked statement) +T = TypeVar("T") +_TypeCheckEntry = tuple[int, tuple[int, int], Statement] +_type_check_cache: dict[str, dict[tuple[int, int], _TypeCheckEntry]] = {} + +# Statements whose effect on type checking other statements goes only +# through the names they define. Every other kind is a barrier: views +# and models are found by type, associativity by operator and type, +# imports bring in whole modules, and so on. +_NAME_ONLY_STATEMENTS = ( + Assert, Define, GenRecFun, Postulate, PostulateFun, PostulateType, + Predicate, Print, RecFun, Theorem, TypeAlias, Union, ViewRecFun, +) + +# The statements whose type check is cached: the ones that do real +# work in ``type_check_stmt`` and have no side effect there. +TYPE_CHECK_CACHED_STATEMENTS = ( + Assert, Define, GenRecFun, Postulate, Print, RecFun, Theorem, ViewRecFun, +) + + +def type_check_fingerprints(stmts: list[Statement], + hashes: list[int]) -> list[int]: + """For each statement, a fingerprint of the statements its type + check depends on: the ones defining a name it mentions, what those + depend on in turn (a function's type can come from a type alias it + mentions), and every barrier statement in the file (the env is the + whole file's).""" + defined_to_idx: dict[str, int] = {} + for i, s in enumerate(stmts): + for n in _collect_defined_names(s): + defined_to_idx.setdefault(n, i) + barriers = hash(tuple(h for s, h in zip(stmts, hashes) + if not isinstance(s, _NAME_ONLY_STATEMENTS))) + deps_fps: list[int] = [] + full_fps: list[int] = [] # a statement's hash folded with its deps + for i, s in enumerate(stmts): + deps = {defined_to_idx[n] for n in _collect_referenced_names(s) + if n in defined_to_idx} - {i} + fp = hash((barriers, tuple(full_fps[j] if j < i else hashes[j] + for j in sorted(deps)))) + deps_fps.append(fp) + full_fps.append(hash((hashes[i], fp))) + return deps_fps + + +def statement_start(stmt: Statement) -> tuple[int, int]: + loc = stmt.location + return (loc.line, getattr(loc, "start_pos", 0)) + + +def _metas(node: object, out: list[Meta]) -> list[Meta]: + """Every location in ``node``, in a fixed order.""" + if node is None or isinstance(node, (str, int, bool, float)): + return out + if isinstance(node, Meta): + out.append(node) + return out + if isinstance(node, (list, tuple)): + for x in cast(list[object] | tuple[object, ...], node): + _metas(x, out) + return out + if isinstance(node, dict): + for v in cast(dict[object, object], node).values(): + _metas(v, out) + return out + if hasattr(node, "__dict__"): + for k, v in cast(dict[str, object], vars(node)).items(): + if k != "__hash_cache__": + _metas(v, out) + return out + + +def statement_layout(stmt: Statement) -> int: + """A hash of the statement's locations, relative to where it starts + when they are in its own file. Two statements with the same hash + (``_hash_ast``) and layout differ only in where they are.""" + filename = getattr(stmt.location, "filename", None) + line, pos = statement_start(stmt) + sig: list[tuple[object, ...]] = [] + for m in _metas(stmt, []): + if m.empty: + sig.append(()) + elif getattr(m, "filename", None) == filename: + sig.append((m.line - line, m.column, m.end_line - line, + m.end_column, getattr(m, "start_pos", 0) - pos, + getattr(m, "end_pos", 0) - pos)) + else: + sig.append((getattr(m, "filename", None), m.line, m.column, + m.end_line, m.end_column)) + return hash(tuple(sig)) + + +def _relocated(node: T, old: Statement, old_first: tuple[int, int], + new_first: tuple[int, int]) -> T: + """A copy of ``node``, part of the checked statement ``old`` that + started at ``old_first``, with ``old``'s own locations moved so that + it starts at ``new_first``.""" + filename = getattr(old.location, "filename", None) + start, end = old_first[1], getattr(old.location, "end_pos", 0) + dline, dpos = new_first[0] - old_first[0], new_first[1] - old_first[1] + moved = copy.deepcopy(node) + seen: set[int] = set() + for m in _metas(moved, []): + if id(m) in seen or m.empty \ + or getattr(m, "filename", None) != filename \ + or not start <= getattr(m, "start_pos", -1) <= end: + continue + seen.add(id(m)) + m.line += dline + m.end_line += dline + m.start_pos += dpos + m.end_pos += dpos + return moved + + +def lookup_type_check(module_name: str, key: tuple[int, int], layout: int, + stmt: Statement) -> Statement | None: + """The cached type check of ``stmt``, given its key and layout, + moved to where ``stmt`` is now.""" + entry = _type_check_cache.get(module_name, {}).get(key) + if entry is None or entry[0] != layout: + return None + _, old_first, checked = entry + first = statement_start(stmt) + if isinstance(checked, Theorem) and isinstance(stmt, Theorem): + # Type checking passes the proof through, so take it from + # ``stmt``, which is already where it belongs. + what = checked.what if first == old_first \ + else _relocated(checked.what, checked, old_first, first) + return Theorem(stmt.location, checked.name, what, stmt.proof, + checked.isLemma, visibility=checked.visibility) + if first != old_first: + checked = _relocated(checked, checked, old_first, first) + return checked + + +def store_type_checks(module_name: str, + entries: dict[tuple[int, int], _TypeCheckEntry]) -> None: + """Replace the module's cached type checks with ``entries``.""" + _type_check_cache[module_name] = entries diff --git a/checker_pipeline.py b/checker_pipeline.py index 3b34b646..e5cbf313 100644 --- a/checker_pipeline.py +++ b/checker_pipeline.py @@ -45,9 +45,11 @@ print_theorems, register_library_name, type_match, type_names, ) from checker_cache import ( - _collect_defined_names, _collect_referenced_names, _hash_ast, - _is_global_barrier, _prelude_imports_cache, _record_hit, _record_miss, - _stmt_cache, + TYPE_CHECK_CACHED_STATEMENTS, _TypeCheckEntry, _collect_defined_names, + _collect_referenced_names, _hash_ast, _is_global_barrier, + _prelude_imports_cache, _record_hit, _record_miss, _stmt_cache, + lookup_type_check, statement_layout, statement_start, store_type_checks, + type_check_fingerprints, ) from checker_common import * from checker_predicates import ( @@ -2517,17 +2519,52 @@ def _collect_diagnostic(exc: Diagnostic) -> None: ast3_hashes = [] error_on_next_import : dict[str, bool] = {} - for s, sh in ast2_pairs: + # Per-statement type-check cache (#1301). The postulate report records + # what each type check uses, so it needs every one to run. + use_type_check_cache = not get_postulate_report() + fingerprints = type_check_fingerprints( + [s for s, _ in ast2_pairs], [sh for _, sh in ast2_pairs]) \ + if use_type_check_cache else [] + type_checks: dict[tuple[int, int], _TypeCheckEntry] = {} + for i, (s, sh) in enumerate(ast2_pairs): + cacheable = use_type_check_cache \ + and isinstance(s, TYPE_CHECK_CACHED_STATEMENTS) and not s.location.empty + if cacheable: + tc_key = (sh, fingerprints[i]) + layout = statement_layout(s) + start = statement_start(s) + new_s = lookup_type_check(module_name, tc_key, layout, s) + if new_s is not None: + _record_hit("type_check") + type_checks[tc_key] = (layout, start, new_s) + ast3.append(new_s) + ast3_hashes.append(sh) + continue + _sink = get_active_sink() + _wsink = get_active_warning_sink() + pre_n = len(_sink) if _sink is not None else 0 + pre_wn = len(_wsink) if _wsink is not None else 0 try: new_s = type_check_stmt(s, env, error_on_next_import) except Diagnostic as e: _collect_diagnostic(e) continue + if cacheable and new_s is not None: + _record_miss("type_check") + # As for ``check_proofs`` below: a statement whose check reported + # an error or a warning must be checked again to report it again. + _sink = get_active_sink() + _wsink = get_active_warning_sink() + if (_sink is None or len(_sink) == pre_n) \ + and (_wsink is None or len(_wsink) == pre_wn): + type_checks[tc_key] = (layout, start, new_s) # If None gets returned we want to remove the current statement # Which is represented by not appending it to the new ast if new_s != None: ast3.append(new_s) ast3_hashes.append(sh) + if use_type_check_cache: + store_type_checks(module_name, type_checks) if get_verbose(): for s in ast3: print(s) @@ -2538,14 +2575,10 @@ def _collect_diagnostic(exc: Diagnostic) -> None: if get_verbose() and needs_checking[0]: print('checking ' + module_name) # Per-statement cache for ``check_proofs`` (Steps 13 + 14). - # Earlier loops (``process_declaration``, ``type_check_stmt``) - # emit AST nodes whose ``Meta`` locations participate in - # side-effecting behaviour (e.g. the ``target_hole_location`` - # flag used by ``goal_at`` to single out which `?` raises), so - # caching their outputs across calls would let stale locations - # leak into a new run. ``check_proofs`` itself is the bulk of - # the time and its only persistent effect is "verified" -- safe - # to cache. + # Its only persistent effect is "verified", so the cache stores + # just that. (The type-check cache above stores checked + # statements, so it has to move their locations; see + # ``checker_cache``.) # # Step 14: the cache key is ``(stmt_hash, deps_fingerprint, # target, module_name)`` where ``deps_fingerprint`` folds in diff --git a/docs/lsp-plan.md b/docs/lsp-plan.md index ead10410..373933f5 100644 --- a/docs/lsp-plan.md +++ b/docs/lsp-plan.md @@ -208,6 +208,7 @@ Steps 15 and 18 are duals: Step 15 is **introduction** (template chosen by the * - [x] **Faster lemma ranking (issue #1256, under the iPad umbrella #1213).** `available_lemmas_at` / `lemmas_for_subterm` took ~0.3 s per call on the Mac (~0.7 s on the iPad simulator), the slowest query at a hole. Three changes, with the same ranked results: `MatchFailed` renders its message only when it is read (`match_failed` takes a thunk), since ranking and `replace` discard almost every failed match and used to pretty-print two formulas for each one; `_prelude_lemma_candidates` caches the prelude's candidates and their `_formula_symbols`, reused while the uniquified prelude modules are the same objects; and `_equation_subterm_match` reduces a call-shaped equation side once instead of once per name or type subterm of the goal. At `length_append`'s `node` hole: 0.30 s → 0.027 s (0.095 s for the call that fills the cache). - [x] **A worker thread for checks; cancel stale checks (issue #1218, under the iPad umbrella #1213).** Every check and request used to run on pygls's I/O loop, one after another: a slow check (or query) blocked everything behind it, and a burst of edits queued a check per edit. Now `lsp/worker.py`'s `Worker` runs them, in order, on one thread with a large stack (the pipeline's module-level state rules out running two at once), and the I/O loop only reads messages and sends results. A new version of a document supersedes its check: a queued check is dropped and a running one is interrupted with `PyThreadState_SetAsyncExc` (raising `CheckSuperseded`), so a burst of edits ends in one completed check, of the final text. `$/cancelRequest` drops a queued request or interrupts a running one (`RequestCancelled`), and `lsp_server.interrupt()` (the iPad's Stop button) interrupts whichever is running. Request handlers stay plain functions, registered through `_feature`. The other half of the issue, re-parsing only the statements that changed, is left for later: re-checks already take ~0.2 s. Since an interrupt can land inside cleanup code, the worker resets the pipeline's scoped state (`library._SCOPED_STATE`) before every job. Tests in `test/lsp/test_lsp_worker.py`, including the server over stdio, and `test/lsp/test_interrupt_fuzz.py`. The design and its rules are in `docs/knowledge-base/lsp-concurrency.md`. +- [x] **Cache type checking per statement (issue #1301).** After the `check_proofs` cache, `type_check_stmt` was the largest per-check cost that did not depend on what changed (20–42% of a warm re-check). `check_deduce` now caches each statement's checked form per module, keyed by its structural hash and `checker_cache.type_check_fingerprints`: the statements defining the names it mentions, *transitively* (a function's type can come from a type alias), with every candidate of an overloaded name, plus every statement that is not a plain named definition anywhere in the file (views, models, `associative`, imports, ... are found by type or have file-wide effects, and the env is the whole file's). A statement that only moved reuses its entry with its own locations shifted (`_relocated`; a theorem takes its proof from the fresh parse). Statements whose check reported an error or warning are not cached, and the postulate report bypasses the cache. Each check replaces the module's entries with the ones it used, so the cache holds one file per module. On `test/should-validate/IntTests.pf` a warm re-check went from 0.273 s to 0.167 s (0.184 s after inserting a line), on `doc_reference.pf` from 0.237 s to 0.203 s (0.218 s). Tests in `test/lsp/test_type_check_cache.py`. - [x] **Using an `all` given with values (issue #1222, under the iPad umbrella #1213).** `eliminate_at` on an `all` given replaces the hole with `H[?]`, whose `?` is a placeholder term: rarely what's wanted. `instantiation_vars_at(path, content, pos, label, prelude=())` returns the variables of the given's first `all` block as frozen `InstantiationVar(name, type, suggestions)`: the suggestions are the goal's subterms of that type that are in scope at the hole (shortest first), then the variables in scope of that type. `instantiate_at(path, content, pos, label, values, prelude=())` returns the edit putting `have H: by label[values]` before the `?`, or the checker's message when the values don't check (a string). The formula is the checker's: `have H: ? by label[values]` is checked in place of the hole with the outline recorder on, and a `have` whose formula is `?` proves what its reason does. Exposed over the LSP as `deduce/instantiationVarsAt` and `deduce/instantiateAt`. `Term.free_vars` is now defined for every term (the union over its children, with `all`, `some`, `fun`, `define … in`, `generic` and `switch` cases subtracting what they bind). Tests in `test/lsp/test_instantiate.py`. diff --git a/test/lsp/test_check_proofs_cache.py b/test/lsp/test_check_proofs_cache.py index 582915e3..9f33b5eb 100644 --- a/test/lsp/test_check_proofs_cache.py +++ b/test/lsp/test_check_proofs_cache.py @@ -34,12 +34,8 @@ reference it as hits (Step 14's deps-fingerprint kicking in), - editing T1 invalidates T2 that uses it (Step 14 acceptance). -The earlier two loops (``process_declaration``, ``type_check_stmt``) -are deliberately *not* cached -- their AST output carries -``Meta`` locations that interact with the -``target_hole_location`` flag used by ``goal_at`` and friends, so -caching them across calls would surface as stale-location bugs. -``check_proofs`` is where the bulk of the time goes anyway. +``type_check_stmt`` has a cache of its own (#1301), tested in +``test_type_check_cache.py``. """ from __future__ import annotations diff --git a/test/lsp/test_snapshot.py b/test/lsp/test_snapshot.py index ffe5f9a4..764025d1 100644 --- a/test/lsp/test_snapshot.py +++ b/test/lsp/test_snapshot.py @@ -91,7 +91,8 @@ def test_second_start_loads_the_snapshot_instead_of_checking_the_prelude( # The statement cache saw only the user file's two statements (the # prelude's `import Mini` and the theorem): Mini's own statements, # checked in the first run, were never checked in this "process". - assert get_cache_stats()["misses"] == {"check_proofs": 2} + # (The type-check cache, #1301, saw only the theorem.) + assert get_cache_stats()["misses"] == {"check_proofs": 2, "type_check": 1} assert first_misses > 2 assert list((tmp_path / "snapshots").glob("prelude-*.pickle")) == [snapshot] diff --git a/test/lsp/test_type_check_cache.py b/test/lsp/test_type_check_cache.py new file mode 100644 index 00000000..b57cc717 --- /dev/null +++ b/test/lsp/test_type_check_cache.py @@ -0,0 +1,183 @@ +"""The per-statement type-check cache (#1301). + +``check_deduce`` caches each statement's type check, keyed by its +structural hash and a fingerprint of the statements it depends on (see +``checker_cache.type_check_fingerprints``). A statement that only moved +reuses its cached check with the locations shifted. + +What this file pins: + +- a re-check of unchanged content type-checks nothing, +- editing one statement re-checks only it and what depends on it, + including through a type alias and through every overload of a name, +- a barrier (here an ``auto``) invalidates everything, +- diagnostics of statements that moved are where a cold check puts them. +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +import pytest + +REPO_ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(REPO_ROOT)) + +import proof_checker # noqa: E402 +from lsp.query import Position, check, goal_at # noqa: E402 + + +@pytest.fixture(autouse=True) +def _fresh_cache(): + proof_checker.reset_stmt_cache() + yield + proof_checker.reset_stmt_cache() + + +def _recheck(content: str) -> tuple[int, int]: + """Check ``content`` and return the type-check (hits, misses).""" + proof_checker._cache_stats["hits"].clear() + proof_checker._cache_stats["misses"].clear() + assert check("test.pf", content) == [] + stats = proof_checker.get_cache_stats() + return stats["hits"].get("type_check", 0), stats["misses"].get("type_check", 0) + + +_FILE = """\ +union N { z s(N) } +union K { z } + +type M = N + +fun f(x : M) { x } + +fun g(x : N) { x } + +theorem uses_f: f(z) = z +proof + expand f. +end + +theorem uses_g: g(z) = z +proof + expand g. +end + +assert g(s(z)) = s(z) + +auto uses_g +""" +# The statements whose type check is cached: f, g, uses_f, uses_g, and +# the assert. (Edits below keep the number of statements: inserting one +# renames every statement after it, #1303.) + + +def test_unchanged_recheck_type_checks_nothing() -> None: + assert _recheck(_FILE) == (0, 5) + assert _recheck(_FILE) == (5, 0) + + +def test_moving_statements_type_checks_nothing() -> None: + _recheck(_FILE) + assert _recheck("\n\n" + _FILE) == (5, 0) + + +def test_moving_statements_apart_rechecks_one_carrying_anothers_location() -> None: + _recheck(_FILE) + # `f`'s type carries the location of the alias `M`, which now moved + # by a different amount than `f`, so `f` is checked again. + assert _recheck("\n\n" + _FILE.replace("\n\n", "\n\n\n")) == (4, 1) + + +def test_editing_one_theorem_rechecks_only_it() -> None: + _recheck(_FILE) + edited = _FILE.replace("theorem uses_f: f(z) = z", "theorem uses_f: z = f(z)") + assert _recheck(edited) == (4, 1) + + +def test_editing_a_function_rechecks_its_users() -> None: + _recheck(_FILE) + # `g`, and `uses_g` and the assert that mention it. + edited = _FILE.replace("fun g(x : N) { x }", "fun g(x : N) { s(x) }") \ + .replace("theorem uses_g: g(z) = z", "theorem uses_g: g(z) = s(z)") \ + .replace("assert g(s(z)) = s(z)", "assert g(z) = s(z)") + assert _recheck(edited) == (2, 3) + + +def test_editing_a_type_alias_rechecks_users_of_functions_that_mention_it() -> None: + _recheck(_FILE) + # `uses_f` mentions only `f`, but `f`'s type comes from `M`, so + # whether its `z` is N's or K's does too. + assert _recheck(_FILE.replace("type M = N", "type M = K")) == (3, 2) + + +def test_every_overload_is_a_dependency() -> None: + content = """\ +union N { z s(N) } +union B { t ff } + +fun h(x : N) { x } +fun h(x : B) { x } + +theorem uses_h: h(z) = z +proof + expand h. +end +""" + assert _recheck(content) == (0, 3) + # `uses_h` calls the first `h`, but editing the second one can + # change which one it resolves to. + edited = content.replace("fun h(x : B) { x }", "fun h(x : B) { t }") + assert _recheck(edited) == (1, 2) + + +def test_a_barrier_rechecks_everything() -> None: + _recheck(_FILE) + assert _recheck(_FILE.replace("auto uses_g", "auto uses_f")) == (0, 5) + + +_SHIFTED = """\ +union N { z s(N) } + +fun f(x : N) { s(x) } + +assert f(z) = z + +theorem holey: f(z) = s(z) +proof + ? +end + +theorem wrong: f(z) = z +proof + expand f. +end +""" + + +def _diagnostics(content: str) -> list[tuple[object, str]]: + return [(d.range, d.message) for d in check("test.pf", content)] + + +@pytest.mark.parametrize("prefix", ["\n\n\n", "// a comment\n", " "]) +def test_moved_statements_report_where_a_cold_check_does(prefix: str) -> None: + _diagnostics(_SHIFTED) + moved = prefix + _SHIFTED + warm = _diagnostics(moved) + proof_checker.reset_stmt_cache() + cold = _diagnostics(moved) + assert warm == cold + assert len(cold) == 3 + + +def test_goal_at_in_a_moved_theorem() -> None: + # Without the failing statements, which stop `goal_at`'s check. + content = _SHIFTED.split("assert")[0] + "theorem holey" \ + + _SHIFTED.split("theorem holey")[1].split("theorem wrong")[0] + check("test.pf", content) + moved = "\n\n" + content + line = moved.split("\n").index(" ?") + 1 + goal = goal_at("test.pf", moved, Position(line, 3)) + assert goal is not None + assert goal.range.start.line == line From 7b40a30018f8acaf5797fe976d2eed60a608f6c3 Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Fri, 20 Nov 2026 08:57:15 -0500 Subject: [PATCH 2/2] Type-check cache: a statement's layout includes its file (#1301) Two files with the same base name share a module name, so a check of one could reuse the other's checked statements, which still name the first file (Codex review on #1305). Co-Authored-By: Claude Opus 5.5 --- checker_cache.py | 10 ++++++---- test/lsp/test_type_check_cache.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/checker_cache.py b/checker_cache.py index 3aa6fbe4..5c75fca0 100644 --- a/checker_cache.py +++ b/checker_cache.py @@ -349,9 +349,11 @@ def _metas(node: object, out: list[Meta]) -> list[Meta]: def statement_layout(stmt: Statement) -> int: - """A hash of the statement's locations, relative to where it starts - when they are in its own file. Two statements with the same hash - (``_hash_ast``) and layout differ only in where they are.""" + """A hash of the statement's file and of its locations, relative to + where it starts when they are in its own file. Two statements with + the same hash (``_hash_ast``) and layout differ only in where they + are in the same file. (Two files with the same base name share a + module name, so the file must be part of it.)""" filename = getattr(stmt.location, "filename", None) line, pos = statement_start(stmt) sig: list[tuple[object, ...]] = [] @@ -365,7 +367,7 @@ def statement_layout(stmt: Statement) -> int: else: sig.append((getattr(m, "filename", None), m.line, m.column, m.end_line, m.end_column)) - return hash(tuple(sig)) + return hash((filename, tuple(sig))) def _relocated(node: T, old: Statement, old_first: tuple[int, int], diff --git a/test/lsp/test_type_check_cache.py b/test/lsp/test_type_check_cache.py index b57cc717..fff4dba1 100644 --- a/test/lsp/test_type_check_cache.py +++ b/test/lsp/test_type_check_cache.py @@ -35,11 +35,11 @@ def _fresh_cache(): proof_checker.reset_stmt_cache() -def _recheck(content: str) -> tuple[int, int]: +def _recheck(content: str, path: str = "test.pf") -> tuple[int, int]: """Check ``content`` and return the type-check (hits, misses).""" proof_checker._cache_stats["hits"].clear() proof_checker._cache_stats["misses"].clear() - assert check("test.pf", content) == [] + assert check(path, content) == [] stats = proof_checker.get_cache_stats() return stats["hits"].get("type_check", 0), stats["misses"].get("type_check", 0) @@ -90,6 +90,12 @@ def test_moving_statements_apart_rechecks_one_carrying_anothers_location() -> No assert _recheck("\n\n" + _FILE.replace("\n\n", "\n\n\n")) == (4, 1) +def test_a_file_with_the_same_base_name_does_not_reuse_entries() -> None: + # Both are module `test`, but a checked statement names its file. + _recheck(_FILE, "a/test.pf") + assert _recheck(_FILE, "b/test.pf") == (0, 5) + + def test_editing_one_theorem_rechecks_only_it() -> None: _recheck(_FILE) edited = _FILE.replace("theorem uses_f: f(z) = z", "theorem uses_f: z = f(z)")