authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-23 22:40:12+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-23 22:40:12+01:00
log8d9b8ab930d8633b9e9e77a80a36fa08a5e7f3f5
tree19606bbd361440098d445c7781f7fa0a9cbb39ef
parent357f42da6c2f158fff2afa28a56b7e0e7a8a5963

More error checking for unresolved TLDs

Closes #4274

2 files changed, 34 insertions(+), 16 deletions(-)

src/ir.cpp+28-16
...@@ -264,6 +264,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n...@@ -264,6 +264,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
264 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);264 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
265static ResultLoc *no_result_loc(void);265static ResultLoc *no_result_loc(void);
266static IrInstruction *ir_analyze_test_non_null(IrAnalyze *ira, IrInstruction *source_inst, IrInstruction *value);266static IrInstruction *ir_analyze_test_non_null(IrAnalyze *ira, IrInstruction *source_inst, IrInstruction *value);
267static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr);
267268
268static void destroy_instruction(IrInstruction *inst) {269static void destroy_instruction(IrInstruction *inst) {
269#ifdef ZIG_ENABLE_MEM_PROFILE270#ifdef ZIG_ENABLE_MEM_PROFILE
...@@ -20022,8 +20023,13 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -20022,8 +20023,13 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
20022 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);20023 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);
20023 if (tld->resolution == TldResolutionInvalid)20024 if (tld->resolution == TldResolutionInvalid)
20024 return ira->codegen->invalid_instruction;20025 return ira->codegen->invalid_instruction;
20026 if (tld->resolution == TldResolutionResolving)
20027 return ir_error_dependency_loop(ira, source_instr);
20028
20025 TldFn *tld_fn = (TldFn *)tld;20029 TldFn *tld_fn = (TldFn *)tld;
20026 ZigFn *fn_entry = tld_fn->fn_entry;20030 ZigFn *fn_entry = tld_fn->fn_entry;
20031 assert(fn_entry != nullptr);
20032
20027 if (type_is_invalid(fn_entry->type_entry))20033 if (type_is_invalid(fn_entry->type_entry))
20028 return ira->codegen->invalid_instruction;20034 return ira->codegen->invalid_instruction;
2002920035
...@@ -20034,8 +20040,13 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -20034,8 +20040,13 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
20034 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);20040 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);
20035 if (tld->resolution == TldResolutionInvalid)20041 if (tld->resolution == TldResolutionInvalid)
20036 return ira->codegen->invalid_instruction;20042 return ira->codegen->invalid_instruction;
20043 if (tld->resolution == TldResolutionResolving)
20044 return ir_error_dependency_loop(ira, source_instr);
20045
20037 TldVar *tld_var = (TldVar *)tld;20046 TldVar *tld_var = (TldVar *)tld;
20038 ZigVar *var = tld_var->var;20047 ZigVar *var = tld_var->var;
20048 assert(var != nullptr);
20049
20039 if (type_is_invalid(var->var_type))20050 if (type_is_invalid(var->var_type))
20040 return ira->codegen->invalid_instruction;20051 return ira->codegen->invalid_instruction;
2004120052
...@@ -20369,9 +20380,10 @@ static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *so...@@ -20369,9 +20380,10 @@ static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *so
2036920380
20370static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {20381static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
20371 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, true);20382 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, true);
20372 if (tld->resolution == TldResolutionInvalid) {20383 if (tld->resolution == TldResolutionInvalid)
20373 return ira->codegen->invalid_instruction;20384 return ira->codegen->invalid_instruction;
20374 }20385 if (tld->resolution == TldResolutionResolving)
20386 return ir_error_dependency_loop(ira, source_instruction);
2037520387
20376 switch (tld->id) {20388 switch (tld->id) {
20377 case TldIdContainer:20389 case TldIdContainer:
...@@ -20381,9 +20393,8 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_...@@ -20381,9 +20393,8 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
20381 case TldIdVar: {20393 case TldIdVar: {
20382 TldVar *tld_var = (TldVar *)tld;20394 TldVar *tld_var = (TldVar *)tld;
20383 ZigVar *var = tld_var->var;20395 ZigVar *var = tld_var->var;
20384 if (var == nullptr) {20396 assert(var != nullptr);
20385 return ir_error_dependency_loop(ira, source_instruction);20397
20386 }
20387 if (tld_var->extern_lib_name != nullptr) {20398 if (tld_var->extern_lib_name != nullptr) {
20388 add_link_lib_symbol(ira, tld_var->extern_lib_name, buf_create_from_str(var->name),20399 add_link_lib_symbol(ira, tld_var->extern_lib_name, buf_create_from_str(var->name),
20389 source_instruction->source_node);20400 source_instruction->source_node);
...@@ -20394,7 +20405,7 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_...@@ -20394,7 +20405,7 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
20394 case TldIdFn: {20405 case TldIdFn: {
20395 TldFn *tld_fn = (TldFn *)tld;20406 TldFn *tld_fn = (TldFn *)tld;
20396 ZigFn *fn_entry = tld_fn->fn_entry;20407 ZigFn *fn_entry = tld_fn->fn_entry;
20397 assert(fn_entry->type_entry);20408 assert(fn_entry->type_entry != nullptr);
2039820409
20399 if (type_is_invalid(fn_entry->type_entry))20410 if (type_is_invalid(fn_entry->type_entry))
20400 return ira->codegen->invalid_instruction;20411 return ira->codegen->invalid_instruction;
...@@ -22628,11 +22639,14 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -22628,11 +22639,14 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2262822639
22629 while ((curr_entry = decl_it.next()) != nullptr) {22640 while ((curr_entry = decl_it.next()) != nullptr) {
22630 // If the declaration is unresolved, force it to be resolved again.22641 // If the declaration is unresolved, force it to be resolved again.
22631 if (curr_entry->value->resolution == TldResolutionUnresolved) {22642 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);
22632 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);22643 if (curr_entry->value->resolution == TldResolutionInvalid) {
22633 if (curr_entry->value->resolution != TldResolutionOk) {22644 return ErrorSemanticAnalyzeFail;
22634 return ErrorSemanticAnalyzeFail;22645 }
22635 }22646
22647 if (curr_entry->value->resolution == TldResolutionResolving) {
22648 ir_error_dependency_loop(ira, source_instr);
22649 return ErrorSemanticAnalyzeFail;
22636 }22650 }
2263722651
22638 // Skip comptime blocks and test functions.22652 // Skip comptime blocks and test functions.
...@@ -22689,6 +22703,8 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -22689,6 +22703,8 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
22689 case TldIdVar:22703 case TldIdVar:
22690 {22704 {
22691 ZigVar *var = ((TldVar *)curr_entry->value)->var;22705 ZigVar *var = ((TldVar *)curr_entry->value)->var;
22706 assert(var != nullptr);
22707
22692 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))22708 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
22693 return ErrorSemanticAnalyzeFail;22709 return ErrorSemanticAnalyzeFail;
2269422710
...@@ -22719,11 +22735,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -22719,11 +22735,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2271922735
22720 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;22736 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
22721 assert(!fn_entry->is_test);22737 assert(!fn_entry->is_test);
2272222738 assert(fn_entry->type_entry != nullptr);
22723 if (fn_entry->type_entry == nullptr) {
22724 ir_error_dependency_loop(ira, source_instr);
22725 return ErrorSemanticAnalyzeFail;
22726 }
2272722739
22728 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;22740 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;
2272922741
test/compile_errors.zig+6
...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("dependency loop in top-level decl with @TypeInfo",
6 \\export const foo = @typeInfo(@This());
7 , &[_][]const u8{
8 "tmp.zig:1:20: error: dependency loop detected",
9 });
10
5 cases.addTest("non-exhaustive enums",11 cases.addTest("non-exhaustive enums",
6 \\const A = enum {12 \\const A = enum {
7 \\ a,13 \\ a,