Skip to content

BendTT name escaping merges distinct constructor labels across module paths #1131

Closed

Description

@mike-reese

Summary

BendTT name escaping is not injective for constructor labels. Two distinct
Bend source constructors can therefore become the same semantic label in the
generated BendTT book.

For example, a literal source substring _2d_ collides with the encoding of
-, and _2f_ collides with /.

Top-level BendTT definition names go through fresh, so one colliding
definition receives an underscore suffix. Constructor labels do not go
through fresh. In the reproduction below, two nominally distinct source
datatypes consequently receive structurally identical BendTT bodies.

I did not derive a proof of Empty from this. I am reporting it as a
translation correctness and hardening issue, not as a bounty claim.

Version

bend 2.0.32
v2.0.32 commit 573002f01ec6c52416d44489543f69a9625facf8
Linux x86-64 release binary

Reproduction

Create this layout:

name-collision/
  main.bend
  a-b/
    lib.bend

name-collision/a-b/lib.bend:

type Good is Data:
  K{}

name-collision/main.bend:

import ./a-b/lib.bend as Far

type a_2d_b_2f_lib.Good is Data:
  a_2d_b_2f_lib.K{}

def imported() -> Far.Good:
  Far.K{}

def local() -> a_2d_b_2f_lib.Good:
  a_2d_b_2f_lib.K{}

Run:

bend name-collision/main.bend --verdict
bend name-collision/main.bend -o /tmp/name-collision.bendtt

The verdict is:

ALL PROOFS CHECK

The relevant generated BendTT is:

a_2d_b_2f_lib.Good.arms : ∀x0 : <a_2d_b_2f_lib.K> -> *2 =
  λ{.a_2d_b_2f_lib.K: <()>; λ{}}

a_2d_b_2f_lib.Good : *2 =
  Σx0 : <a_2d_b_2f_lib.K> -> (a_2d_b_2f_lib.Good.arms x0)

a_2d_b_2f_lib.Good_.arms : ∀x0 : <a_2d_b_2f_lib.K> -> *2 =
  λ{.a_2d_b_2f_lib.K: <()>; λ{}}

a_2d_b_2f_lib.Good_ : *2 =
  Σx0 : <a_2d_b_2f_lib.K> -> (a_2d_b_2f_lib.Good_.arms x0)

imported : a_2d_b_2f_lib.Good =
  (.a_2d_b_2f_lib.K, ())

local : a_2d_b_2f_lib.Good_ =
  (.a_2d_b_2f_lib.K, ())

The two source constructors are:

a-b/lib.K
a_2d_b_2f_lib.K

Both become the BendTT label:

a_2d_b_2f_lib.K

Cause

name_tt
leaves _ and hexadecimal-looking text unchanged while replacing unsupported
characters with _hex_:

function name_tt(k: Name): string {
  return k.replace(/[^A-Za-z0-9_.]|^[.0-9]/g, (c) => "_" + (c.codePointAt(0) ?? 0).toString(16) + "_");
}

Datatype enum labels and match arms use that value directly. Mutually
recursive group selector labels use the same encoding, so the same collision
class appears possible there.

This seems closely related to the uniqueness work in #1042, but it occurs in
the Bend-to-BendTT proof translation rather than the source name table or
runtime identifiers.

Expected behavior

Distinct canonical Bend constructor names should produce distinct BendTT
labels, or the translator should reject a collision.

Suggested fix

Use an injective encoding for semantic labels and apply one canonical mapping
consistently to:

  • datatype enum labels;
  • constructor tuples;
  • match arms;
  • mutually recursive group selectors.

One simple encoding is a length-prefixed hexadecimal encoding of every UTF-8
byte, including underscores. A per-book Map<Name, string> that allocates
unique labels would also work if every label-emitting path uses it.

The exact two-file example above would make a useful regression test. The
generated constructor labels should differ. Related cases include _2f_
versus /, hyphenated package namespaces, and group member selectors.

Activity

  1. Vacrin commented on Sep 30, 2026

    @Vacrin

    Two more consequences of name_tt not being injective
    (safe.ts#L484-L486:
    _ is never escaped, so a-b → a_2d_b, the same as name_tt("a_2d_b")). Def names are made unique
    afterwards by fresh; labels aren't.

    1. A group selector with two colliding members is unparsable. A mutually recursive group whose
    members are L.k (from a-b.bend, imported as L) and a local a_2d_b.k gets the selector enum
    <a_2d_b.k, a_2d_b.k>:

    # ---- a-b.bend ----
    import Base
    
    law k:
      for n: Nat
      Nat
    
    # ---- main.bend ----
    import Base
    import ./a-b.bend as L
    
    def keep(n: Nat, -m: Nat) -> Nat:
      n
    
    def a_2d_b.k(n: Nat) -> Nat:
      keep(n, L.k(n))
    
    def L.k(n):
      match n:
        case 0n:
          0n
        case 1n+p:
          a_2d_b.k(p)
    $ bend main.bend --check-only     ALL PROOFS CHECK
    $ bend main.bend --verdict        SOME PROOFS FAIL (MISMATCH)
    kernel on the -o text:            parse error: expected a fresh label at '> -> (λ{.a_2d_b.k: <()>; λ{.a_ …'
    

    Renaming the helper a_2d_b.k to h passes --verdict.

    2. With a selector misroute, the collision becomes a silent PASS. In #1212 , safe.ts routes a call
    through a group the callee isn't a member of. Normally the label falls outside the enum and the kernel
    rejects it. When the non-member's name collides with a real member's under name_tt, the label lands
    inside the enum and the kernel accepts: fb3.bend there PASSes --verdict while the certified book
    computes main == True and bend2 runs it to False.

    Constructor labels share the same way ((.a_2d_b.C, ()) for two different constructors); that's
    sound on its own, since bend2 is nominal and the kernel keeps the types apart through their stuck
    *.arms heads, but it removes a layer of defense.

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