Files
sx/examples/0039-basic-free-fn-ufcs-pointer-receiver.sx
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

27 lines
985 B
Plaintext

// Free-function UFCS with a pointer first-param (issue 0063). `recv.fn(args)`
// on a value `recv` whose matching free function takes `*T` now takes the
// receiver's address (the same implicit address-of as a struct-defined method),
// so mutations through the pointer are visible. Also: a function reached ONLY
// via UFCS is lazily lowered (previously declared-but-never-emitted → undefined
// symbol at link).
#import "modules/std.sx";
Counter :: struct { n: s32; }
// FREE functions (defined outside the struct), pointer first param.
bump :: (c: *Counter) -> s32 { c.n += 1; return c.n; }
// reached ONLY via UFCS — must still be emitted.
reset :: (c: *Counter) { c.n = 0; }
main :: () -> s32 {
c := Counter.{ n = 10 };
a := c.bump(); // 11, mutates c
b := c.bump(); // 12
print("a={} b={} n={}\n", a, b, c.n); // a=11 b=12 n=12
c.reset(); // UFCS-only free fn
print("after reset n={}\n", c.n); // after reset n=0
return 0;
}