Commit 528c151a55

LemonBoy <thatlemon@gmail.com>
2019-05-27 17:39:56
Reject undefined as type
Make analyze_type_expr behave like ir_resolve_type when the user tries to use `undefined` as a type. Closes #2436
1 parent a169d84
Changed files (2)
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 {