Commit c40448eb9a

Andrew Kelley <andrew@ziglang.org>
2019-03-16 05:18:10
add compile error for wrong type with `use`
closes #1557
1 parent 4a5cd0b
Changed files (2)
src/analyze.cpp
@@ -4367,6 +4367,13 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
     ZigType *target_import = use_target_value->data.x_type;
     assert(target_import);
 
+    if (target_import->id != ZigTypeIdStruct) {
+        add_node_error(g, dst_use_node,
+            buf_sprintf("expected struct, found '%s'", buf_ptr(&target_import->name)));
+        get_container_scope(dst_use_node->owner)->any_imports_failed = true;
+        return;
+    }
+
     if (get_container_scope(target_import)->any_imports_failed) {
         get_container_scope(dst_use_node->owner)->any_imports_failed = true;
     }
test/compile_errors.zig
@@ -2,6 +2,13 @@ const tests = @import("tests.zig");
 const builtin = @import("builtin");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "usingnamespace with wrong type",
+        \\use void;
+    ,
+        "tmp.zig:1:1: error: expected struct, found 'void'",
+    );
+
     cases.add(
         "ignored expression in while continuation",
         \\export fn a() void {