authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 20:52:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 20:52:40-05:00
log132e2fa5d9cc17088082cf4797701214c20845f1
treefaf24be2d876da8ec77edcc5e5fa56b5217a7099
parentcfc9f7422f3b0e4077eca42d7b71cd5523dad0a9

errors from inline fn calls include stack trace


5 files changed, 26 insertions(+), 10 deletions(-)

src/all_types.hpp+2
......@@ -55,6 +55,8 @@ struct IrExecutable {
5555 bool is_inline;
5656 FnTableEntry *fn_entry;
5757 Buf *c_import_buf;
58 AstNode *source_node;
59 IrExecutable *parent_exec;
5860};
5961
6062enum OutType {
src/analyze.cpp+1-1
......@@ -875,7 +875,7 @@ static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *nod
875875 size_t backward_branch_count = 0;
876876 return ir_eval_const_value(g, scope, node, type_entry,
877877 &backward_branch_count, default_backward_branch_quota,
878 nullptr, nullptr, node, type_name);
878 nullptr, nullptr, node, type_name, nullptr);
879879}
880880
881881TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
src/ir.cpp+19-6
......@@ -4169,9 +4169,17 @@ IrInstruction *ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {
41694169 return ir_gen(codegen, body_node, fn_entry->child_scope, ir_executable);
41704170}
41714171
4172static void add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg *err_msg, int limit) {
4173 if (!exec || !exec->source_node || limit < 0) return;
4174 add_error_note(codegen, err_msg, exec->source_node, buf_sprintf("called from here"));
4175 add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
4176}
4177
41724178static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
41734179 ira->new_irb.exec->invalid = true;
4174 return add_node_error(ira->codegen, source_instruction->source_node, msg);
4180 ErrorMsg *err_msg = add_node_error(ira->codegen, source_instruction->source_node, msg);
4181 add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);
4182 return err_msg;
41754183}
41764184
41774185static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
......@@ -4680,9 +4688,12 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
46804688
46814689IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
46824690 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
4683 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name)
4691 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
4692 IrExecutable *parent_exec)
46844693{
46854694 IrExecutable ir_executable = {0};
4695 ir_executable.source_node = source_node;
4696 ir_executable.parent_exec = parent_exec;
46864697 ir_executable.name = exec_name;
46874698 ir_executable.is_inline = true;
46884699 ir_executable.fn_entry = fn_entry;
......@@ -4700,6 +4711,8 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
47004711 fprintf(stderr, "}\n");
47014712 }
47024713 IrExecutable analyzed_executable = {0};
4714 analyzed_executable.source_node = source_node;
4715 analyzed_executable.parent_exec = parent_exec;
47034716 analyzed_executable.name = exec_name;
47044717 analyzed_executable.is_inline = true;
47054718 analyzed_executable.fn_entry = fn_entry;
......@@ -4730,7 +4743,7 @@ static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value
47304743 return ira->codegen->builtin_types.entry_invalid;
47314744
47324745 if (type_value->type_entry->id != TypeTableEntryIdMetaType) {
4733 add_node_error(ira->codegen, type_value->source_node,
4746 ir_add_error(ira, type_value,
47344747 buf_sprintf("expected type 'type', found '%s'", buf_ptr(&type_value->type_entry->name)));
47354748 return ira->codegen->builtin_types.entry_invalid;
47364749 }
......@@ -5152,7 +5165,7 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ
51525165 ImplicitCastMatchResult result = ir_types_match_with_implicit_cast(ira, expected_type, value->type_entry, value);
51535166 switch (result) {
51545167 case ImplicitCastMatchResultNo:
5155 add_node_error(ira->codegen, first_executing_node(value->source_node),
5168 ir_add_error(ira, value,
51565169 buf_sprintf("expected type '%s', found '%s'",
51575170 buf_ptr(&expected_type->name),
51585171 buf_ptr(&value->type_entry->name)));
......@@ -6147,7 +6160,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
61476160 AstNode *body_node = fn_entry->fn_def_node->data.fn_def.body;
61486161 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
61496162 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
6150 nullptr, call_instruction->base.source_node, nullptr);
6163 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec);
61516164 if (result->type_entry->id == TypeTableEntryIdInvalid)
61526165 return ira->codegen->builtin_types.entry_invalid;
61536166
......@@ -8432,7 +8445,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc
84328445 TypeTableEntry *void_type = ira->codegen->builtin_types.entry_void;
84338446 IrInstruction *result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
84348447 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
8435 &cimport_scope->buf, block_node, nullptr);
8448 &cimport_scope->buf, block_node, nullptr, nullptr);
84368449 if (result->type_entry->id == TypeTableEntryIdInvalid)
84378450 return ira->codegen->builtin_types.entry_invalid;
84388451
src/ir.hpp+2-1
......@@ -15,7 +15,8 @@ IrInstruction *ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);
1515
1616IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
18 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name);
18 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec);
1920
2021TypeTableEntry *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
2122 TypeTableEntry *expected_type, AstNode *expected_type_source_node);
std/test_runner.zig+2-2
......@@ -1,9 +1,9 @@
11const io = @import("std").io;
22
3struct TestFn {
3const TestFn = struct {
44 name: []u8,
55 func: extern fn(),
6}
6};
77
88extern var zig_test_fn_list: []TestFn;
99