zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit cd2f65ff6ace9f1e426d5e8a4721666d347b289d (tree)
parent 987768778a67538299f84a6ab7ff0ca65f69d2ac
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sat, 19 Aug 2017 02:02:25 -0400

add compile error for globally shadowing a primitive type

closes #423

Diffstat:
Msrc/analyze.cpp | 23+++++++++++++++++------
Mtest/cases/struct.zig | 3---
Mtest/compile_errors.zig | 8++++++++
3 files changed, 25 insertions(+), 9 deletions(-)

diff --git 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 @@ -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 @@ -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'"); }