refactor(ir): distinguish free-function UFCS from namespace calls in CallPlan (A3.2 review fix)
CallPlan collapsed two different field-access dispatches onto namespace_fn: a true namespace call (`pkg.fn()`, no receiver) and free-function UFCS (`c.bump()`, receiver prepended + `*T` fixup). Return typing was preserved either way, but sub-step 3 could not consume the plan — it would have had to re-classify the AST to decide whether to prepend the receiver. Add a distinct `free_fn_ufcs` kind and a plan(c) branch, inserted after the struct-method block and gated on `objectIsValue` (the negation of lowerCall's `is_namespace`: a non-identifier receiver is always a value; an identifier/type_expr is a value iff it names a local or a global). The branch sets prepends_receiver = true and reads prepends_ctx from the resolved FuncId (best-effort, like direct_fn). namespace_fn now means strictly "receiver is a namespace/type prefix". New test `plan: free-function UFCS prepends receiver, distinct from namespace_fn` covers a scope-bound `c.bump()` against a lowered free fn: asserts free_fn_ufcs kind, func target, prepends_receiver, prepends_ctx, and preserved s32 return type. zig build, zig build test, tests/run_examples.sh (357/0) all green; return typing unchanged.
This commit is contained in:
@@ -420,6 +420,43 @@ test "plan: enum construction (qualified + dot-shorthand) carries variant tag" {
|
||||
}
|
||||
}
|
||||
|
||||
test "plan: free-function UFCS prepends receiver, distinct from namespace_fn" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// struct Counter, and a FREE function `bump :: (c: Counter) -> s32` — NOT
|
||||
// registered as `Counter.bump`, so it can only be reached via UFCS.
|
||||
const counter = module.types.intern(.{ .@"struct" = .{ .name = module.types.internString("Counter"), .fields = &.{} } });
|
||||
const c_param = ast.Param{ .name = "c", .name_span = .{ .start = 0, .end = 0 }, .type_expr = typeExpr(alloc, "Counter") };
|
||||
const params = [_]ast.Param{c_param};
|
||||
const ret_stmt = mk(alloc, .{ .return_stmt = .{ .value = intLit(alloc, 7) } });
|
||||
const body = mk(alloc, .{ .block = .{ .stmts = &[_]*Node{ret_stmt} } });
|
||||
const fd = ast.FnDecl{ .name = "bump", .params = ¶ms, .return_type = typeExpr(alloc, "s32"), .body = body };
|
||||
l.lowerFunction(&fd, "bump", false);
|
||||
const fid = l.resolveFuncByName("bump").?;
|
||||
module.functions.items[@intFromEnum(fid)].has_implicit_ctx = true;
|
||||
|
||||
// A value receiver in scope: `c : Counter`. `c.bump()` is UFCS, not a
|
||||
// namespace call — the receiver must be prepended.
|
||||
var scope = Scope.init(alloc, null);
|
||||
defer scope.deinit();
|
||||
scope.put("c", .{ .ref = Ref.none, .ty = counter, .is_alloca = false });
|
||||
l.scope = &scope;
|
||||
|
||||
const call = callNode(alloc, fieldAccess(alloc, ident(alloc, "c"), "bump"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.free_fn_ufcs, p.kind);
|
||||
try std.testing.expectEqual(fid, p.target.func);
|
||||
try std.testing.expect(p.prepends_receiver);
|
||||
try std.testing.expect(p.prepends_ctx);
|
||||
try std.testing.expectEqual(TypeId.s32, p.return_type);
|
||||
}
|
||||
|
||||
test "plan: qualified namespace function" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
Reference in New Issue
Block a user