Skip to content

bug: type_of() on struct type name returns "unknown" instead of compile error #1586

@SchoolyB

Description

@SchoolyB

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingerror-messagesRelated to improving error messagestypecheckerRelated to type checking and validation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions