Skip to content

C facts ignore a concrete type argument of a stuck family, so a checked True becomes False #916

Closed

Description

@jasisz

On current main (6018e28e, Bend 2.0.21), facts_fam does not visit a concrete type argument of a stuck family. The checker proves the result is True{}, while native C deterministically returns False{}. The file has no templates, @unsafe, FFI, holes or open user laws.

import Base

type RT is Data:
  RT{a: U32, b: U32, c: U32}

law F:
  for n: Nat
  for A: Data
  Data

def F(n, A):
  match n:
    case 0n:
      U32
    case 1n+p:
      A

type Carrier<-n: Nat> is Data:
  Carrier{item: F(n, RT), tail: Maybe<&2, Carrier<n>>}

def hold(n: Nat) -> Nat:
  n

def opaque_one() -> Nat:
  a b = hold(0n) hold(0n)
  1n+a

def make(+n: Nat, +x: U32) -> F(n, RT):
  match n:
    case 0n:
      0
    case 1n+p:
      RT{x, U32.add(x, 1), U32.add(x, 7)}

def pack(+n: Nat, x: F(n, RT)) -> Carrier<n>:
  Carrier{x, None{}}

def first(c: Carrier<0n>) -> U32:
  Carrier{x, tail} = c
  x

def heat(+c: Carrier<0n>) -> U32:
  U32.add(first(c), first(c))

def use(+n: Nat, c: Carrier<n>) -> U32:
  match n:
    case 0n:
      Carrier{x, tail} = c
      x
    case 1n+p:
      Carrier{x, tail} = c
      RT{a, b, c} = x
      U32.add(a, c)

def result() -> U32:
  +n = opaque_one()
  U32.add(heat(Carrier{U32.from_nat(n), None{}}), use(n, pack(n, make(n, 10))))

def runtime_tag() -> Bool:
  U32.is_eq(result(), 29)

def runtime_tag_ok() -> {runtime_tag() == True{} : Bool}:
  {==}

def main() -> Bool:
  runtime_tag()
$ bun bend2/main.ts arg.bend --check-only
All terms check.
$ bun bend2/main.ts arg.bend
True{}
$ bun bend2/main.ts arg.bend -o out.js && bun out.js
True{}
$ bun bend2/main.ts arg.bend -o out && ./out
False{}

The C result is deterministic. Returning result() directly gives 29 in the evaluator and JS and 2198905222 in C on my machine.

facts_fam receives F(n, RT) and term_spine contains both actual arguments [n, RT], but the body it walks is the generic body:

Zero -> U32
Succ -> A

It marks the generic A as unknown, caches only m:F, and returns from every later facts_hot(F(...)) without walking the actual RT argument. CID_CARRIER becomes hot while CID_RT remains cold. The hot Carrier build emits rfc_seal(e, x), then the Succ consumer takes that sealed value as a raw three-word RT through term_loc, reads the seal cell as the record, and frees it at class 3.

Two controls isolate this path:

  • Replacing the type parameter with a monomorphic family whose Succ arm returns RT makes C print True{}.
  • In a temporary compiler copy, walking the concrete family arguments before returning from facts_fam also makes C print True{}.

This is a remaining case related to #853, whose 2.0.17 fix introduced facts_fam. It is separate from #901's nested Mat: recursively walking Mat fixes #901 but leaves this repro at False{}, while walking the concrete family arguments fixes this repro but leaves #901 at False{}.

Activity

  1. VictorTaelin commented on Sep 21, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.23, thank you for the repro and the two controls, which pointed straight at the cause. facts_fam walked a stuck family's arms once per family and never its arguments, so RT stayed cold while Carrier was hot. It now walks the family's arguments at every instantiation, as the datatype case already did for a type's arguments. Your program prints 29 on the C lane; the emitted C of every existing test is unchanged. Pinned as tests/run/family_arg_hot.bend. bend update brings it.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here 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