authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 17:04:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 17:04:37-07:00
logea21d2beb6cbdac8f5b6811a0f01aa6d2ee2ddf3
tree3b277a3570b63f1eeb8fed2218d16bd578f12835
parent32821e7098ba29c30e458f555f62954ce54dcb1a

add error for shadowing a type

closes #61

2 files changed, 22 insertions(+), 0 deletions(-)

src/analyze.cpp+12
...@@ -2072,6 +2072,18 @@ static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, Block...@@ -2072,6 +2072,18 @@ static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, Block
2072 if (existing_var) {2072 if (existing_var) {
2073 add_node_error(g, source_node, buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));2073 add_node_error(g, source_node, buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
2074 variable_entry->type = g->builtin_types.entry_invalid;2074 variable_entry->type = g->builtin_types.entry_invalid;
2075 } else {
2076 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
2077 TypeTableEntry *type;
2078 if (primitive_table_entry) {
2079 type = primitive_table_entry->value;
2080 } else {
2081 type = find_container(context, name);
2082 }
2083 if (type) {
2084 add_node_error(g, source_node, buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
2085 variable_entry->type = g->builtin_types.entry_invalid;
2086 }
2075 }2087 }
20762088
2077 context->variable_table.put(&variable_entry->name, variable_entry);2089 context->variable_table.put(&variable_entry->name, variable_entry);
test/run_tests.cpp+10
...@@ -1501,6 +1501,16 @@ const foo = []u16{.x = 1024,};...@@ -1501,6 +1501,16 @@ const foo = []u16{.x = 1024,};
1501 add_compile_fail_case("type variables must be constant", R"SOURCE(1501 add_compile_fail_case("type variables must be constant", R"SOURCE(
1502var foo = u8;1502var foo = u8;
1503 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");1503 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");
1504
1505 add_compile_fail_case("variables shadowing types", R"SOURCE(
1506struct Foo {}
1507struct Bar {}
1508
1509fn f(Foo: i32) => {
1510 var Bar : i32;
1511}
1512 )SOURCE", 2, ".tmp_source.zig:5:6: error: variable shadows type 'Foo'",
1513 ".tmp_source.zig:6:5: error: variable shadows type 'Bar'");
1504}1514}
15051515
1506static void print_compiler_invocation(TestCase *test_case) {1516static void print_compiler_invocation(TestCase *test_case) {