BendTT name escaping merges distinct constructor labels across module paths
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 binaryReproduction
Create this layout:
name-collision/
main.bend
a-b/
lib.bendname-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.bendttThe verdict is:
ALL PROOFS CHECKThe 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.KBoth become the BendTT label:
a_2d_b_2f_lib.KCause
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.
Source: HigherOrderCO/Bend