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

@@ -45,7 +45,7 @@ reserved spellings — `i1`..`i64`, `u1`..`u64`, `bool`, `string`, `cstring`, `v
member-name slots require an identifier token; a bare `f32` / `f64` is therefore
rejected at parse (`expected field name in struct`) even in a member position. Use
the backtick there too — `` struct { `f32: i64; } `` / `` union { `f64: … } `` /
`` protocol { `f32 :: () -> i64; } `` work as field / tag / method names.
`` protocol { `f32 :: (self: *Self) -> i64; } `` work as field / tag / method names.
```sx
i2 := 2.5; // ERROR: 'i2' is a reserved type name and cannot be used as an identifier
@@ -563,12 +563,12 @@ Protocols define a set of method signatures that types can implement. They enabl
#### Declaration
```sx
Allocator :: protocol #inline {
alloc :: (size: i64) -> *void;
dealloc :: (ptr: *void);
alloc :: (self: *Self, size: i64) -> *void;
dealloc :: (self: *Self, ptr: *void);
}
```
Protocol methods have an **implicit receiver** — no `self` in the protocol signature. The compiler adds `*Self` automatically. The `#inline` modifier embeds function pointers directly in the protocol value (no vtable indirection).
Protocol methods declare their receiver **explicitly** as the first parameter — `self: *Self` (or `self: Self`) — matching the corresponding `impl` method signature. This is **required**: a protocol method whose first parameter is not `self: *Self`/`self: Self` is a parse error. (It removes the old implicit-receiver ambiguity over whether the first listed parameter was the receiver or an extra argument.) The receiver annotation is validated then erased — the dispatch ABI is unchanged, so existing `impl` blocks and call sites are unaffected. The `#inline` modifier embeds function pointers directly in the protocol value (no vtable indirection).
#### `#inline` vs default layout
@@ -675,8 +675,8 @@ s : Sizable = xx @w; // identical to `xx w` — borrows w
Protocol methods can have bodies. `self` dispatches through the vtable (dynamic dispatch):
```sx
Writer :: protocol {
write :: (data: string) -> i64; // required
write_line :: (data: string) -> i64 { // default
write :: (self: *Self, data: string) -> i64; // required
write_line :: (self: *Self, data: string) -> i64 { // default
n := self.write(data);
n + self.write("\n");
}
@@ -688,7 +688,7 @@ Default methods are used unless overridden in the impl. Default methods calling
#### `Self` Type
`Self` is a contextual keyword in protocol declarations — resolves to the concrete type in impls:
```sx
Eq :: protocol { eq :: (other: Self) -> bool; }
Eq :: protocol { eq :: (self: *Self, other: Self) -> bool; }
impl Eq for Point {
eq :: (self: *Point, other: Point) -> bool {
@@ -755,7 +755,7 @@ in `modules/std.sx`:
```sx
Into :: protocol(Target: Type) {
convert :: () -> Target;
convert :: (self: *Self) -> Target;
}
```