diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-05-03 16:13:22 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-05-03 16:13:57 -0400 |
| commit | 0940d46c0160683fb5a28f66589e53ec5b64241d (patch) | |
| tree | 1b762e3c124b68c00db161efe6f076f61730f4b6 /src/analyze.cpp | |
| parent | 6756c27ca48f65c7f21b7a4365ff7a80069d2441 (diff) | |
| download | zig-0940d46c0160683fb5a28f66589e53ec5b64241d.tar.gz zig-0940d46c0160683fb5a28f66589e53ec5b64241d.zip | |
add compile error for shadowing variable
closes #360
Diffstat (limited to 'src/analyze.cpp')
| -rw-r--r-- | src/analyze.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp index b9a5188b71..2b2c303ea8 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2289,13 +2289,21 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent add_node_error(g, source_node, buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name))); variable_entry->value->type = g->builtin_types.entry_invalid; - } else if (src_tld == nullptr) { - Tld *tld = find_decl(g, parent_scope, name); - if (tld) { - ErrorMsg *msg = add_node_error(g, source_node, - buf_sprintf("redefinition of '%s'", buf_ptr(name))); - add_error_note(g, msg, tld->source_node, buf_sprintf("previous definition is here")); - variable_entry->value->type = g->builtin_types.entry_invalid; + } else { + Scope *search_scope = nullptr; + if (src_tld == nullptr) { + search_scope = parent_scope; + } else if (src_tld->parent_scope != nullptr && src_tld->parent_scope->parent != nullptr) { + search_scope = src_tld->parent_scope->parent; + } + if (search_scope != nullptr) { + Tld *tld = find_decl(g, search_scope, name); + if (tld != nullptr) { + ErrorMsg *msg = add_node_error(g, source_node, + buf_sprintf("redefinition of '%s'", buf_ptr(name))); + add_error_note(g, msg, tld->source_node, buf_sprintf("previous definition is here")); + variable_entry->value->type = g->builtin_types.entry_invalid; + } } } } |
