authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-19 02:02:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-19 02:02:25-04:00
logcd2f65ff6ace9f1e426d5e8a4721666d347b289d
tree54231420872d0d852ad2f8bc2e2f3bbabd88ac7a
parent987768778a67538299f84a6ab7ff0ca65f69d2ac

add compile error for globally shadowing a primitive type

closes #423

3 files changed, 25 insertions(+), 9 deletions(-)

src/analyze.cpp+17-6
......@@ -2028,12 +2028,23 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
20282028 }
20292029 }
20302030
2031 auto entry = decls_scope->decl_table.put_unique(tld->name, tld);
2032 if (entry) {
2033 Tld *other_tld = entry->value;
2034 ErrorMsg *msg = add_node_error(g, tld->source_node, buf_sprintf("redefinition of '%s'", buf_ptr(tld->name)));
2035 add_error_note(g, msg, other_tld->source_node, buf_sprintf("previous definition is here"));
2036 return;
2031 {
2032 auto entry = decls_scope->decl_table.put_unique(tld->name, tld);
2033 if (entry) {
2034 Tld *other_tld = entry->value;
2035 ErrorMsg *msg = add_node_error(g, tld->source_node, buf_sprintf("redefinition of '%s'", buf_ptr(tld->name)));
2036 add_error_note(g, msg, other_tld->source_node, buf_sprintf("previous definition is here"));
2037 return;
2038 }
2039 }
2040
2041 {
2042 auto entry = g->primitive_type_table.maybe_get(tld->name);
2043 if (entry) {
2044 TypeTableEntry *type = entry->value;
2045 add_node_error(g, tld->source_node,
2046 buf_sprintf("declaration shadows type '%s'", buf_ptr(&type->name)));
2047 }
20372048 }
20382049}
20392050
test/cases/struct.zig-3
......@@ -202,7 +202,6 @@ test "packed struct" {
202202
203203
204204const u2 = @IntType(false, 2);
205const u3 = @IntType(false, 3);
206205
207206const BitField1 = packed struct {
208207 a: u3,
......@@ -374,8 +373,6 @@ test "runtime struct initialization of bitfield" {
374373 assert(s2.y == u4(x2));
375374}
376375
377const u4 = @IntType(false, 4);
378
379376var x1 = u4(1);
380377var x2 = u8(2);
381378
test/compile_errors.zig+8
......@@ -1987,4 +1987,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
19871987 \\}
19881988 ,
19891989 ".tmp_source.zig:2:17: error: expected type 'u3', found 'u8'");
1990
1991 cases.add("globally shadowing a primitive type",
1992 \\const u16 = @intType(false, 8);
1993 \\export fn entry() {
1994 \\ const a: u16 = 300;
1995 \\}
1996 ,
1997 ".tmp_source.zig:1:1: error: declaration shadows type 'u16'");
19901998}