lang: require explicit receiver in protocol method declarations

Protocol method declarations now declare their receiver explicitly as the first
parameter — 'self: *Self' (or 'self: Self') — matching the impl method signature,
instead of the old implicit-receiver form where the listed params were only the
extra args. That asymmetry repeatedly caused confusion over whether the first
param was the receiver or an argument.

The parser validates the first param is 'self' typed Self/*Self, then strips it,
so all downstream lowering and the dispatch ABI are unchanged (impl blocks and
call sites are unaffected). A protocol method missing the receiver is now a parse
error.

Migrated all 129 protocol method signatures across library + examples (+ one
inline-sx test in sema.zig) to the explicit form. Updated specs.md + readme.md.

New: examples/0418-protocols-explicit-receiver.sx (feature),
examples/1190-diagnostics-protocol-missing-receiver.sx (negative/diagnostic).
This commit is contained in:
agra
2026-06-21 11:02:16 +03:00
parent eb93c63c45
commit 6b0ebdd92b
66 changed files with 249 additions and 146 deletions

View File

@@ -1223,6 +1223,27 @@ pub const Parser = struct {
}
try self.expect(.r_paren);
// Every protocol method must declare its receiver EXPLICITLY as the
// first parameter — `self: *Self` (or `self: Self`) — matching how
// `impl` methods and ordinary methods are written. This removes the
// old implicit-receiver ambiguity (was the first listed param the
// receiver, or an extra arg?). The receiver is validated and then
// stripped here, so downstream lowering sees only the EXTRA-arg
// params, exactly as it did under the implicit form.
if (param_names.items.len == 0 or !std.mem.eql(u8, param_names.items[0], "self")) {
return self.fail("protocol method must declare its receiver as the first parameter: `self: *Self` (or `self: Self`)");
}
{
const rtype = param_types.items[0];
const is_self_val = rtype.data == .type_expr and std.mem.eql(u8, rtype.data.type_expr.name, "Self");
const is_self_ptr = rtype.data == .pointer_type_expr and
rtype.data.pointer_type_expr.pointee_type.data == .type_expr and
std.mem.eql(u8, rtype.data.pointer_type_expr.pointee_type.data.type_expr.name, "Self");
if (!is_self_val and !is_self_ptr) {
return self.fail("protocol method receiver must be typed `*Self` or `Self`");
}
}
// Optional return type
var return_type: ?*Node = null;
if (self.current.tag == .arrow) {
@@ -1238,12 +1259,19 @@ pub const Parser = struct {
if (self.current.tag == .semicolon) self.advance();
}
// Strip the receiver (index 0) — the method's stored params are the
// extra args only.
const all_param_types = try param_types.toOwnedSlice(self.allocator);
const all_param_names = try param_names.toOwnedSlice(self.allocator);
const all_param_name_spans = try param_name_spans.toOwnedSlice(self.allocator);
const all_param_name_is_raw = try param_name_is_raw.toOwnedSlice(self.allocator);
try methods.append(self.allocator, .{
.name = method_name,
.params = try param_types.toOwnedSlice(self.allocator),
.param_names = try param_names.toOwnedSlice(self.allocator),
.param_name_spans = try param_name_spans.toOwnedSlice(self.allocator),
.param_name_is_raw = try param_name_is_raw.toOwnedSlice(self.allocator),
.params = all_param_types[1..],
.param_names = all_param_names[1..],
.param_name_spans = all_param_name_spans[1..],
.param_name_is_raw = all_param_name_is_raw[1..],
.return_type = return_type,
.default_body = default_body,
});

View File

@@ -2205,7 +2205,7 @@ test "sema: method-return slice + .ptr index + tagged-enum element" {
const source =
"Event :: enum { none; click: i64; }" ++
"Plat :: protocol { poll :: () -> []Event; }" ++
"Plat :: protocol { poll :: (self: *Self) -> []Event; }" ++
"go :: (p: *Plat) { evs := p.poll(); e := evs.ptr[0]; }";
var parser = parser_mod.Parser.init(alloc, source);
const root = try parser.parse();