Skip to content

emit-c silently skips all non-Vec destructors (Drop emitted as comment) #983

Closed

Description

@eas4ai

Classification

Silent backend semantic divergence (audit finding CCGEN-002). LLVM-vs-C behavioral split with no diagnostic and no documented limitation.

Source chain

StmtKind.Drop (src/CCodegen.w:8155-8160) and TK_DROP_AND_GOTO (:8232-8241) emit /* drop(p); */ COMMENTS for every non-Vec type — the C backend runs NO user destructors (files, sockets, mutexes, Boxes) and frees nothing. requirements.md calls --emit-c THE C source backend for bootstrapping new platforms, i.e. a real execution path, not a debug view; no documented limitation was found. Mission-level leak/defect gap on the C path, plus behavior divergence (RAII guards that release on LLVM never release on C).

Repair boundary

Either implement drop glue in the C backend or make --emit-c refuse programs with non-Vec Drop obligations loudly (and document the lane's exact semantic subset).

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established on current main by running both backends. For every non-Vec type, the C backend emits /* drop(p); */ as a comment for StmtKind.Drop and TK_DROP_AND_GOTO, so user Drop impls never run and nothing they own is released. The LLVM binary and the C binary of the same program produce different output, and --emit-c reports success with no diagnostic. The report's framing (silent semantic divergence with no documented limitation) holds; docs/requirements.md line 11193 describes --emit-c as "C source backend, used for bootstrapping new platforms", and docs/with-build.md line 328 as "emit C (cross-compilation)", neither of which mentions a destructor subset.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14); the two /* drop( sites are unchanged since 450733e
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    C compiler for the emitted source Apple cc, linked against the worktree's own out/lib/{rt_core,rt_darwin_aarch64,compat_runtime,panic_runtime,fiber_stubs,cimport_stubs}.o, the same object set build/emit_c.w:582-587 uses
    platform macOS Darwin 25.6.0, arm64

    Repro

    type Guard { name: str }
    impl Drop for Guard:
        move fn drop():
            print("drop " ++ self.name)
    fn make(n: str) -> Guard: Guard { name: n }
    fn main:
        let a = make("a")
        if true:
            let b = make("b")
            print("inner")
        var v: Vec[i32] = Vec.new()
        v.push(1)
        print("vec")
        print("end")
    
    backend output
    with run drop3.w (LLVM) inner, drop b, vec, end, drop a
    with build drop3.w --emit-c (rc 0, warnings only), then cc + runtime objects, run inner, vec, end

    In the emitted C: Guard_drop__13904 is declared and defined but has zero call sites; main's block bb6 contains /* drop(_3); */ where the LLVM path runs b's destructor; the Vec is handled ((with_vec){0} / with_vec_clear).

    Two notes for repeaters: --emit-c must be run from a tree with the generated std.re modules (a scratch cwd fails with "import module not found: std.re.defs"), and a Vec literal makes the backend fail loudly with "emit-c does not support generic intrinsic __collection_literal", which is the correct behaviour this issue asks for on the drop path.

    Source

    Spec / mission

    docs/mission.md line 9: an allocation "is released when the scope ends ... With calls [leaking] a defect". AGENTS.md §No Silent Fallbacks: "Silently dropping a clause, arm, or statement the translator can't lower" is forbidden, and emitting a comment in place of a statement is that exactly. No decision-log entry scopes the C backend to a drop-free subset.

    Related PRs

    None merged or open address drop emission in the C backend.

    Would I champion a fix? Yes.

    Either path is acceptable; silence is not. Acceptance criteria:

    1. Preferred: the C backend emits a call to the type's drop glue (the same Drop dispatch the LLVM backend uses, including field drops) for StmtKind.Drop and TK_DROP_AND_GOTO; the repro prints identical output on both backends, and the emit-c roundtrip lane (:emit-c-roundtrip) proves it on the compiler itself.
    2. Minimum: self.fail("emit-c does not support Drop for <type>") at both sites for any non-Vec type that needs a drop, so --emit-c exits non-zero on the repro, plus a sentence in docs/with-build.md naming the subset.
    3. A behaviour test that builds a small Drop program through both backends and compares stdout.

    Untested

    Linux and Windows not executed. Whether types with Drop reach the C backend in the compiler's own self-host lane (and therefore leak or skip cleanup there) was not measured; with the compiler's use of Mutex, JsonDocument, and file handles it almost certainly does.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced. A user Drop runs under LLVM and is a comment in the emitted C.

    $ cat main.w
    type T:
        n: i32
    impl Drop for T:
        move fn drop():
            print("dropped")
    fn main:
        let t = T { n: 1 }
        print(t.n)
    $ with run main.w
    1
    dropped
    $ with build main.w --emit-c -o main.c
    $ # main.c, main():
    bb1:
        /* drop(_1); */
        return;
    

    post-release: part of the emit-C lane campaign (#955); the C backend is not on a user's default path.

  3. added a commit that references this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions