Files
sx/examples/diagnostics/1197-diagnostics-protocol-erasure-no-impl.sx
agra 3c738695dc 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).
2026-06-23 02:13:30 +03:00

21 lines
914 B
Plaintext

// 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());
}