fix(ir): reject non-type args to the 7 type-introspection builtins [F0.8]

size_of, align_of, field_count, type_name, type_eq, type_is_unsigned,
and is_flags silently reinterpreted a value argument as a type:
type_is_unsigned(6) read 6 as a TypeId index (types[6] = u8 -> true),
size_of(6)/size_of(true) sized its typeof (8), type_name(6) returned
types[6]'s name. Per Agra's ruling, all 7 now strictly require a type
(compile-time): a value argument is a compile error.

One shared guard (Lowering.reflectionTypeArgGuard, run at the top of
tryLowerReflectionCall) classifies each arg via reflectionArgIsType: a
spelled / compile-time type or generic type parameter (isStaticTypeArg),
or a runtime Type value (static type .any -- type_of(x), a []Type
element list[i], a Type-typed local/field/param) is accepted; anything
else is rejected with "<builtin> expects a type, got '<type>'". The
runtime path for type_name / type_is_unsigned is preserved (the {}
formatter calls type_is_unsigned(type_of(val)) at runtime). The 5
comptime-only builtins stay comptime-only (runtime reflection deferred).

Regression: examples/1144-diagnostics-reflection-builtin-needs-type.sx
(reject cases across all 7, exit 1). Unit test: reflectionArgIsType in
lower.test.zig. specs.md / readme.md document the strict type
requirement (and add the previously-undocumented align_of, type_eq,
type_is_unsigned). issues/0090 RESOLVED banner updated.
This commit is contained in:
agra
2026-06-05 11:22:59 +03:00
parent 64f77e9779
commit b053c64149
9 changed files with 210 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
// The 7 type-introspection builtins (size_of, align_of, field_count,
// type_name, type_eq, type_is_unsigned, is_flags) take ONLY types. A
// value argument is a compile-time error, not a silently-reinterpreted
// TypeId index.
//
// Regression (issue 0090, attempt 2): each builtin used to accept a
// non-type value and reinterpret it — `type_is_unsigned(6)` read 6 as a
// TypeId and returned the signedness of types[6] (`u8` → true);
// `size_of(true)` sized `typeof(true)` (8). The strict `$T: Type` guard
// (`Lowering.reflectionTypeArgGuard`) now rejects any argument whose
// static type is not `Type` and emits "<builtin> expects a type, got
// '<type>'" at the offending argument, aborting (exit 1).
//
// Reject cases use `true`/`1.5` (whose types — bool/f64 — are stable)
// rather than an integer literal, so the pinned diagnostics don't drift
// when the int-literal default type changes.
#import "modules/std.sx";
main :: () {
print("{}\n", size_of(true)); // bool, not a type
print("{}\n", align_of(1.5)); // f64, not a type
print("{}\n", field_count(true)); // bool, not a type
print("{}\n", type_name(1.5)); // f64, not a type
print("{}\n", type_eq(true, false)); // both bool — both rejected
print("{}\n", type_is_unsigned(true)); // bool, not a type
print("{}\n", is_flags(1.5)); // f64, not a type
}

View File

@@ -0,0 +1,47 @@
error: size_of expects a type, got 'bool'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:21:27
|
21 | print("{}\n", size_of(true)); // bool, not a type
| ^^^^
error: align_of expects a type, got 'f64'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:22:28
|
22 | print("{}\n", align_of(1.5)); // f64, not a type
| ^^^
error: field_count expects a type, got 'bool'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:23:31
|
23 | print("{}\n", field_count(true)); // bool, not a type
| ^^^^
error: type_name expects a type, got 'f64'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:24:29
|
24 | print("{}\n", type_name(1.5)); // f64, not a type
| ^^^
error: type_eq expects a type, got 'bool'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:25:27
|
25 | print("{}\n", type_eq(true, false)); // both bool — both rejected
| ^^^^
error: type_eq expects a type, got 'bool'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:25:33
|
25 | print("{}\n", type_eq(true, false)); // both bool — both rejected
| ^^^^^
error: type_is_unsigned expects a type, got 'bool'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:26:36
|
26 | print("{}\n", type_is_unsigned(true)); // bool, not a type
| ^^^^
error: is_flags expects a type, got 'f64'
--> examples/1144-diagnostics-reflection-builtin-needs-type.sx:27:28
|
27 | print("{}\n", is_flags(1.5)); // f64, not a type
| ^^^