From cd2f65ff6ace9f1e426d5e8a4721666d347b289d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 19 Aug 2017 02:02:25 -0400 Subject: [PATCH] add compile error for globally shadowing a primitive type closes #423 --- src/analyze.cpp | 23 +++++++++++++++++------ test/cases/struct.zig | 3 --- test/compile_errors.zig | 8 ++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index b7616151c5..a59c77a1c5 100644 --- a/src/analyze.cpp +++ b/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))); + } } } diff --git a/test/cases/struct.zig b/test/cases/struct.zig index ea444555cd..792ac9fd98 100644 --- a/test/cases/struct.zig +++ b/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); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 16c36dc294..e2f4489b0f 100644 --- a/test/compile_errors.zig +++ b/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'"); }