fix: diagnose non-conforming protocol erasure instead of unreachable-thunk SIGABRT (issue 0176)
Erasing a type to a protocol when it conforms only via a free function (not an explicit impl P for T) built a vtable of unreachable thunks -> SIGABRT on first dispatch, with no diagnostic. Per specs.md erasure is impl-driven, not structural, so the erasure was never valid. Add a conformance gate (firstUnimplementedMethod in buildProtocolValue, src/ir/lower/protocol.zig): emit a located diagnostic when a protocol method has no reachable impl, or when an impl method introduces its own type params (signature mismatch — it bails lazyLowerFunction and would reach the unreachable thunk). A std.debug.panic tripwire guards the diagnostics==null path so a non-conforming erasure can never silently ship as undef. Gate<->thunk equivalence verified bidirectional. Regressions: protocols/0419 (positive struct-field dispatch), diagnostics/1197 (no-impl) + 1198 (generic-method signature mismatch). Updated memory/0808 (it erased a non-conforming type that never dispatched). Verified by 3+1 adversarial reviews, suite 788/0. Filed adjacent bug 0178 (protocol impl method type-mismatch silent miscompile).
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
// Erasing a concrete type to a protocol it does NOT `impl`-ement is a hard
|
||||
// error, not a silent SIGABRT. Regression (issue 0176): a plain free function
|
||||
// `speak :: (self: *Dog)` with a matching receiver does NOT satisfy the
|
||||
// protocol — protocol erasure is impl-driven, not structural (specs.md
|
||||
// §"Storage and protocol conformance"). Before the fix, erasure built a vtable
|
||||
// of unreachable thunks and `h.s.speak()` aborted with exit 133 and no output;
|
||||
// now it reports a clear diagnostic at the erasure site and exits 1.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
Speaker :: protocol { speak :: (self: *Self) -> i64; }
|
||||
Dog :: struct { n: i64 = 0; }
|
||||
speak :: (self: *Dog) -> i64 { return self.n; } // free fn — NOT an impl
|
||||
Holder :: struct { s: Speaker; b: i64 = 0; }
|
||||
|
||||
main :: () {
|
||||
d := Dog.{ n = 42 };
|
||||
h : Holder = .{ s = d, b = 5 }; // <- 'Dog' does not implement 'Speaker'
|
||||
print("{}\n", h.s.speak());
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Erasing a concrete type to a protocol whose `impl` method introduces its OWN
|
||||
// type parameter is a hard error, not a silent SIGABRT. Regression (issue 0176,
|
||||
// adversarial-review gap 1): `impl Speaker for Dog { speak :: (self: *Dog, $T:
|
||||
// Type) }` has a matching qualified name `Dog.speak`, so the conformance gate
|
||||
// used to ACCEPT it — but the thunk's `lazyLowerFunction("Dog.speak")` bails on
|
||||
// `type_params.len > 0` (generics are monomorphized, not registered), so
|
||||
// `resolveFuncByName` returns null and the thunk hit its `else => unreachable`
|
||||
// arm: a SILENT SIGABRT (exit 133, no output) at the first dispatch. The gate
|
||||
// now mirrors the thunk exactly — a method that introduces its own type params
|
||||
// is a SIGNATURE MISMATCH, reported here at the erasure site (exit 1).
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
Speaker :: protocol { speak :: (self: *Self) -> i64; }
|
||||
Dog :: struct { n: i64 = 0; }
|
||||
impl Speaker for Dog { speak :: (self: *Dog, $T: Type) -> i64 { return self.n; } }
|
||||
|
||||
main :: () {
|
||||
d := Dog.{ n = 42 };
|
||||
s : Speaker = d; // <- 'Dog.speak' has a mismatched signature
|
||||
print("{}\n", s.speak());
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
error: 'Dog' does not implement protocol 'Speaker': no `impl Speaker for Dog` provides method 'speak' (protocol erasure is impl-driven — a plain or `ufcs` free function with a matching receiver does not satisfy a protocol)
|
||||
--> examples/diagnostics/1197-diagnostics-protocol-erasure-no-impl.sx:18:16
|
||||
|
|
||||
18 | h : Holder = .{ s = d, b = 5 }; // <- 'Dog' does not implement 'Speaker'
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
error: 'Dog' does not implement protocol 'Speaker': method 'speak' has a mismatched signature — a protocol-method impl must not introduce its own type parameters (e.g. `$T: Type`); it must match the protocol's signature exactly
|
||||
--> examples/diagnostics/1198-diagnostics-protocol-erasure-generic-method.sx:20:3
|
||||
|
|
||||
20 | s : Speaker = d; // <- 'Dog.speak' has a mismatched signature
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -31,13 +31,14 @@ impl Allocator for Tracer {
|
||||
}
|
||||
}
|
||||
|
||||
ByValue :: struct { x: i64; y: i64; }
|
||||
|
||||
main :: () -> i32 {
|
||||
tracer := Tracer.init();
|
||||
push Context.{ allocator = xx tracer, data = null } {
|
||||
// Struct-literal operand: rvalue → heap-copy through context.allocator.
|
||||
ignore : Allocator = xx ByValue.{ x = 1, y = 2 };
|
||||
// The erased type must actually `impl Allocator` (erasure is
|
||||
// impl-driven, issue 0176), so use a fresh `Tracer.{}` rvalue — the
|
||||
// copy is what we want routed through the active allocator.
|
||||
ignore : Allocator = xx Tracer.{ count = 0 };
|
||||
_ = ignore;
|
||||
}
|
||||
print("Tracer.count = {}\n", tracer.count);
|
||||
|
||||
49
examples/protocols/0419-protocols-struct-field-dispatch.sx
Normal file
49
examples/protocols/0419-protocols-struct-field-dispatch.sx
Normal file
@@ -0,0 +1,49 @@
|
||||
// Dispatch a protocol method THROUGH a protocol-typed struct field.
|
||||
// Regression (issue 0176): the issue's repro used a plain free function to
|
||||
// "satisfy" the protocol — which is NOT impl-driven conformance, so erasure
|
||||
// built a vtable of unreachable thunks and the first dispatch SIGABRT'd with no
|
||||
// diagnostic. With a real `impl` block the field dispatch must work; the
|
||||
// non-conforming case is now a loud diagnostic (see the negative example).
|
||||
//
|
||||
// Covers: struct-literal init, field-assign init, reassigning a protocol field
|
||||
// to a DIFFERENT concrete impl, a protocol method with args + non-void return,
|
||||
// a protocol field beside a non-protocol field (offsets), a nested struct
|
||||
// holding a struct holding a protocol field, and passing the holder by value
|
||||
// into another function before dispatching.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
Speaker :: protocol { greet :: (self: *Self, x: i64) -> i64; }
|
||||
|
||||
Dog :: struct { n: i64 = 0; }
|
||||
impl Speaker for Dog { greet :: (self: *Dog, x: i64) -> i64 { return self.n + x; } }
|
||||
|
||||
Cat :: struct { m: i64 = 0; }
|
||||
impl Speaker for Cat { greet :: (self: *Cat, x: i64) -> i64 { return self.m * x; } }
|
||||
|
||||
Holder :: struct { s: Speaker; b: i64 = 0; t: Speaker; }
|
||||
Outer :: struct { h: Holder; }
|
||||
|
||||
run :: (h: Holder) -> i64 { return h.s.greet(10) + h.t.greet(2); }
|
||||
|
||||
main :: () {
|
||||
d := Dog.{ n = 42 };
|
||||
c := Cat.{ m = 5 };
|
||||
|
||||
// struct-literal init: two protocol fields straddling a non-protocol one.
|
||||
h : Holder = .{ s = d, b = 7, t = c };
|
||||
print("lit: {} {} {}\n", h.s.greet(10), h.b, h.t.greet(3)); // 52 7 15
|
||||
|
||||
// field-assign init, then reassign each field to a DIFFERENT concrete impl.
|
||||
h2 : Holder = .{ s = d, b = 0, t = c };
|
||||
h2.s = c;
|
||||
h2.t = d;
|
||||
print("reassign: {} {}\n", h2.s.greet(4), h2.t.greet(8)); // 20 50
|
||||
|
||||
// nested struct holding a struct holding a protocol field.
|
||||
o : Outer = .{ h = .{ s = d, b = 1, t = c } };
|
||||
print("nested: {} {}\n", o.h.s.greet(0), o.h.t.greet(2)); // 42 10
|
||||
|
||||
// pass the holder by value into a function, dispatch there.
|
||||
print("passed: {}\n", run(h)); // 52 + 10 = 62
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
lit: 52 7 15
|
||||
reassign: 20 50
|
||||
nested: 42 10
|
||||
passed: 62
|
||||
Reference in New Issue
Block a user