authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-06-03 15:09:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-06-03 15:09:40-04:00
loge5b90651ba118cb0d3293c83bdfbc62c40f4c266
tree35b7af7aeef87a6bbd8f621c0c4f763156945555
parente64f0971fc788cefb348e9516cd3b284df7ea0df

compileError builtin includes "referenced by" notes

to help track down the cause closes #278

6 files changed, 54 insertions(+), 18 deletions(-)

src/all_types.hpp+1-2
...@@ -1453,8 +1453,6 @@ struct CodeGen {...@@ -1453,8 +1453,6 @@ struct CodeGen {
1453 FnTableEntry *extern_panic_fn;1453 FnTableEntry *extern_panic_fn;
1454 LLVMValueRef cur_ret_ptr;1454 LLVMValueRef cur_ret_ptr;
1455 LLVMValueRef cur_fn_val;1455 LLVMValueRef cur_fn_val;
1456 ZigList<LLVMBasicBlockRef> break_block_stack;
1457 ZigList<LLVMBasicBlockRef> continue_block_stack;
1458 bool c_want_stdint;1456 bool c_want_stdint;
1459 bool c_want_stdbool;1457 bool c_want_stdbool;
1460 AstNode *root_export_decl;1458 AstNode *root_export_decl;
...@@ -1510,6 +1508,7 @@ struct CodeGen {...@@ -1510,6 +1508,7 @@ struct CodeGen {
1510 Buf *out_h_path;1508 Buf *out_h_path;
15111509
1512 ZigList<FnTableEntry *> inline_fns;1510 ZigList<FnTableEntry *> inline_fns;
1511 ZigList<AstNode *> tld_ref_source_node_stack;
1513};1512};
15141513
1515enum VarLinkage {1514enum VarLinkage {
src/analyze.cpp+7-5
...@@ -2086,7 +2086,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source...@@ -2086,7 +2086,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
20862086
2087void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {2087void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
2088 Tld *tld = g->compile_var_import->decls_scope->decl_table.get(name);2088 Tld *tld = g->compile_var_import->decls_scope->decl_table.get(name);
2089 resolve_top_level_decl(g, tld, false);2089 resolve_top_level_decl(g, tld, false, tld->source_node);
2090 assert(tld->id == TldIdVar);2090 assert(tld->id == TldIdVar);
2091 TldVar *tld_var = (TldVar *)tld;2091 TldVar *tld_var = (TldVar *)tld;
2092 tld_var->var->value = value;2092 tld_var->var->value = value;
...@@ -2399,7 +2399,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -2399,7 +2399,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
2399 g->global_vars.append(tld_var);2399 g->global_vars.append(tld_var);
2400}2400}
24012401
2402void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {2402void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node) {
2403 if (tld->resolution != TldResolutionUnresolved)2403 if (tld->resolution != TldResolutionUnresolved)
2404 return;2404 return;
24052405
...@@ -2407,10 +2407,11 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {...@@ -2407,10 +2407,11 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
2407 add_node_error(g, tld->source_node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));2407 add_node_error(g, tld->source_node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));
2408 tld->resolution = TldResolutionInvalid;2408 tld->resolution = TldResolutionInvalid;
2409 return;2409 return;
2410 } else {
2411 tld->dep_loop_flag = true;
2412 }2410 }
24132411
2412 tld->dep_loop_flag = true;
2413 g->tld_ref_source_node_stack.append(source_node);
2414
2414 switch (tld->id) {2415 switch (tld->id) {
2415 case TldIdVar:2416 case TldIdVar:
2416 {2417 {
...@@ -2440,6 +2441,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {...@@ -2440,6 +2441,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
24402441
2441 tld->resolution = TldResolutionOk;2442 tld->resolution = TldResolutionOk;
2442 tld->dep_loop_flag = false;2443 tld->dep_loop_flag = false;
2444 g->tld_ref_source_node_stack.pop();
2443}2445}
24442446
2445bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type) {2447bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type) {
...@@ -3056,7 +3058,7 @@ void semantic_analyze(CodeGen *g) {...@@ -3056,7 +3058,7 @@ void semantic_analyze(CodeGen *g) {
3056 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {3058 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
3057 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);3059 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
3058 bool pointer_only = false;3060 bool pointer_only = false;
3059 resolve_top_level_decl(g, tld, pointer_only);3061 resolve_top_level_decl(g, tld, pointer_only, nullptr);
3060 }3062 }
30613063
3062 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {3064 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
src/analyze.hpp+1-1
...@@ -50,7 +50,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a...@@ -50,7 +50,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a
50bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type);50bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type);
51VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name);51VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name);
52Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);52Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
53void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only);53void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node);
54bool type_is_codegen_pointer(TypeTableEntry *type);54bool type_is_codegen_pointer(TypeTableEntry *type);
55TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);55TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);
56TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);56TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);
src/ir.cpp+19-7
...@@ -7909,7 +7909,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {...@@ -7909,7 +7909,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
79097909
7910static ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {7910static ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
7911 Tld *tld = codegen->compile_var_import->decls_scope->decl_table.get(buf_create_from_str(name));7911 Tld *tld = codegen->compile_var_import->decls_scope->decl_table.get(buf_create_from_str(name));
7912 resolve_top_level_decl(codegen, tld, false);7912 resolve_top_level_decl(codegen, tld, false, nullptr);
7913 assert(tld->id == TldIdVar);7913 assert(tld->id == TldIdVar);
7914 TldVar *tld_var = (TldVar *)tld;7914 TldVar *tld_var = (TldVar *)tld;
7915 ConstExprValue *var_value = tld_var->var->value;7915 ConstExprValue *var_value = tld_var->var->value;
...@@ -10032,7 +10032,7 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -10032,7 +10032,7 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
10032 auto entry = container_scope->decl_table.maybe_get(field_name);10032 auto entry = container_scope->decl_table.maybe_get(field_name);
10033 Tld *tld = entry ? entry->value : nullptr;10033 Tld *tld = entry ? entry->value : nullptr;
10034 if (tld && tld->id == TldIdFn) {10034 if (tld && tld->id == TldIdFn) {
10035 resolve_top_level_decl(ira->codegen, tld, false);10035 resolve_top_level_decl(ira->codegen, tld, false, field_ptr_instruction->base.source_node);
10036 if (tld->resolution == TldResolutionInvalid)10036 if (tld->resolution == TldResolutionInvalid)
10037 return ira->codegen->builtin_types.entry_invalid;10037 return ira->codegen->builtin_types.entry_invalid;
10038 TldFn *tld_fn = (TldFn *)tld;10038 TldFn *tld_fn = (TldFn *)tld;
...@@ -10117,7 +10117,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field...@@ -10117,7 +10117,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
1011710117
10118static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {10118static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
10119 bool pointer_only = false;10119 bool pointer_only = false;
10120 resolve_top_level_decl(ira->codegen, tld, pointer_only);10120 resolve_top_level_decl(ira->codegen, tld, pointer_only, source_instruction->source_node);
10121 if (tld->resolution == TldResolutionInvalid)10121 if (tld->resolution == TldResolutionInvalid)
10122 return ira->codegen->builtin_types.entry_invalid;10122 return ira->codegen->builtin_types.entry_invalid;
1012310123
...@@ -10539,7 +10539,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,...@@ -10539,7 +10539,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
10539 Tld *tld = instruction->tld;10539 Tld *tld = instruction->tld;
10540 IrInstruction *align_value = instruction->value->other;10540 IrInstruction *align_value = instruction->value->other;
1054110541
10542 resolve_top_level_decl(ira->codegen, tld, true);10542 resolve_top_level_decl(ira->codegen, tld, true, instruction->base.source_node);
10543 if (tld->resolution == TldResolutionInvalid)10543 if (tld->resolution == TldResolutionInvalid)
10544 return ira->codegen->builtin_types.entry_invalid;10544 return ira->codegen->builtin_types.entry_invalid;
1054510545
...@@ -10602,7 +10602,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,...@@ -10602,7 +10602,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,
10602 Tld *tld = instruction->tld;10602 Tld *tld = instruction->tld;
10603 IrInstruction *section_value = instruction->value->other;10603 IrInstruction *section_value = instruction->value->other;
1060410604
10605 resolve_top_level_decl(ira->codegen, tld, true);10605 resolve_top_level_decl(ira->codegen, tld, true, instruction->base.source_node);
10606 if (tld->resolution == TldResolutionInvalid)10606 if (tld->resolution == TldResolutionInvalid)
10607 return ira->codegen->builtin_types.entry_invalid;10607 return ira->codegen->builtin_types.entry_invalid;
1060810608
...@@ -11932,7 +11932,19 @@ static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,...@@ -11932,7 +11932,19 @@ static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,
11932 if (!msg_buf)11932 if (!msg_buf)
11933 return ira->codegen->builtin_types.entry_invalid;11933 return ira->codegen->builtin_types.entry_invalid;
1193411934
11935 ir_add_error(ira, &instruction->base, msg_buf);11935 ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf);
11936 size_t i = ira->codegen->tld_ref_source_node_stack.length;
11937 for (;;) {
11938 if (i == 0)
11939 break;
11940 i -= 1;
11941 AstNode *source_node = ira->codegen->tld_ref_source_node_stack.at(i);
11942 if (source_node) {
11943 add_error_note(ira->codegen, msg, source_node,
11944 buf_sprintf("referenced here"));
11945 }
11946 }
11947
11936 return ira->codegen->builtin_types.entry_invalid;11948 return ira->codegen->builtin_types.entry_invalid;
11937}11949}
1193811950
...@@ -13454,7 +13466,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_ref(IrAnalyze *ira,...@@ -13454,7 +13466,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
13454 Tld *tld = instruction->tld;13466 Tld *tld = instruction->tld;
13455 LVal lval = instruction->lval;13467 LVal lval = instruction->lval;
1345613468
13457 resolve_top_level_decl(ira->codegen, tld, lval.is_ptr);13469 resolve_top_level_decl(ira->codegen, tld, lval.is_ptr, instruction->base.source_node);
13458 if (tld->resolution == TldResolutionInvalid)13470 if (tld->resolution == TldResolutionInvalid)
13459 return ira->codegen->builtin_types.entry_invalid;13471 return ira->codegen->builtin_types.entry_invalid;
1346013472
std/special/bootstrap.zig+12-3
...@@ -8,7 +8,8 @@ const builtin = @import("builtin");...@@ -8,7 +8,8 @@ const builtin = @import("builtin");
8const want_main_symbol = std.target.linking_libc;8const want_main_symbol = std.target.linking_libc;
9const want_start_symbol = !want_main_symbol;9const want_start_symbol = !want_main_symbol;
1010
11const exit = std.os.posix.exit;11const posix_exit = std.os.posix.exit;
12extern fn ExitProcess(exit_code: c_uint) -> noreturn;
1213
13var argc_ptr: &usize = undefined;14var argc_ptr: &usize = undefined;
1415
...@@ -34,8 +35,16 @@ fn callMainAndExit() -> noreturn {...@@ -34,8 +35,16 @@ fn callMainAndExit() -> noreturn {
34 const argc = *argc_ptr;35 const argc = *argc_ptr;
35 const argv = @ptrCast(&&u8, &argc_ptr[1]);36 const argv = @ptrCast(&&u8, &argc_ptr[1]);
36 const envp = @ptrCast(&?&u8, &argv[argc + 1]);37 const envp = @ptrCast(&?&u8, &argv[argc + 1]);
37 callMain(argc, argv, envp) %% exit(1);38 callMain(argc, argv, envp) %% exit(true);
38 exit(0);39 exit(false);
40}
41
42fn exit(failure: bool) -> noreturn {
43 if (builtin.os == builtin.Os.windows) {
44 ExitProcess(c_uint(failure));
45 } else {
46 posix_exit(i32(failure));
47 }
39}48}
4049
41fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {50fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
test/compile_errors.zig+14
...@@ -1916,4 +1916,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1916,4 +1916,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1916 \\}1916 \\}
1917 ,1917 ,
1918 ".tmp_source.zig:7:9: error: calling a generic function requires compile-time known function value");1918 ".tmp_source.zig:7:9: error: calling a generic function requires compile-time known function value");
1919
1920 cases.add("@compileError shows traceback of references that caused it",
1921 \\const foo = @compileError("aoeu");
1922 \\
1923 \\const bar = baz + foo;
1924 \\const baz = 1;
1925 \\
1926 \\export fn entry() -> i32 {
1927 \\ return bar;
1928 \\}
1929 ,
1930 ".tmp_source.zig:1:13: error: aoeu",
1931 ".tmp_source.zig:3:19: note: referenced here",
1932 ".tmp_source.zig:7:12: note: referenced here");
1919}1933}