Skip to content

Duplicate template binder names collide during generic checking, forge Data, and admit Empty #905

Closed

Description

@jasisz

What I did

bun bend2/main.ts duplicate_binder_curry.bend --check-only

What happened

All terms check.

The file has a closed boom() -> Empty and derives False{} == True{} from it. It does not import Base and uses no @unsafe, FFI, law, hole, native definition, or runtime backend.

type Empty is Data:

type Bit is Data:
  False{}
  True{}

def cast(~T: Type, ~x: T, ~T: Type) -> T:
  x

type Bad is Type:
  MkBad{apply: @+x: cast(~Type, ~Bad, ~Data) -> Empty}

def Bad.apply(b: Bad) -> (@+x: cast(~Type, ~Bad, ~Data) -> Empty):
  match b:
    case MkBad{f}:
      f

def step(+b: cast(~Type, ~Bad, ~Data)) -> Empty:
  Bad.apply(b)(MkBad{Bad.apply(b)})

def boom() -> Empty:
  step(MkBad{step})

def absurd(-A: Type, e: Empty) -> A:
  match e:

def false_theorem() -> {False{} == True{} : Bit}:
  absurd({False{} == True{} : Bit}, boom())

Why it happens

def_check names each opaque template parameter with k + "~" + h.k. The two source binders named T therefore both become cast~T, and the second declaration overwrites the first one in the generic checking book. That makes this invalid generic cast check as though its source and target types were the same.

Renaming the last binder makes the same definition fail at its body, as expected:

def cast(~T: Type, ~x: T, ~U: Type) -> U:
  x
Error:
- expected : cast~U
- observed : cast~T
Location: cast

The exploit uses cast(~Type, ~Bad, ~Data) only in type positions. Its declared result type is Data, so the checker accepts it as the domain of a + binder. Normalization unfolds the raw template body to Bad, which is actually Type-kinded. This gives the negative Bad value a forged contraction license, and the ordinary Curry term in step inhabits Empty.

This is separate from #902. No live cast instance is created here; after checking the file, book.tmps["cast"] is still empty. A live use of the same cast does create an instance and is rejected at cast~0 with expected Data, observed Type. The hole is specifically the generic opaque-name collision combined with type-position template reduction.

Version

bend 2.0.21
main commit 8a51f9de
Bun 1.4.2
Darwin arm64
Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. jasisz commented on Sep 20, 2026

    @jasisz
    ContributorAuthor

    There is a second way to collide with the same opaque name construction. This one uses no duplicate binder names, so rejecting repeated names alone would not close #905.

    Put these two files in one directory.

    cast~T.bend:

    type Empty is Data:
    
    type Bad is Type:
      MkBad{apply: Bad -> Empty}
    
    def Bad.apply(b: Bad) -> (Bad -> Empty):
      match b:
        case MkBad{f}:
          f
    
    def x() -> Type:
      Bad
    

    main.bend:

    import ./cast~T.bend as M
    
    type Unit is Data:
      Unit{}
    
    type Bit is Data:
      False{}
      True{}
    
    def cast(~T.x: Data) -> Data:
      M.x
    
    def step(+b: cast(~Unit)) -> M.Empty:
      M.Bad.apply(b)(M.MkBad{M.Bad.apply(b)})
    
    def boom() -> M.Empty:
      step(M.MkBad{x => step(x)})
    
    def absurd(-A: Type, e: M.Empty) -> A:
      match e:
    
    def false_theorem() -> {False{} == True{} : Bit}:
      absurd({False{} == True{} : Bit}, boom())
    

    On main at 6018e28e:

    $ bun bend2/main.ts main.bend --check-only
    All terms check.
    

    The imported file's namespace makes M.x resolve to the real book key cast~T.x. Generic checking of the template cast creates the opaque parameter name cast~T.x from k + "~" + h.k, shadowing that imported definition in gen.tlds. The body therefore checks as a value of Data. Outside the temporary generic book, the same reference is the real x : Type, and normalization reduces it to Bad. The type-only call cast(~Unit) creates no instance, so it supplies a forged Data license for Bad and the Curry term inhabits Empty.

    After checking, book.tmps["cast"] has zero entries. Renaming the binder to ~U rejects the generic body with expected Data, observed Type. Adding a live def live() -> Data: cast(~Unit) creates cast~0 and rejects that instance with the same mismatch. The hole is the collision with a user-representable imported name during generic checking, independently of duplicate source binders.

  2. VictorTaelin commented on Sep 21, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.23, and thank you for an analysis that was right in every detail. A template's leading ~ binders become named opaque constants for the generic check, so two of one name collided. A template (a def, or a law with for ~T clauses) now refuses a second ~ binder of the same name at the binder:

    - expected : a fresh ~ binder name
    - observed : T
    

    Your file is refused at cast; the renamed version still fails at its body as before. Pinned as tests/check/template_dup_binder.bend. bend update brings it.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions