Files
sx/issues/0063-free-fn-ufcs-pointer-param-passes-by-value.md
agra 34bdf8b87c issues: file 0062 (generic failable return not monomorphized) + 0063 (free-fn UFCS pointer param by-value)
Both discovered while verifying ERR E5.1 "verify-only" sub-features against
the built compiler. 0062 is sub-feature 8 (generic + ! returns); 0063 is a
general UFCS/address-of miscompile orthogonal to ERR.
2026-06-01 22:13:12 +03:00

2.2 KiB

0063 — free-function UFCS with a pointer first-param passes the struct by value

Symptom

Calling a free function via UFCS where the function's first parameter is a pointer (p: *Parser), on a local struct value, passes the struct BY VALUE where the function expects a pointer:

LLVM verification failed: Call parameter type does not match function signature!
  %load = load { i32, i32 }, ptr %alloca, align 4
  %call = call i32 @bump(ptr @__sx_default_context, { i32, i32 } %load)

The UFCS auto-address-of (p.bump()bump(@p)) does not kick in for free functions; the receiver is loaded by value instead of having its address taken. The same method defined inside the struct works fine — so this is specific to free-function UFCS, not method calls in general. Not failable-specific (the repro is a plain -> s32), so this is orthogonal to ERR.

Expected: p.bump() on a *Parser-first-param free function takes @p's address, matching the in-struct method behavior.

Reproduction

#import "modules/std.sx";
Parser :: struct { pos: s32; }
bump :: (p: *Parser) -> s32 { p.pos += 1; return p.pos; }   // FREE fn, pointer first param

main :: () -> s32 {
    p := Parser.{ pos = 0 };
    print("{}\n", p.bump());   // LLVM signature mismatch
    return 0;
}

Control (works): move bump inside Parser :: struct { … bump :: (p: *Parser) -> s32 { … } }. Also fails with an explicit bump(@p) — so the explicit address-of of a local struct into a pointer param is the underlying miscompile, not just the UFCS sugar.

Investigation prompt

Two related call paths in src/ir/lower.zig: (1) UFCS rewrite of obj.fn(args) for a free function whose first param is a pointer — it must auto-take-address of the receiver (as the in-struct method path does); (2) more fundamentally, lowering an explicit @local_struct argument into a *T parameter loads the struct by value instead of passing its slot pointer. Compare the in-struct method call lowering (which marshals the self/receiver correctly) against the free-function call + the address-of-local lowering. Verify with the repro (p.bump() and bump(@p) both compile + print 1, then 2 if called twice).