| author | |
| committer | |
| log | ea21d2beb6cbdac8f5b6811a0f01aa6d2ee2ddf3 |
| tree | 3b277a3570b63f1eeb8fed2218d16bd578f12835 |
| parent | 32821e7098ba29c30e458f555f62954ce54dcb1a |
closes #612 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 | } |
| 2076 | 2088 | ||
| 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( |
| 1502 | var foo = u8; | 1502 | var 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( | ||
| 1506 | struct Foo {} | ||
| 1507 | struct Bar {} | ||
| 1508 | |||
| 1509 | fn 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 | } |
| 1505 | 1515 | ||
| 1506 | static void print_compiler_invocation(TestCase *test_case) { | 1516 | static void print_compiler_invocation(TestCase *test_case) { |