Skip to content

C emission references an undeclared constructor ID when a pure main exposes a datatype through a generic family field #1067

Closed

Description

@jasisz

What I did

I returned a generic record from a pure main. The record's field type is a family that reduces to a user datatype at this instantiation.

import Base

type Inner is Data:
  Inner{a: U32, b: U32}

def Pick(b: Bool) -> Data:
  match b:
    case False{}:
      U32
    case True{}:
      Inner

type Box<-b: Bool> is Data:
  Box{value: Pick(b)}

def main() -> Box<True{}>:
  Box{Inner{1, 2}}
bun bend2/main.ts repro.bend
bun bend2/main.ts repro.bend -o out.js
bun out.js
bun bend2/main.ts repro.bend -o out.c
clang -std=c11 -O2 out.c -lpthread -lm -o out

What happened

The evaluator and emitted JS both accept the program and print the expected value:

Box{Inner{1, 2}}

Emitting C also exits successfully, but the generated source does not compile:

out.c:409:79: error: use of undeclared identifier 'CID_INNER'
  409 | static const u32 SHOW_DESC[] = { ..., CID_INNER, ... };
      |                                       ^
1 error generated.

The generated declarations contain #define CID_BOX 13, but no definition for CID_INNER.

The same failure occurs when Inner has multiple constructors: SHOW_DESC references every hidden arm's CID, while none of those CID macros are declared.

Why it happens

compile_book computes the compiler reachability set before building the pure-main display descriptor. The generic Box field is seen as Pick(b) during reachability, so the concrete Inner datatype is not included among the constructors whose C IDs are emitted. show_main later reduces the concrete result type Box<True{}>, discovers Inner, and writes CID_INNER into SHOW_DESC anyway.

A direct Inner field makes the failure disappear because that separately roots its constructor ID. The failure therefore comes from disagreement between compilation reachability and the concrete type traversal used by show_main, rather than from the value itself.

Expected

A C file emitted successfully for an accepted program should compile and print the same Box{Inner{1, 2}} value as the evaluator and JS backend. Constructors referenced by the pure-main descriptor need to be included in the emitted CID table, including constructors reached only after reducing a family at a generic instantiation.

Environment

bend 2.0.27
commit 47bed75adedd6d6dd811721905f06569bf465f85
Bun 1.4.2
Darwin arm64
Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. VictorTaelin commented on Sep 27, 2026

    @VictorTaelin
    Contributor

    Fixed in Bend 2.0.32 (573002f): a pure main's printed datatypes are build roots, so a family field's datatype gets its constructor IDs.

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