authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-20 08:04:46-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-20 08:04:46-05:00
log079728752eca4cffbb4f7e8dc06d5e23b81d7627
tree552dcf5837bf1b3f4cab297bed4a9e0e0bea4c0f
parent067968c57f2c7ed4f0606913aa608f6c2be418d2
signaturelock-open Commit is signed but in an unrecognized format.

deduplicate compile errors for undeclared identifiers

closes #111

3 files changed, 38 insertions(+), 8 deletions(-)

src/ir.cpp+18-1
......@@ -3674,6 +3674,22 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode
36743674 return ir_build_const_null(irb, scope, node);
36753675}
36763676
3677static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode *node, Buf *var_name) {
3678 ScopeDecls *scope_decls = nullptr;
3679 while (scope != nullptr) {
3680 if (scope->id == ScopeIdDecls) {
3681 scope_decls = reinterpret_cast<ScopeDecls *>(scope);
3682 }
3683 scope = scope->parent;
3684 }
3685 TldVar *tld_var = allocate<TldVar>(1);
3686 init_tld(&tld_var->base, TldIdVar, var_name, VisibModPub, node, &scope_decls->base);
3687 tld_var->base.resolution = TldResolutionInvalid;
3688 tld_var->var = add_variable(g, node, &scope_decls->base, var_name, false,
3689 &g->invalid_instruction->value, &tld_var->base, g->builtin_types.entry_invalid);
3690 scope_decls->decl_table.put(var_name, &tld_var->base);
3691}
3692
36773693static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
36783694 Error err;
36793695 assert(node->type == NodeTypeSymbol);
......@@ -3727,8 +3743,9 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
37273743 return irb->codegen->invalid_instruction;
37283744 }
37293745
3730 // TODO put a variable of same name with invalid type in global scope
3746 // put a variable of same name with invalid type in global scope
37313747 // so that future references to this same name will find a variable with an invalid type
3748 populate_invalid_variable_in_scope(irb->codegen, scope, node, variable_name);
37323749 add_node_error(irb->codegen, node, buf_sprintf("use of undeclared identifier '%s'", buf_ptr(variable_name)));
37333750 return irb->codegen->invalid_instruction;
37343751}
test/compile_errors.zig+18-6
......@@ -1,6 +1,22 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.addCase(x: {
5 var tc = cases.create(
6 "deduplicate undeclared identifier",
7 \\export fn a() void {
8 \\ x += 1;
9 \\}
10 \\export fn b() void {
11 \\ x += 1;
12 \\}
13 ,
14 ".tmp_source.zig:2:5: error: use of undeclared identifier 'x'",
15 );
16 tc.expect_exact = true;
17 break :x tc;
18 });
19
420 cases.addTest(
521 "export generic function",
622 \\export fn foo(num: var) i32 {
......@@ -2280,7 +2296,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22802296 \\}
22812297 ,
22822298 ".tmp_source.zig:2:5: error: use of undeclared identifier 'i'",
2283 ".tmp_source.zig:2:12: error: use of undeclared identifier 'i'",
22842299 );
22852300
22862301 cases.add(
......@@ -5618,8 +5633,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56185633 ".tmp_source.zig:2:26: error: vector element type must be integer, float, or pointer; '@Vector(4, u8)' is invalid",
56195634 );
56205635
5621 cases.add(
5622 "compileLog of tagged enum doesn't crash the compiler",
5636 cases.add("compileLog of tagged enum doesn't crash the compiler",
56235637 \\const Bar = union(enum(u32)) {
56245638 \\ X: i32 = 1
56255639 \\};
......@@ -5631,7 +5645,5 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56315645 \\pub fn main () void {
56325646 \\ comptime testCompileLog(Bar{.X = 123});
56335647 \\}
5634 ,
5635 ".tmp_source.zig:6:5: error: found compile log statement"
5636 );
5648 , ".tmp_source.zig:6:5: error: found compile log statement");
56375649}
test/tests.zig+2-1
......@@ -716,7 +716,8 @@ pub const CompileErrorContext = struct {
716716 for (self.case.expected_errors.toSliceConst()) |expected| {
717717 if (mem.indexOf(u8, stderr, expected) == null) {
718718 warn(
719 \\\n=========== Expected compile error: ============
719 \\
720 \\=========== Expected compile error: ============
720721 \\{}
721722 \\
722723 , expected);