Commit cd2f65ff6a

Andrew Kelley <superjoe30@gmail.com>
2017-08-19 08:02:25
add compile error for globally shadowing a primitive type
closes #423
1 parent 9877687
Changed files (3)
src/analyze.cpp
@@ -2028,12 +2028,23 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
         }
     }
 
-    auto entry = decls_scope->decl_table.put_unique(tld->name, tld);
-    if (entry) {
-        Tld *other_tld = entry->value;
-        ErrorMsg *msg = add_node_error(g, tld->source_node, buf_sprintf("redefinition of '%s'", buf_ptr(tld->name)));
-        add_error_note(g, msg, other_tld->source_node, buf_sprintf("previous definition is here"));
-        return;
+    {
+        auto entry = decls_scope->decl_table.put_unique(tld->name, tld);
+        if (entry) {
+            Tld *other_tld = entry->value;
+            ErrorMsg *msg = add_node_error(g, tld->source_node, buf_sprintf("redefinition of '%s'", buf_ptr(tld->name)));
+            add_error_note(g, msg, other_tld->source_node, buf_sprintf("previous definition is here"));
+            return;
+        }
+    }
+
+    {
+        auto entry = g->primitive_type_table.maybe_get(tld->name);
+        if (entry) {
+            TypeTableEntry *type = entry->value;
+            add_node_error(g, tld->source_node,
+                    buf_sprintf("declaration shadows type '%s'", buf_ptr(&type->name)));
+        }
     }
 }
 
test/cases/struct.zig
@@ -202,7 +202,6 @@ test "packed struct" {
 
 
 const u2 = @IntType(false, 2);
-const u3 = @IntType(false, 3);
 
 const BitField1 = packed struct {
     a: u3,
@@ -374,8 +373,6 @@ test "runtime struct initialization of bitfield" {
     assert(s2.y == u4(x2));
 }
 
-const u4 = @IntType(false, 4);
-
 var x1 = u4(1);
 var x2 = u8(2);
 
test/compile_errors.zig
@@ -1987,4 +1987,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
         \\}
     ,
         ".tmp_source.zig:2:17: error: expected type 'u3', found 'u8'");
+
+    cases.add("globally shadowing a primitive type",
+        \\const u16 = @intType(false, 8);
+        \\export fn entry() {
+        \\    const a: u16 = 300;
+        \\}
+    ,
+        ".tmp_source.zig:1:1: error: declaration shadows type 'u16'");
 }