Skip to content

c-migrate: designated inits over payload-enum carriers (tag + anonymous union) are untranslatable #749

Closed

Description

@ehartford

Surfaced by

The #746 fix: record initializers that cannot recover field names now fail
loudly instead of emitting positional (invalid-With) literals. The first such
construct in the prelude-bearing fixture is:

migrate: untranslatable function 'str_to_cstring__83': unsupported assignment rhs in goto CFG (kind=100) at .../generic_intrinsics.w:439:10

The construct

The emitted compiler C models With payload enums as tag + anonymous union:

struct Result_CString__CStringError_ {
    int32_t tag;
    union {
        CString payload0;
        int32_t payload1;
    };
};
...
_8 = (Result_CString__CStringError_){.tag = 1, .payload1 = 0};

Two gaps stack up:

  1. with_cimport_struct_field_count / the record-field queries do not
    enumerate through the anonymous union member (the typedef-of-anon path is
    also a stubbed TODO returning -1 at ClangBridge.w's
    with_cimport_typedef_anon_record_field_count).
  2. Even with names, the migrator's designated-init lowering pairs children
    with fields BY INDEX (lower_init_list_ir), which cannot express a
    partial, designator-selected init — .payload1 selects a union arm, not
    the field at index 1.

Correct translation

These carriers are the emit-C rendering of With enums; the faithful migration
of {.tag = K, .payloadN = expr} is the corresponding With enum-variant
construction (variant K with payload expr), not a struct literal. The
migrator needs a payload-enum recognition pass (tag + lone anonymous union is
exactly the emit-C carrier shape) or, failing that, honest per-designator
lowering (store to .tag, store through the union arm).

Priority

On the roundtrip's critical path: the emitted compiler C is full of Result/
Option carriers, so migrate-compiler-c stops here immediately after #744's
pin lets it run. Repro is fast (~2 min): migrate the 5 MB
emit_c_generic_intrinsics_case fixture C.

Activity

  1. ehartford commented on Jul 31, 2026

    @ehartford
    ContributorAuthor

    Deeper root cause, one layer below the init-list: the DECL translation is already wrong.

    The migrated carrier type is type Result_CString__CStringError_ { @[align(8)] tag: c_int = 0 } — one field, wrong size, and every downstream .payload0/.payload1 access references a field that does not exist.

    Why: collect_field (src/compiler/ClangBridge.w:993) visits record children and keeps only CXCursor_FieldDecl. A C11 anonymous member (union { ... }; with no declarator) has no FieldDecl cursor — libclang surfaces only the bare UnionDecl child — so with_cimport_struct_field_count returns 1 for the carrier and the struct renderer never sees the union. The existing anon-record machinery (ci_field_cursor_anon_record_decl, ci_translate_anon_record_cursor, synth naming) only handles NAMED fields of anonymous types (union {...} u;), which do get a FieldDecl.

    Fix layering, bottom up:

    1. Bridge: surface anonymous members in field enumeration — an anonymous record child that no FieldDecl consumes is a member; expose it as an unnamed field of the anon record type (clang_Cursor_isAnonymous is already bound; the field cache needs a synthetic entry carrying the anon decl cursor).
    2. Decl translation: with the member visible, the existing synth-type path renders Parent_anon_N = union {...} + a named field — With unions exist (type X = union {...} is already emitted for top-level unions at CImport.w:2204).
    3. Init lowering: designated inits then need designator-aware lowering (.tag, .payloadN through the member) — index-pairing cannot express union-arm selection; or, better, recognize the tag+lone-anon-union shape as the emit-C enum carrier and lower to variant construction.
    4. Until 1–3 land, the c-migrate: goto-CFG memcpy path renders positional struct literals (invalid With) #746 loud gate keeps this from producing silently-wrong output.

    Also worth noting for scoping: with_cimport_typedef_anon_record_field_count (ClangBridge.w:2573) is a stubbed return -1 // TODO — the same campaign should implement or remove it per no-silent-fallbacks.

  2. ehartford commented on Jul 31, 2026

    @ehartford
    ContributorAuthor

    Blast-radius census from the first-ever complete migration scan of the compiler C (roundtrip ran end-to-end under a keep-awake hold, 639 s total, uncapped per the D28 pin — no memory death, no environment death):

    1,111 untranslatable functions, all reporting unsupported assignment rhs in goto CFG (kind=100) — the payload-enum carrier shape. The inventory spans the whole compiler: the std.regex family, InternPool intern paths, large swaths of AstPool, and onward (full list in out/command/emit-c-roundtrip/migrate-compiler-c.stderr). Result/Option carriers are everywhere the compiler returns fallible values, so this issue IS the roundtrip gate — nothing else is queued behind mystery anymore: the memory chain (#744 pin), the loud-failure conversion, and the field-name recovery (#746) all held; migration now runs its full course deterministically and stops exactly here.

    Also confirms the fix must be the enum-carrier recognition path rather than per-designator stores: at 1,111 sites the faithful-translation shape (tag + lone anonymous union → With enum variant construction) is the only one that scales.

  3. ehartford commented on Aug 1, 2026

    @ehartford
    ContributorAuthor

    Major progress in 2545b05 — the initializer side is done:

    • Bridge: collect_field enumerates C11 anonymous members (FieldInfo now carries the field cursor; nameless members answer offsets via clang_Cursor_getOffsetOfField); ci_field_cursor_anon_record_decl detects members by cursor identity instead of the version-dependent "(unnamed ...)" spelling — that spelling mismatch is what silently demoted every carrier decl to opaque.
    • Decls: carriers render faithfully (type X_anon_0 = union { payload0: ..., payload1: ... } + anon_0 member), matching sizes/offsets.
    • Inits: designator-driven lowering (designators recovered from source text — libclang's C visitor doesn't surface them as cursors); arm-correct nested union literals; C's {0} lowers to a zero marker; designated-init assignments print as memset + field stores (C's own semantics), removing all dependence on With field defaults.

    The 5 MB prelude-bearing fixture C now migrates with zero untranslatable functions (this issue's class was 1,111 functions across the compiler C). Migrator basic/core lanes green.

    Remaining for the roundtrip:

    1. Access hop: payload reads still render .payload0 without the .anon_0 member hop (masked in the fixture check by item 2 aborting early). The member-ref lowering needs the same anon-member resolution: when the referenced field's parent is an anonymous member, insert the synthesized member name.
    2. extern fn 'write' shadows prelude function 'write' on the migrated output — standalone-check artifact or real migrated-compiler-build blocker, to be determined at the roundtrip's build-migrated step.
  4. ehartford commented on Aug 1, 2026

    @ehartford
    ContributorAuthor

    Access side done in 6bed71c: with_ci_member_field_name returns the full dotted path through anonymous members (semantic-parent chain walk, ordinals by cursor equality), so x.payload0 renders x.anon_0.payload0 — 27 fixture access sites carry the hop, none remain bare. Prelude-collision renames also landed: main-file C decls named write/print/etc. rename with @[link_name] preserving C linkage (the emitted compiler C declares POSIX write through std.libc's link-named externs, which round-tripped into a prelude shadow).

    Fixture state: migrates fully; with check of the output is down from the 1,111-function class to ONE new error family (~11): the migrated program's own runtime types (CString, Regex, Captures, ...) collide with the embedded std's types under a prelude build — duplicate implementation of trait, Copy-with-Drop conflicts. --no-prelude is not the answer (the output depends on prelude surface: ~993 errors).

    Next design question for the roundtrip's build-migrated step: how a migrated self-hosting program relates to the std it embeds — dedup/alias identical type definitions against the prelude, rename-namespace the migrated copies, or a dedicated migrate-build mode. That wants the decision procedure (spec §type-identity, reference projects, mission) rather than an ad-hoc pick. Migrator basic/core lanes green on everything above.

  5. ehartford commented on Aug 1, 2026

    @ehartford
    ContributorAuthor

    The 'type identity design question' was a false fork — the spec already rules it (§18.1 name precedence; the #627 shadowable-aliases ruling). The implementation is non-conforming: visible user declarations lose to prelude/std names (probes in #750). Filed as #750 with the fix scope; it is the roundtrip's next gate after this issue's migrator work. When #750 lands, the prelude-surface renames from 6bed71c revert (link_name emission stays).

  6. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-08-31: CLOSE (overcome by events). Landed in 2545b05 + 6bed71c — tag + anonymous-union designated inits now migrate to named anon-member inits (Res { tag: 0, anon_0: Res_anon_0 { payload0: 42 } }) and the output checks clean.

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