Commit 528c151a55
Changed files (2)
src
src/analyze.cpp
@@ -981,6 +981,17 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
return g->builtin_types.entry_invalid;
assert(result->special != ConstValSpecialRuntime);
+ // Reject undefined as valid `type` type even though the specification
+ // allows it to be casted to anything.
+ // See also ir_resolve_type()
+ if (result->special == ConstValSpecialUndef) {
+ add_node_error(g, node,
+ buf_sprintf("expected type 'type', found '%s'",
+ buf_ptr(&g->builtin_types.entry_undef->name)));
+ return g->builtin_types.entry_invalid;
+ }
+
+ assert(result->data.x_type != nullptr);
return result->data.x_type;
}
test/compile_errors.zig
@@ -2,6 +2,23 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.add(
+ "undefined as field type is rejected",
+ \\const Foo = struct {
+ \\ a: undefined,
+ \\};
+ \\const Bar = union {
+ \\ a: undefined,
+ \\};
+ \\pub fn main() void {
+ \\ const foo: Foo = undefined;
+ \\ const bar: Bar = undefined;
+ \\}
+ ,
+ "tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
+ "tmp.zig:5:8: error: expected type 'type', found '(undefined)'",
+ );
+
cases.add(
"@hasDecl with non-container",
\\export fn entry() void {