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

@@ -203,12 +203,12 @@ c_abs :: (n: i32) -> i32 extern libc "abs";
// --- Protocol declarations (Phase 1: static dispatch only) ---
Counter :: protocol {
inc :: ();
get :: () -> i32;
inc :: (self: *Self);
get :: (self: *Self) -> i32;
}
Summable :: protocol {
sum :: () -> i32;
sum :: (self: *Self) -> i32;
}
SimpleCounter :: struct { val: i32; }
@@ -226,8 +226,8 @@ impl Summable for Point {
// Phase 2: #inline protocol for dynamic dispatch
Adder :: protocol #inline {
add :: (n: i32);
value :: () -> i32;
add :: (self: *Self, n: i32);
value :: (self: *Self) -> i32;
}
Accumulator :: struct {
@@ -250,8 +250,8 @@ impl Adder for Doubler {
// Phase 4: default methods
Repeater :: protocol {
say :: (msg: string);
say_twice :: (msg: string) {
say :: (self: *Self, msg: string);
say_twice :: (self: *Self, msg: string) {
self.say(msg);
self.say(msg);
}
@@ -270,11 +270,11 @@ impl Repeater for Printer {
// P4 edge: Chained default→default calls
Chained :: protocol {
base :: (msg: string) -> i32;
wrap :: (msg: string) -> i32 {
base :: (self: *Self, msg: string) -> i32;
wrap :: (self: *Self, msg: string) -> i32 {
self.base(msg) + 1
}
double_wrap :: (msg: string) -> i32 {
double_wrap :: (self: *Self, msg: string) -> i32 {
self.wrap(msg) + self.wrap(msg)
}
}
@@ -291,7 +291,7 @@ impl Chained for ChainImpl {
// Phase 5: Self type
Eq :: protocol {
eq :: (other: Self) -> bool;
eq :: (self: *Self, other: Self) -> bool;
}
impl Eq for Point {
@@ -301,7 +301,7 @@ impl Eq for Point {
}
Cloneable :: protocol {
clone :: () -> Self;
clone :: (self: *Self) -> Self;
}
impl Cloneable for Point {
@@ -324,7 +324,7 @@ are_equal :: ($T: Type/Eq, a: T, b: T) -> bool {
}
Hashable :: protocol {
hash :: () -> i64;
hash :: (self: *Self) -> i64;
}
impl Hashable for Point {