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

@@ -184,7 +184,7 @@ function declaration, an `impl` method definition, or a `::` type declaration
(`i2 :: 5` and `i2 :: (n) { … }` are rejected just like `i2 := 5`). **Member-name
positions are exempt**: a struct *field*, a union *tag*, and a protocol
*method-signature* may be a bare reserved spelling (`struct { i2: i64 }`,
`union { u8: … }`, `protocol { i2 :: () -> i64 }`) — they are reached via `obj.name`,
`union { u8: … }`, `protocol { i2 :: (self: *Self) -> i64 }`) — they are reached via `obj.name`,
so they never mis-lower. The bare exemption covers only the identifier-classified
reserved names (`i1`..`i64`, `u1`..`u64`, `bool`, `string`, `void`, `usize`,
`isize`, `Any`); `f32` and `f64` are lexer keywords, so even in a member slot they
@@ -334,7 +334,7 @@ Closures capture by value. Bare functions auto-promote to closures when needed.
```sx
Drawable :: protocol {
draw :: (x: i32, y: i32);
draw :: (self: *Self, x: i32, y: i32); // receiver is explicit + required
}
impl Drawable for Circle {
@@ -345,11 +345,15 @@ shape : Drawable = xx my_circle; // type erasure via xx
shape.draw(10, 20); // dynamic dispatch
```
Every protocol method declares its receiver explicitly as the first parameter
(`self: *Self` or `self: Self`), matching the `impl` signature; the annotation is
erased before dispatch, so the call site is unchanged.
`#inline` protocols store function pointers directly (no vtable indirection):
```sx
Allocator :: protocol #inline {
alloc :: (size: i64) -> *void;
dealloc :: (ptr: *void);
alloc :: (self: *Self, size: i64) -> *void;
dealloc :: (self: *Self, ptr: *void);
}
```