Skip to content

A @[c_export] definition is silently dropped when an importer declares the same name extern fn; a bare @[c_export] exports nothing #2554

Closed

Description

@ehartford

A module's @[c_export("name")] pub fn name(..) is silently dropped when an importing unit declares extern fn name(..) for the same C symbol; a bare @[c_export] is silently no export at all.

Repro (installed compiler v0.15.3.0-g603b5574a), lib/x/h.w:

module x.h
use std.sync
var fails: AtomicI64 = atomic_new(0)
pub fn failures() -> i64: atomic_load(&fails)
@[c_export("assert_bool")]
pub unsafe fn assert_bool(exp: i32, real: i32, line: i32) -> Unit:
    if exp != real: fails.add(1)

src/main.w:

use x.h
use std.builtins.print_i64
extern fn assert_bool(exp: i32, real: i32, line: i32) -> Unit
fn run():
    unsafe { assert_bool(1, 2, 7) }
fn main:
    run()
    print_i64(failures())

with run src/main.w: ld64.lld: error: undefined symbol: _assert_bool. with analyze src/main.w 'select:name~assert_bool' lists only the extern's Sema signature: the module's declaration is gone from the pool, so no MIR body and no definition. Renaming the With function (pub unsafe fn ctest_assert_bool, same @[c_export("assert_bool")]) links and prints 1: the export's definition is only lost when a same-named declaration collides with it.

Expected: the extern declaration and the exported definition name one C symbol; the definition binds it (codegen already does that for a c_import prototype, #1850). Either the colliding declaration resolves to the export, or the collision is a diagnostic. Losing the body silently is the No Silent Fallbacks class.

Also: a bare @[c_export] (no name) compiles and exports nothing (fn_decl_has_c_export reads c_export:<name>); either it means the function's own name or it is refused.

Found wiring STC's ctest.h assertions to a With harness (test/corpora/stc_harness, Phase 3 of docs/proposals/stdlib_sourcing_plan.md); the harness uses distinct With names meanwhile.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced.

    $ cat lib/x/h.w
    module x.h
    use std.sync
    var fails: AtomicI64 = atomic_new(0)
    pub fn failures() -> i64: atomic_load(&fails)
    @[c_export("assert_bool")]
    pub unsafe fn assert_bool(exp: i32, real: i32, line: i32) -> Unit:
        if exp != real: fails.add(1)
    $ with run src/main.w   # declares extern fn assert_bool(...) and calls it
    ld64.lld: error: undefined symbol: _assert_bool
    >>> referenced by …/src/main.w
    

    pre-release: a definition silently disappears (the no-silent-fallbacks class); a collision diagnostic is small.

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