Description
Calling type_of() with a struct type name (e.g., type_of(Foo)) silently returns "unknown" at runtime instead of producing a compile-time error. Per the standard, type_of() accepts a value, not a type name. Passing a type name should be rejected by the typechecker.
Reproduction
const Foo struct {
name string
}
do main() {
println(type_of(Foo))
}
Expected: Compile error — type_of() expects a value, not a type name
Actual: Compiles and prints unknown
Root Cause
The typechecker does not validate that the argument to type_of() is a value expression. When a bare struct type name is passed, it resolves as TK_UNKNOWN in the typetable. The codegen then falls through to the default branch in emit_builtin_call() (~line 2448 in codegen.c):
const char *tn = t ? type_name(t) : "unknown";
emitf(cg, "ez_string_lit(\"%s\")", tn);
This silently emits "unknown" instead of the compiler catching the mistake.
Fix
Add a typechecker validation in the type_of handling block (~line 2678 in typechecker.c). When the argument is a NODE_LABEL that resolves to a struct or enum type name (not a variable), emit a new error code:
E3xxx: type_of() expects a value, not a type name; use type_of(instance) instead
Register the new code in error_codes.h under the E3xxx (type errors) section and run ./scripts/generate_errors.sh.
Notes
This same class of bug (passing a type name where a value is expected) may affect other builtins like len(), copy(), etc. Those should be audited separately but are out of scope for this issue.
Description
Calling
type_of()with a struct type name (e.g.,type_of(Foo)) silently returns"unknown"at runtime instead of producing a compile-time error. Per the standard,type_of()accepts a value, not a type name. Passing a type name should be rejected by the typechecker.Reproduction
Expected: Compile error —
type_of()expects a value, not a type nameActual: Compiles and prints
unknownRoot Cause
The typechecker does not validate that the argument to
type_of()is a value expression. When a bare struct type name is passed, it resolves asTK_UNKNOWNin the typetable. The codegen then falls through to the default branch inemit_builtin_call()(~line 2448 incodegen.c):This silently emits
"unknown"instead of the compiler catching the mistake.Fix
Add a typechecker validation in the
type_ofhandling block (~line 2678 intypechecker.c). When the argument is aNODE_LABELthat resolves to a struct or enum type name (not a variable), emit a new error code:Register the new code in
error_codes.hunder the E3xxx (type errors) section and run./scripts/generate_errors.sh.Notes
This same class of bug (passing a type name where a value is expected) may affect other builtins like
len(),copy(), etc. Those should be audited separately but are out of scope for this issue.