Skip to content

term_show cache collisions cause wrong template results and SHOW_DESC layouts #834

Closed

Description

@jasisz

What you did

bend repro.bend
bend repro.bend -o repro && ./repro
bend repro.bend -o repro.js && bun repro.js

What happened

Expected:

Out{True{}, False{}, Box{(8, 9)}, Box{7}}

bend repro.bend:

Out{True{}, True{}, Box{(8, 9)}, Box{7}}

./repro:

Out{True{}, True{}, Box{(8, 9)}, Box{(7, 51412992)}}

bun repro.js:

bend: runtime fail-stop

The two Array terms have the same leaves but different tree shapes. term_show prints both as [1, 2, 3]. That string is used as an identity key in two places:

  1. The template cache reuses the first baked instance for the second call, so the second field is True{} instead of False{}.
  2. show_main reuses the descriptor for the Box whose field is U32 & U32 for the Box whose field is U32. The generated C main returns WL_RETN(5), but the reused descriptor reads the final Box at absolute word offsets 4 and 5. Word 5 does not belong to main's result; 51412992 is the adjacent heap word observed in this run. The JS lane fail-stops on the same descriptor mismatch.

This seems separate from WONTFIX #808. Neither Array is executed at runtime: the baked arguments are compile-time values, and the Box arguments are erased type indices. The generated main returns only the two booleans and the three U32 words.

Tested on current main at 0da46e82e6e964e1e998f20be378be61f571fb01.

The file

import Base

def left_is_leaf(xs: Array<U32>) -> Bool:
  match xs:
    case ALeaf{x}:
      False{}
    case ANode{ALeaf{x}, r}:
      True{}
    case ANode{ANode{a, b}, r}:
      False{}

def baked(~xs: Array<U32>) -> Bool:
  left_is_leaf(xs)

def LowType(xs: Array<U32>) -> Type:
  match xs:
    case ALeaf{x}:
      U32
    case ANode{ALeaf{x}, r}:
      U32 & U32
    case ANode{ANode{a, b}, r}:
      U32

type Box<-xs: Array<U32>> is Type:
  Box{value: LowType(xs)}

type Out is Type:
  Out{
    first: Bool,
    second: Bool,
    wide: Box<ANode{ALeaf{1}, ANode{ALeaf{2}, ALeaf{3}}}>,
    narrow: Box<ANode{ANode{ALeaf{1}, ALeaf{2}}, ALeaf{3}}>
  }

def main() -> Out:
  Out{
    baked(~ANode{ALeaf{1}, ANode{ALeaf{2}, ALeaf{3}}}),
    baked(~ANode{ANode{ALeaf{1}, ALeaf{2}}, ALeaf{3}}),
    Box{(8, 9)},
    Box{7}
  }

Version

bend 2.0.9
Darwin arm64
Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Fixed in Bend 2.0.11 (de20dcd39). Thank you, this was a real one.

    The cause was as you said: term_show was the identity key of both caches (the template specialization table and the compiled main's show descriptor table), and its Array sugar prints every tree with the same leaves as one [1, 2, 3]. The key now skips that sugar, the only non-injective one, so the two shapes get their own specialization and their own descriptor. Your program prints Out{True{}, False{}, Box{(8, 9)}, Box{7}} on the interpreter and the C build. The JS build still fail-stops on it, but for the reason in #808: an unbalanced Array tree at runtime, not the key.

    Both cases are regression tests now (tests/comptime/template_key_shape.bend, tests/check/template_key_empty.bend). The same collision also let a template-typed proof of Empty check; that is closed by this fix too.

    This reply was written by an AI after reporting to me and my decision. If it 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