Skip to content

def_inst reuses template specializations across unrelated functions due to un-scoped term_key collisions #1204

Closed

Description

@Vacrin

What you did

bend key_collision.bend --check-only

What happened

$ bend key_collision.bend --check-only
ALL PROOFS CHECK

The file

import Base

def app(~f: Unit -> Nat) -> Nat:
  f(Unit{})

def g(x: Nat) -> Nat:
  app(~{\{Unit: 0n; x} : Unit -> Nat})

def h(x: Bool) -> Nat:
  app(~{\{Unit: 0n; x} : Unit -> Nat})

def main() -> Nat:
  _ = g(42n)
  h(True{})

In bend2/bend.ts:3726-3736:

const key = xs.map((a) => term_key(term_lower(a))).join("\n");
...
const is = book.tmps[tm.k] ??= Object.create(null);
if (is[key] === undefined) {
  const o = is[key] = tm.k + "~" + String(Object.keys(is).length);
  ...
}
return is[key];
  1. term_lower(a) strips context and typing information, lowering terms to LTerm relative to the local telescope depth.
  2. term_key (bend.ts:1192-1194) serializes the stripped AST: JSON.stringify(tm, (k, v) => k === "s" ? undefined : v).
  3. In g(x: Nat) and h(x: Bool), the variable x is lowered to Var("x", 0). Because term_key contains neither the variable's type, the enclosing definition name (lhs.def), nor lexical origin, both template arguments produce the identical JSON string.
  4. book.tmps is a global table keyed only by template name (tm.k, e.g. "app"). Because is[key] was already populated during the check of g, h encounters a cache hit and reuses app~0!

Distinction

This demonstrates that the template caching subsystem has an independent hygiene flaw: cache keys lack lexical qualification, causing cross-function specialization collisions whenever two callers construct structurally identical template arguments.

Suggested fix

Incorporate the caller's definition name or fully typed telescope context into the cache key in def_inst, or include variable types in term_key.

bend --version

bend 2.0.32

uname -sm

Linux x86_64

clang --version (the first line)

No response

Activity

  1. added
    bug
    on Sep 29, 2026
  2. quimmedes commented on Sep 30, 2026

    @quimmedes

    Confirmed still alive on HEAD 01875127 (2026-09-29), and I can sharpen the root cause: the "closed ~arguments" guard is domain-asymmetric, which is what makes the collision reachable at all.

    Minimal pair (only the ~f domain differs, the defaulted arm x is identical):

    Rejected (Bool domain — x free in the default arm caught):

    def app(~f: Bool -> Nat) -> Nat:
      f(True{})
    
    def g(x: Nat) -> Nat:
      app(~{\{False: 0n; x} : Bool -> Nat})

    expected : a template applied to closed ~ arguments (x is a variable here, not comptime: pass it at run time)

    Accepted (--check-only green): Unit domain, same open default arm:

    def app(~f: Unit -> Nat) -> Nat:
      f(Unit{})
    
    def g(x: Nat) -> Nat:
      app(~{\{Unit: 0n; x} : Unit -> Nat})
    
    def h(x: Bool) -> Nat:
      app(~{\{Unit: 0n; x} : Unit -> Nat})

    So a free run-time variable in a defaulted arm passes the guard when the domain is a single-label ADT (where the default is unreachable at run time) and fails when the domain has more labels — the guard seems to key on arm reachability rather than on the argument actually being closed. The term_key collision in my report above then rides on top of this: both g and h submit "open-but-accepted" arguments that serialize identically, and h reuses g's app~0 without its own instantiation ever being checked.

    Verdict behavior of the collision repro (same file as above, plus a main): --verdict on HEAD returns the auto-confessed mismatch:

    SOME PROOFS FAIL
    Sorry - this is a mismatch between the TypeScript implementation, and the formalized BendTT kernel.
    

    i.e. the kernel refuses the emitted book that carries the dangling x — so the leak currently lands on the safe side, but the specialization cache itself is unsound: a Bool value flows through a Nat-typed spine (app(~f: Unit -> Nat) applied with an argument whose default arm is x : Bool yields h(True{}) computed as True{} at type Nat). A fix scoped only to term_key (types in the key / caller scoping) would keep the guard asymmetry; fixing both closes the family.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions