#1205·Bend

The unsafe/foreign gate misses an @unsafe dependency that appears only in a datatype's is Kind(..)

Author: VacrinCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

What you did

Put the same @unsafe call in two positions — a def body, and a datatype's kind — and ran --verdict on each.

What happened

A minimal pair, identical but for where the call sits:

python
# control.bend — the reference sits in a def
import Base

def qsize(j: Array<Nat>) -> Quant:
  match j:
    case ALeaf{value}: &1
    case ANode{xs, ys}: &1

def qv() -> Quant:
  qsize(Array.join(Nat, ALeaf{0n}, ALeaf{0n}))

type D is Kind(qv()):
  Mk{}

def use(d: D) -> Nat:
  match d:
    case Mk{}: 0n
python
# attack.bend — the identical expression inlined into the kind
import Base

def qsize(j: Array<Nat>) -> Quant:
  match j:
    case ALeaf{value}: &1
    case ANode{xs, ys}: &1

type D is Kind(qsize(Array.join(Nat, ALeaf{0n}, ALeaf{0n}))):
  Mk{}

def use(d: D) -> Nat:
  match d:
    case Mk{}: 0n
$ bend control.bend --verdict
SOME PROOFS FAIL
Error: 1 def relies on unsafe or foreign code:
- qv                                                   (exit 1)

$ bend attack.bend --verdict
ALL PROOFS CHECK                                       (exit 0)

Array.join is @unsafe (base.bend:2323-2325).

book_promises (main.ts:733) gathers each tld's references with:

typescript
for (const c of t.$ === "ADT" ? t.c : [t]) {
  term_refs(Bend.term_lower(c.T), rs);
}
term_refs(t.$ === "Def" ? t.e : undefined, rs);

For an ADT it walks each constructor's type c.T and never the datatype's own kind t.T, although ADT carries one (bend.ts:316: { $: "ADT"; n; g; T: HTerm; c: Ctrs; b? }).

Two consequences:

  1. A kind's references never enter uses, so the promise flood never reaches the datatype or its users;
  2. For a zero-constructor datatype t.c is [], so the loop body never runs and nothing about that declaration is walked at all.

Expected

A datatype's kind is part of its meaning — it is what spec_of/root_cols reduce — so it should be walked like a constructor field type.

Suggested fix (one line, tested)

diff
-      for (const c of t.$ === "ADT" ? t.c : [t]) {
+      for (const c of t.$ === "ADT" ? [t, ...t.c] : [t]) {

With this fix, attack.bend gives 2 defs rely on unsafe or foreign code: - D - use, exit 1, and control.bend is unchanged.

bend --version

bend 2.0.32

uname -sm

Linux x86_64

clang --version (the first line)

No response