fix(reflection): replace silent .s64 arg-type fallback with loud .unresolved (issue 0075)

The `type_name` / `type_eq` reflection builtins resolved their Type arg's IR
type via `getRefIRType(...) orelse TypeId.s64`, then gated `== .any`. A failed
must-succeed lookup silently became `.s64` (`!= .any`), classifying a boxed
`Any` arg as bare i64 and reading the wrong value with no diagnostic.

Add the sibling classifier `LLVMEmitter.reflectArgRepr`, which routes the
lookup through `argIRTypeOrFail` (the issue-0074 `.unresolved` resolver) and
returns `{ boxed, bare, unresolved }`. The three emit sites in ops.zig
(`type_name` + `type_eq` x2) now switch on it: `.boxed` extracts the Any value
field, `.bare` uses the value directly, `.unresolved` hits a hard `@panic`
tripwire — never silently treated as bare. Real args always resolve, so the
happy path is byte-identical (suite stays 361/0, zero snapshot churn).

Secondary `lower.zig` `null_literal`/`undef_literal => target_type orelse .void`
confirmed intentional (typeless-literal default deliberately handled by
emitConstNull/emitConstUndef as null-ptr / undef-i64) — left with an invariant
comment, not the `.unresolved` tripwire.

Regression test in emit_llvm.test.zig asserts the loud path: fail-before with
`orelse .s64` yields `.bare`; pass-after yields `.unresolved`.
This commit is contained in:
agra
2026-06-03 16:05:31 +03:00
parent 759e3caa5e
commit aca077d720
5 changed files with 106 additions and 13 deletions

View File

@@ -1144,3 +1144,54 @@ test "emit: argIRTypeOrFail surfaces .unresolved for an unresolvable FFI arg ref
try std.testing.expectEqual(TypeId.unresolved, emitter.argIRTypeOrFail(bogus));
try std.testing.expect(emitter.argIRTypeOrFail(bogus) != .void);
}
// ── issue 0075: reflection-builtin arg-type lookup must fail loudly, never `.s64` ──
// `reflectArgRepr` backs the `type_name` / `type_eq` reflection builtins, which read
// their `Type` arg as a boxed `Any` aggregate (`.any` → extract value field) or a bare
// i64 TypeId index. A ref it cannot resolve is a codegen invariant violation; it must
// surface `.unresolved` (which the emit site hard-panics on) instead of the old silent
// `getRefIRType(arg) orelse .s64` default that would mis-classify a boxed arg as bare
// and read the wrong value with no diagnostic.
test "emit: reflectArgRepr surfaces .unresolved for an unresolvable reflection arg ref (issue 0075)" {
const alloc = std.testing.allocator;
var module = Module.init(alloc);
defer module.deinit();
var b = Builder.init(&module);
// func reflfn(boxed: any, bare: s64) -> void { <entry> }
const fid = b.beginFunction(str(&module, "reflfn"), &[_]Function.Param{
.{ .name = str(&module, "boxed"), .ty = .any },
.{ .name = str(&module, "bare"), .ty = .s64 },
}, .void);
const entry = b.appendBlock(str(&module, "entry"), &.{});
b.switchToBlock(entry);
b.retVoid();
b.finalize();
var emitter = LLVMEmitter.init(alloc, &module, "test_refl_argty", .{});
defer emitter.deinit();
emitter.current_func_idx = fid.index();
// Happy path: a boxed `.any` Type arg classifies as `.boxed` (extract value
// field); a bare `.s64` TypeId arg classifies as `.bare` (use directly).
// These decisions are byte-identical to the pre-fix `== .any` gate.
try std.testing.expectEqual(LLVMEmitter.ReflectArgRepr.boxed, emitter.reflectArgRepr(Ref.fromIndex(0)));
try std.testing.expectEqual(LLVMEmitter.ReflectArgRepr.bare, emitter.reflectArgRepr(Ref.fromIndex(1)));
// A ref past every param and instruction is unresolvable.
const bogus = Ref.fromIndex(100_000);
try std.testing.expectEqual(@as(?TypeId, null), emitter.getRefIRType(bogus));
// Fail-before: the old `getRefIRType(arg) orelse .s64` would silently yield
// `.s64` here — which `!= .any`, so the reflection arm would treat a failed
// lookup as a bare i64 and read the wrong value with no diagnostic.
try std.testing.expectEqual(TypeId.s64, emitter.getRefIRType(bogus) orelse TypeId.s64);
try std.testing.expect((emitter.getRefIRType(bogus) orelse TypeId.s64) != .any);
// Pass-after: the classifier returns the dedicated `.unresolved` variant,
// never `.bare`, so the emit site trips its hard panic instead of silently
// reading the wrong value.
try std.testing.expectEqual(LLVMEmitter.ReflectArgRepr.unresolved, emitter.reflectArgRepr(bogus));
try std.testing.expect(emitter.reflectArgRepr(bogus) != .bare);
}