From 140dc2f43e347ddddc2f6f344b388bdc74c97c56 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 20 May 2020 23:13:02 -0400 Subject: [PATCH] stage1: fix false positive redeclared variable compile error --- src/analyze.cpp | 21 ++++++++++++++++++--- src/ir.cpp | 15 ++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 5a2629a0ea..0fddc568a5 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3612,6 +3612,12 @@ 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; + if (other_tld->id == TldIdVar) { + ZigVar *var = reinterpret_cast(other_tld)->var; + if (var->var_type != nullptr && type_is_invalid(var->var_type)) { + return; // already reported compile error + } + } 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; @@ -3887,9 +3893,18 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf if (search_scope != nullptr) { Tld *tld = find_decl(g, search_scope, name); if (tld != nullptr && tld != src_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")); + bool want_err_msg = true; + if (tld->id == TldIdVar) { + ZigVar *var = reinterpret_cast(tld)->var; + if (var->var_type != nullptr && type_is_invalid(var->var_type)) { + want_err_msg = false; + } + } + if (want_err_msg) { + 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->var_type = g->builtin_types.entry_invalid; } } diff --git a/src/ir.cpp b/src/ir.cpp index 20e83ecb9d..cb0b6223c7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5300,9 +5300,18 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s } else { Tld *tld = find_decl(codegen, parent_scope, name); if (tld != nullptr) { - ErrorMsg *msg = add_node_error(codegen, node, - buf_sprintf("redefinition of '%s'", buf_ptr(name))); - add_error_note(codegen, msg, tld->source_node, buf_sprintf("previous definition is here")); + bool want_err_msg = true; + if (tld->id == TldIdVar) { + ZigVar *var = reinterpret_cast(tld)->var; + if (var->var_type != nullptr && type_is_invalid(var->var_type)) { + want_err_msg = false; + } + } + if (want_err_msg) { + ErrorMsg *msg = add_node_error(codegen, node, + buf_sprintf("redefinition of '%s'", buf_ptr(name))); + add_error_note(codegen, msg, tld->source_node, buf_sprintf("previous definition is here")); + } variable_entry->var_type = codegen->builtin_types.entry_invalid; } }