Skip to content

Global Atomic: initializer from another global's load() panics the compiler; top-level fetch_add + f-string fails codegen #1861

Closed

Description

@ehartford

A global initialized from an Atomic method on another global crashes the compiler, and a top-level Atomic method call next to an f-string read of the same global fails codegen. Both reproduce on main (v0.15.3.0-g355c20b16) and on a1f82be6.

Spec: §14.17.1 (Atomic), §9.1c (globals), §18.5 (implicit main).

Repro 1: the compiler panics

use std.sync

let a: Atomic[i64] = Atomic.new(4)
let v = a.load()

with build g4.w -o g4 prints panic: index out of bounds and writes no binary. The same thing happens with a fn main that prints v.

Repro 2: codegen reports the wrong argument type

use std.sync

let a: Atomic[i64] = Atomic.new(4)
a.fetch_add(3)
print(f"x{a.load()}")

The output is error: wrong argument type actual=i32 expected=ptr fn=main (x3), then code generation failed. print(f"{a.load()}"), with no literal text in the f-string, prints 7. Inside fn main the same statements work.

Expected: repro 1 compiles and v is 4. Repro 2 prints x7.

Workaround: none needed yet. I found this while building a widening matrix on fix-literals and have not investigated it.

Activity

  1. ehartford commented on Sep 28, 2026

    @ehartford
    ContributorAuthor

    Repro 2 does not need a global. It fails inside fn main too:

    use std.sync
    
    fn main:
        let a: Atomic[i64] = Atomic.new(4)
        a.store(3)
        print(f"x{a.load()}")
    

    wrong argument type actual=i32 expected=ptr fn=main (x2), code generation failed. Without the a.store(3) it prints x4. With print(f"{a.load()}") it prints 3. Same on a1f82be6.

  2. ehartford commented on Sep 28, 2026

    @ehartford
    ContributorAuthor

    The trigger is the omitted ordering argument. With explicit orderings the same program works:

    use std.sync
    
    fn main:
        let a: Atomic[i64] = Atomic.new(4)
        a.fetch_add(3, .SeqCst)
        print(f"x {a.load(.SeqCst)}")
    

    This prints x 7. When the orderings are omitted and the function also has an f-string with literal text, codegen fails with wrong argument type actual=i32 expected=ptr. The ordering is an i32, so it looks as though the default argument is spliced into another call's argument list. The same shape in a helper fn (fn f(u: u32) -> (i64, i64): let a: Atomic[i64] = Atomic.new(u); let a0 = a.load(); a.fetch_add(u); (a0, a.load())) makes the compiler panic with index out of bounds.

  3. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 792351b (stack #2007, battery green on 0f13a62). Atomic methods with the ordering omitted get SeqCst materialized as the call's operand; codegen no longer reads a neighbouring call's operand.

  4. added a commit that references this issue on Oct 13, 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

    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