Skip to content

Option.map(it as i64) in a compiler method links to an undefined bare ___closure in the multi-unit build #2558

Closed

Description

@ehartford

Building the compiler (src/main build :dev, seed nightly-20261020-local-3) with this expression in Codegen.emit_module_runtime_init_helpers (src/CodegenTraits.w):

let global_val: i64 = self.module_constants.get(name_sym).map(it as i64) ?? 0

fails at link:

error: ld64.lld: error: undefined symbol: ___closure
>>> referenced by /with-src/out/gen/main.w
>>>               out/bootstrap/bin/with-stage1.tmp.o.u14.o:(symbol ___wcu$6287$Codegen.emit_module_runtime_init_helpers+0xb0)

The closure body it as i64 was emitted as a call to a bare __closure symbol with no definition in the unit object. The same shape in a user program links and runs with both the installed compiler and the seed:

use std.collections
fn main:
    var m: HashMap[i32, i32] = HashMap.new()
    m.insert(1, 41)
    let v: i64 = m.get(1).map(it as i64) ?? 0
    print(v + 1)   # 42

So the miss is specific to the compiler's multi-unit build (__wcu$ unit objects): the closure's definition is not emitted into (or not named consistently across) the unit that references it. Workaround used: a plain if stored.is_some(): stored.unwrap() as i64 else: 0.

Route: --dump-mir of the method under the seed, then nm of the unit objects for the __closure definition.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): could not be reproduced without editing the compiler. The shape works in a user program (a method returning self.m.get(k).map(it as i64) ?? 0 prints 42), and the compiler source on main no longer contains .map(it as i64). Reproducing needs that line put back into src/CodegenTraits.w and a :dev stage1 build, which this triage does not do.

    The question: keep this open for a multi-unit closure-emission test (a fixture that builds with several codegen units), or close it as unreproducible?

  2. ehartford commented on Oct 29, 2026

    @ehartford
    ContributorAuthor

    Reproduced the multi-unit shape without editing the compiler, on main 279981a (installed compiler, darwin-arm64): WITH_CODEGEN_UNITS reaches a user program's build (Backend.w compile_to_object_backend), so a method holding the issue's closure can be split across units.

    use std.collections
    type Holder:
        m: HashMap[i32, i32]
    impl Holder:
        fn wide(k: i32) -> i64: self.m.get(k).map(it as i64) ?? 0
    fn a1(h: &Holder) -> i64: h.wide(1) + 1
    fn a2(h: &Holder) -> i64: h.wide(2) + 2
    fn a3(h: &Holder) -> i64: h.wide(1) * 3
    fn main:
        var m: HashMap[i32, i32] = HashMap.new()
        m.insert(1, 41)
        let h = Holder { m: m }
        print(f"{a1(&h)} {a2(&h)} {a3(&h)}")
    

    With WITH_CODEGEN_UNITS set to 1, 2, 4 and 8, it prints 42 2 123 every time. The 8-unit binary carries ___wcu$267$Holder.wide, ___wcu$269$a1 and so on, so the method and its callers are in separate units, and there is no undefined ___closure. The failure came from a stage1 built by seed nightly-20261020-local-3, and it does not reproduce on the current toolchain. To recheck: build the program above with WITH_CODEGEN_UNITS=8 with build, then run nm on the binary.

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