Files
sx/issues/0063-free-fn-ufcs-pointer-param-passes-by-value.md
agra 547148b8b6 fix(lower): free-fn UFCS auto-address-of + lazy lowering (issue 0063)
A free function called via UFCS (recv.fn(args)) whose first param is *T
was passed the receiver by value (LLVM "Call parameter type does not
match function signature"), and a function reached only via UFCS was
declared but never emitted (undefined symbol at link).

The bare-name UFCS fallback now mirrors the qualified-method path: it
lazily lowers the target body and calls fixupMethodReceiver +
coerceCallArgs, so the value receiver gets the same implicit address-of
as a struct-defined method and mutations through *T are visible.

Regression: 0039-basic-free-fn-ufcs-pointer-receiver.sx.
2026-06-01 22:28:15 +03:00

64 lines
2.9 KiB
Markdown

# 0063 — free-function UFCS with a pointer first-param passes the struct by value
> **✅ RESOLVED (2026-06-01).** The free-function UFCS fallback in
> [src/ir/lower.zig](../src/ir/lower.zig) ("Try to resolve as bare function
> name") built `method_args` with the value receiver but never called
> `fixupMethodReceiver`, and never lazily lowered the target — so the receiver
> was passed by value (LLVM signature mismatch) and a UFCS-only function was
> declared but never emitted (link error). Fix: that path now (1) lazily lowers
> `fa.field` if it's a known fn not yet lowered, and (2) calls
> `fixupMethodReceiver` + `coerceCallArgs` exactly like the qualified-method
> path. The explicit `bump(@p)` form was always fine. Regression:
> [examples/0039-basic-free-fn-ufcs-pointer-receiver.sx](../examples/0039-basic-free-fn-ufcs-pointer-receiver.sx).
## 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
```sx
#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](../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).