Files
sx/examples/memory/0808-memory-xx-value-routes-through-context-allocator.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

47 lines
1.6 KiB
Plaintext

// Phase 1.1 — the compiler-internal heap-copy that backs `xx <rvalue>`
// protocol erasure must dispatch through `context.allocator`, not call
// libc malloc directly. So when a `push Context.{ allocator = tracer }`
// block is active, a `xx StructLiteral.{}` inside it MUST be allocated
// by the tracker.
//
// Note: `xx` only heap-copies for RVALUES (struct literals, call results).
// `xx <lvalue>` (an identifier, field access, index, or deref) borrows
// the operand's storage, so it never allocates and never reaches this
// path. See specs.md §3 — Protocol value ownership and lifetime.
#import "modules/std.sx";
#import "modules/std/mem.sx"; // `Allocator` is non-transitive: name it, import it.
Tracer :: struct {
count: i64;
init :: () -> *Tracer {
t : *Tracer = xx libc_malloc(size_of(Tracer));
t.count = 0;
t
}
}
impl Allocator for Tracer {
alloc_bytes :: (self: *Tracer, size: i64) -> *void {
self.count += 1;
return libc_malloc(size);
}
dealloc_bytes :: (self: *Tracer, ptr: *void) {
libc_free(ptr);
}
}
main :: () -> i32 {
tracer := Tracer.init();
push Context.{ allocator = xx tracer, data = null } {
// Struct-literal operand: rvalue → heap-copy through context.allocator.
// 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);
0
}