You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
term_show cache collisions cause wrong template results and SHOW_DESC layouts #834
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:
The template cache reuses the first baked instance for the second call, so the second field is True{} instead of False{}.
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)
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.
Activity
Bend 2.0.11: term_show keys are unsugared, so templates and show desc…
VictorTaelin commentedon Sep 19, 2026
Fixed in Bend 2.0.11 (de20dcd39). Thank you, this was a real one.
The cause was as you said:
term_showwas 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 printsOut{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 ofEmptycheck; 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.
A key print carries no sugar: two template arguments share an instanc…
term_key is the identity of a term: the template memo and the show de…
Bend 2.0.11: term_show keys are unsugared, so templates and show desc…
A key print carries no sugar: two template arguments share an instanc…
term_key is the identity of a term: the template memo and the show de…