authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-04 23:21:33-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-04 23:21:33-05:00
logc32a060d4fd432002d32ff17a4cf9ae491cddfc0
treee7754a644be63c3922d68b1bc67b6c2e5cf424d9
parent664b41af65d3c9bbc8e09ffcaa91d404fe87d7a0

IR: add unreachable code compiler error


6 files changed, 324 insertions(+), 295 deletions(-)

src/all_types.hpp+2
...@@ -1494,6 +1494,8 @@ struct IrInstruction {...@@ -1494,6 +1494,8 @@ struct IrInstruction {
1494 size_t ref_count;1494 size_t ref_count;
1495 IrInstruction *other;1495 IrInstruction *other;
1496 IrBasicBlock *owner_bb;1496 IrBasicBlock *owner_bb;
1497 // true if this instruction was generated by zig and not from user code
1498 bool is_gen;
1497};1499};
14981500
1499struct IrInstructionCondBr {1501struct IrInstructionCondBr {
src/analyze.cpp+2
...@@ -2620,6 +2620,8 @@ bool is_node_void_expr(AstNode *node) {...@@ -2620,6 +2620,8 @@ bool is_node_void_expr(AstNode *node) {
2620 {2620 {
2621 return true;2621 return true;
2622 }2622 }
2623 } else if (node->type == NodeTypeBlock && node->data.block.statements.length == 0) {
2624 return true;
2623 }2625 }
26242626
2625 return false;2627 return false;
src/ir.cpp+241-188
...@@ -98,6 +98,10 @@ static bool instr_is_comptime(IrInstruction *instruction) {...@@ -98,6 +98,10 @@ static bool instr_is_comptime(IrInstruction *instruction) {
98 return instruction->value.special != ConstValSpecialRuntime;98 return instruction->value.special != ConstValSpecialRuntime;
99}99}
100100
101static bool instr_is_unreachable(IrInstruction *instruction) {
102 return instruction->value.type && instruction->value.type->id == TypeTableEntryIdUnreachable;
103}
104
101static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {105static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {
102 new_instruction->other = old_instruction;106 new_instruction->other = old_instruction;
103 old_instruction->other = new_instruction;107 old_instruction->other = new_instruction;
...@@ -112,8 +116,10 @@ static void ir_ref_bb(IrBasicBlock *bb) {...@@ -112,8 +116,10 @@ static void ir_ref_bb(IrBasicBlock *bb) {
112 bb->ref_count += 1;116 bb->ref_count += 1;
113}117}
114118
115static void ir_ref_instruction(IrInstruction *instruction) {119static void ir_ref_instruction(IrInstruction *instruction, IrBasicBlock *cur_bb) {
116 instruction->ref_count += 1;120 instruction->ref_count += 1;
121 if (instruction->owner_bb != cur_bb)
122 ir_ref_bb(instruction->owner_bb);
117}123}
118124
119static void ir_ref_var(VariableTableEntry *var) {125static void ir_ref_var(VariableTableEntry *var) {
...@@ -499,7 +505,7 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, Scope *scope, AstNode *sourc...@@ -499,7 +505,7 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, Scope *scope, AstNode *sourc
499 cast_instruction->value = value;505 cast_instruction->value = value;
500 cast_instruction->cast_op = cast_op;506 cast_instruction->cast_op = cast_op;
501507
502 ir_ref_instruction(value);508 ir_ref_instruction(value, irb->current_basic_block);
503509
504 return &cast_instruction->base;510 return &cast_instruction->base;
505}511}
...@@ -515,10 +521,10 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so...@@ -515,10 +521,10 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so
515 cond_br_instruction->else_block = else_block;521 cond_br_instruction->else_block = else_block;
516 cond_br_instruction->is_comptime = is_comptime;522 cond_br_instruction->is_comptime = is_comptime;
517523
518 ir_ref_instruction(condition);524 ir_ref_instruction(condition, irb->current_basic_block);
519 ir_ref_bb(then_block);525 ir_ref_bb(then_block);
520 ir_ref_bb(else_block);526 ir_ref_bb(else_block);
521 if (is_comptime) ir_ref_instruction(is_comptime);527 if (is_comptime) ir_ref_instruction(is_comptime, irb->current_basic_block);
522528
523 return &cond_br_instruction->base;529 return &cond_br_instruction->base;
524}530}
...@@ -538,7 +544,7 @@ static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -538,7 +544,7 @@ static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *sou
538 return_instruction->base.value.special = ConstValSpecialStatic;544 return_instruction->base.value.special = ConstValSpecialStatic;
539 return_instruction->value = return_value;545 return_instruction->value = return_value;
540546
541 ir_ref_instruction(return_value);547 ir_ref_instruction(return_value, irb->current_basic_block);
542548
543 return &return_instruction->base;549 return &return_instruction->base;
544}550}
...@@ -699,8 +705,8 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -699,8 +705,8 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *sou
699 bin_op_instruction->op2 = op2;705 bin_op_instruction->op2 = op2;
700 bin_op_instruction->safety_check_on = safety_check_on;706 bin_op_instruction->safety_check_on = safety_check_on;
701707
702 ir_ref_instruction(op1);708 ir_ref_instruction(op1, irb->current_basic_block);
703 ir_ref_instruction(op2);709 ir_ref_instruction(op2, irb->current_basic_block);
704710
705 return &bin_op_instruction->base;711 return &bin_op_instruction->base;
706}712}
...@@ -738,8 +744,8 @@ static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *s...@@ -738,8 +744,8 @@ static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *s
738 instruction->elem_index = elem_index;744 instruction->elem_index = elem_index;
739 instruction->safety_check_on = safety_check_on;745 instruction->safety_check_on = safety_check_on;
740746
741 ir_ref_instruction(array_ptr);747 ir_ref_instruction(array_ptr, irb->current_basic_block);
742 ir_ref_instruction(elem_index);748 ir_ref_instruction(elem_index, irb->current_basic_block);
743749
744 return &instruction->base;750 return &instruction->base;
745}751}
...@@ -760,7 +766,7 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *...@@ -760,7 +766,7 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *
760 instruction->container_ptr = container_ptr;766 instruction->container_ptr = container_ptr;
761 instruction->field_name = field_name;767 instruction->field_name = field_name;
762768
763 ir_ref_instruction(container_ptr);769 ir_ref_instruction(container_ptr, irb->current_basic_block);
764770
765 return &instruction->base;771 return &instruction->base;
766}772}
...@@ -772,7 +778,7 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As...@@ -772,7 +778,7 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
772 instruction->struct_ptr = struct_ptr;778 instruction->struct_ptr = struct_ptr;
773 instruction->field = field;779 instruction->field = field;
774780
775 ir_ref_instruction(struct_ptr);781 ir_ref_instruction(struct_ptr, irb->current_basic_block);
776782
777 return &instruction->base;783 return &instruction->base;
778}784}
...@@ -793,7 +799,7 @@ static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, Scope *scope, AstN...@@ -793,7 +799,7 @@ static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, Scope *scope, AstN
793 instruction->enum_ptr = enum_ptr;799 instruction->enum_ptr = enum_ptr;
794 instruction->field = field;800 instruction->field = field;
795801
796 ir_ref_instruction(enum_ptr);802 ir_ref_instruction(enum_ptr, irb->current_basic_block);
797803
798 return &instruction->base;804 return &instruction->base;
799}805}
...@@ -819,9 +825,9 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc...@@ -819,9 +825,9 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
819 call_instruction->arg_count = arg_count;825 call_instruction->arg_count = arg_count;
820826
821 if (fn_ref)827 if (fn_ref)
822 ir_ref_instruction(fn_ref);828 ir_ref_instruction(fn_ref, irb->current_basic_block);
823 for (size_t i = 0; i < arg_count; i += 1)829 for (size_t i = 0; i < arg_count; i += 1)
824 ir_ref_instruction(args[i]);830 ir_ref_instruction(args[i], irb->current_basic_block);
825831
826 return &call_instruction->base;832 return &call_instruction->base;
827}833}
...@@ -849,7 +855,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source...@@ -849,7 +855,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source
849855
850 for (size_t i = 0; i < incoming_count; i += 1) {856 for (size_t i = 0; i < incoming_count; i += 1) {
851 ir_ref_bb(incoming_blocks[i]);857 ir_ref_bb(incoming_blocks[i]);
852 ir_ref_instruction(incoming_values[i]);858 ir_ref_instruction(incoming_values[i], irb->current_basic_block);
853 }859 }
854860
855 return &phi_instruction->base;861 return &phi_instruction->base;
...@@ -874,7 +880,7 @@ static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source...@@ -874,7 +880,7 @@ static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source
874 br_instruction->is_comptime = is_comptime;880 br_instruction->is_comptime = is_comptime;
875881
876 ir_ref_bb(dest_block);882 ir_ref_bb(dest_block);
877 if (is_comptime) ir_ref_instruction(is_comptime);883 if (is_comptime) ir_ref_instruction(is_comptime, irb->current_basic_block);
878884
879 return &br_instruction->base;885 return &br_instruction->base;
880}886}
...@@ -898,7 +904,7 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -898,7 +904,7 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
898 br_instruction->op_id = op_id;904 br_instruction->op_id = op_id;
899 br_instruction->value = value;905 br_instruction->value = value;
900906
901 ir_ref_instruction(value);907 ir_ref_instruction(value, irb->current_basic_block);
902908
903 return &br_instruction->base;909 return &br_instruction->base;
904}910}
...@@ -921,9 +927,9 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,...@@ -921,9 +927,9 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,
921 container_init_list_instruction->item_count = item_count;927 container_init_list_instruction->item_count = item_count;
922 container_init_list_instruction->items = items;928 container_init_list_instruction->items = items;
923929
924 ir_ref_instruction(container_type);930 ir_ref_instruction(container_type, irb->current_basic_block);
925 for (size_t i = 0; i < item_count; i += 1) {931 for (size_t i = 0; i < item_count; i += 1) {
926 ir_ref_instruction(items[i]);932 ir_ref_instruction(items[i], irb->current_basic_block);
927 }933 }
928934
929 return &container_init_list_instruction->base;935 return &container_init_list_instruction->base;
...@@ -947,9 +953,9 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scop...@@ -947,9 +953,9 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scop
947 container_init_fields_instruction->field_count = field_count;953 container_init_fields_instruction->field_count = field_count;
948 container_init_fields_instruction->fields = fields;954 container_init_fields_instruction->fields = fields;
949955
950 ir_ref_instruction(container_type);956 ir_ref_instruction(container_type, irb->current_basic_block);
951 for (size_t i = 0; i < field_count; i += 1) {957 for (size_t i = 0; i < field_count; i += 1) {
952 ir_ref_instruction(fields[i].value);958 ir_ref_instruction(fields[i].value, irb->current_basic_block);
953 }959 }
954960
955 return &container_init_fields_instruction->base;961 return &container_init_fields_instruction->base;
...@@ -964,7 +970,7 @@ static IrInstruction *ir_build_struct_init(IrBuilder *irb, Scope *scope, AstNode...@@ -964,7 +970,7 @@ static IrInstruction *ir_build_struct_init(IrBuilder *irb, Scope *scope, AstNode
964 struct_init_instruction->fields = fields;970 struct_init_instruction->fields = fields;
965971
966 for (size_t i = 0; i < field_count; i += 1)972 for (size_t i = 0; i < field_count; i += 1)
967 ir_ref_instruction(fields[i].value);973 ir_ref_instruction(fields[i].value, irb->current_basic_block);
968974
969 return &struct_init_instruction->base;975 return &struct_init_instruction->base;
970}976}
...@@ -1001,8 +1007,8 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *...@@ -1001,8 +1007,8 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
1001 instruction->ptr = ptr;1007 instruction->ptr = ptr;
1002 instruction->value = value;1008 instruction->value = value;
10031009
1004 ir_ref_instruction(ptr);1010 ir_ref_instruction(ptr, irb->current_basic_block);
1005 ir_ref_instruction(value);1011 ir_ref_instruction(value, irb->current_basic_block);
10061012
1007 return &instruction->base;1013 return &instruction->base;
1008}1014}
...@@ -1026,8 +1032,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1026,8 +1032,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s
1026 decl_var_instruction->var_type = var_type;1032 decl_var_instruction->var_type = var_type;
1027 decl_var_instruction->init_value = init_value;1033 decl_var_instruction->init_value = init_value;
10281034
1029 if (var_type) ir_ref_instruction(var_type);1035 if (var_type) ir_ref_instruction(var_type, irb->current_basic_block);
1030 ir_ref_instruction(init_value);1036 ir_ref_instruction(init_value, irb->current_basic_block);
10311037
1032 return &decl_var_instruction->base;1038 return &decl_var_instruction->base;
1033}1039}
...@@ -1045,7 +1051,7 @@ static IrInstruction *ir_build_load_ptr(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1045,7 +1051,7 @@ static IrInstruction *ir_build_load_ptr(IrBuilder *irb, Scope *scope, AstNode *s
1045 IrInstructionLoadPtr *instruction = ir_build_instruction<IrInstructionLoadPtr>(irb, scope, source_node);1051 IrInstructionLoadPtr *instruction = ir_build_instruction<IrInstructionLoadPtr>(irb, scope, source_node);
1046 instruction->ptr = ptr;1052 instruction->ptr = ptr;
10471053
1048 ir_ref_instruction(ptr);1054 ir_ref_instruction(ptr, irb->current_basic_block);
10491055
1050 return &instruction->base;1056 return &instruction->base;
1051}1057}
...@@ -1061,7 +1067,7 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1061,7 +1067,7 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
1061 IrInstructionTypeOf *instruction = ir_build_instruction<IrInstructionTypeOf>(irb, scope, source_node);1067 IrInstructionTypeOf *instruction = ir_build_instruction<IrInstructionTypeOf>(irb, scope, source_node);
1062 instruction->value = value;1068 instruction->value = value;
10631069
1064 ir_ref_instruction(value);1070 ir_ref_instruction(value, irb->current_basic_block);
10651071
1066 return &instruction->base;1072 return &instruction->base;
1067}1073}
...@@ -1070,7 +1076,7 @@ static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode...@@ -1070,7 +1076,7 @@ static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode
1070 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);1076 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
1071 instruction->value = value;1077 instruction->value = value;
10721078
1073 ir_ref_instruction(value);1079 ir_ref_instruction(value, irb->current_basic_block);
10741080
1075 return &instruction->base;1081 return &instruction->base;
1076}1082}
...@@ -1082,7 +1088,7 @@ static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstN...@@ -1082,7 +1088,7 @@ static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstN
1082 irb, scope, source_node);1088 irb, scope, source_node);
1083 instruction->value = value;1089 instruction->value = value;
10841090
1085 ir_ref_instruction(value);1091 ir_ref_instruction(value, irb->current_basic_block);
10861092
1087 return &instruction->base;1093 return &instruction->base;
1088}1094}
...@@ -1093,7 +1099,7 @@ static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, Scope *scope, AstNode...@@ -1093,7 +1099,7 @@ static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, Scope *scope, AstNode
1093 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, scope, source_node);1099 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, scope, source_node);
1094 instruction->fn_value = fn_value;1100 instruction->fn_value = fn_value;
10951101
1096 ir_ref_instruction(fn_value);1102 ir_ref_instruction(fn_value, irb->current_basic_block);
10971103
1098 return &instruction->base;1104 return &instruction->base;
1099}1105}
...@@ -1105,8 +1111,8 @@ static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, Scope *scope, AstN...@@ -1105,8 +1111,8 @@ static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, Scope *scope, AstN
1105 instruction->fn_value = fn_value;1111 instruction->fn_value = fn_value;
1106 instruction->is_visible = is_visible;1112 instruction->is_visible = is_visible;
11071113
1108 ir_ref_instruction(fn_value);1114 ir_ref_instruction(fn_value, irb->current_basic_block);
1109 ir_ref_instruction(is_visible);1115 ir_ref_instruction(is_visible, irb->current_basic_block);
11101116
1111 return &instruction->base;1117 return &instruction->base;
1112}1118}
...@@ -1118,8 +1124,8 @@ static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, Scope *scope, As...@@ -1118,8 +1124,8 @@ static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, Scope *scope, As
1118 instruction->scope_value = scope_value;1124 instruction->scope_value = scope_value;
1119 instruction->debug_safety_on = debug_safety_on;1125 instruction->debug_safety_on = debug_safety_on;
11201126
1121 ir_ref_instruction(scope_value);1127 ir_ref_instruction(scope_value, irb->current_basic_block);
1122 ir_ref_instruction(debug_safety_on);1128 ir_ref_instruction(debug_safety_on, irb->current_basic_block);
11231129
1124 return &instruction->base;1130 return &instruction->base;
1125}1131}
...@@ -1131,8 +1137,8 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode...@@ -1131,8 +1137,8 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode
1131 instruction->size = size;1137 instruction->size = size;
1132 instruction->child_type = child_type;1138 instruction->child_type = child_type;
11331139
1134 ir_ref_instruction(size);1140 ir_ref_instruction(size, irb->current_basic_block);
1135 ir_ref_instruction(child_type);1141 ir_ref_instruction(child_type, irb->current_basic_block);
11361142
1137 return &instruction->base;1143 return &instruction->base;
1138}1144}
...@@ -1144,7 +1150,7 @@ static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode...@@ -1144,7 +1150,7 @@ static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode
1144 instruction->is_const = is_const;1150 instruction->is_const = is_const;
1145 instruction->child_type = child_type;1151 instruction->child_type = child_type;
11461152
1147 ir_ref_instruction(child_type);1153 ir_ref_instruction(child_type, irb->current_basic_block);
11481154
1149 return &instruction->base;1155 return &instruction->base;
1150}1156}
...@@ -1162,12 +1168,12 @@ static IrInstruction *ir_build_asm(IrBuilder *irb, Scope *scope, AstNode *source...@@ -1162,12 +1168,12 @@ static IrInstruction *ir_build_asm(IrBuilder *irb, Scope *scope, AstNode *source
1162 assert(source_node->type == NodeTypeAsmExpr);1168 assert(source_node->type == NodeTypeAsmExpr);
1163 for (size_t i = 0; i < source_node->data.asm_expr.output_list.length; i += 1) {1169 for (size_t i = 0; i < source_node->data.asm_expr.output_list.length; i += 1) {
1164 IrInstruction *output_type = output_types[i];1170 IrInstruction *output_type = output_types[i];
1165 if (output_type) ir_ref_instruction(output_type);1171 if (output_type) ir_ref_instruction(output_type, irb->current_basic_block);
1166 }1172 }
11671173
1168 for (size_t i = 0; i < source_node->data.asm_expr.input_list.length; i += 1) {1174 for (size_t i = 0; i < source_node->data.asm_expr.input_list.length; i += 1) {
1169 IrInstruction *input_value = input_list[i];1175 IrInstruction *input_value = input_list[i];
1170 ir_ref_instruction(input_value);1176 ir_ref_instruction(input_value, irb->current_basic_block);
1171 }1177 }
11721178
1173 return &instruction->base;1179 return &instruction->base;
...@@ -1186,7 +1192,7 @@ static IrInstruction *ir_build_compile_var(IrBuilder *irb, Scope *scope, AstNode...@@ -1186,7 +1192,7 @@ static IrInstruction *ir_build_compile_var(IrBuilder *irb, Scope *scope, AstNode
1186 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, scope, source_node);1192 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, scope, source_node);
1187 instruction->name = name;1193 instruction->name = name;
11881194
1189 ir_ref_instruction(name);1195 ir_ref_instruction(name, irb->current_basic_block);
11901196
1191 return &instruction->base;1197 return &instruction->base;
1192}1198}
...@@ -1195,7 +1201,7 @@ static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *so...@@ -1195,7 +1201,7 @@ static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *so
1195 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);1201 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);
1196 instruction->type_value = type_value;1202 instruction->type_value = type_value;
11971203
1198 ir_ref_instruction(type_value);1204 ir_ref_instruction(type_value, irb->current_basic_block);
11991205
1200 return &instruction->base;1206 return &instruction->base;
1201}1207}
...@@ -1204,7 +1210,7 @@ static IrInstruction *ir_build_test_null(IrBuilder *irb, Scope *scope, AstNode *...@@ -1204,7 +1210,7 @@ static IrInstruction *ir_build_test_null(IrBuilder *irb, Scope *scope, AstNode *
1204 IrInstructionTestNonNull *instruction = ir_build_instruction<IrInstructionTestNonNull>(irb, scope, source_node);1210 IrInstructionTestNonNull *instruction = ir_build_instruction<IrInstructionTestNonNull>(irb, scope, source_node);
1205 instruction->value = value;1211 instruction->value = value;
12061212
1207 ir_ref_instruction(value);1213 ir_ref_instruction(value, irb->current_basic_block);
12081214
1209 return &instruction->base;1215 return &instruction->base;
1210}1216}
...@@ -1225,7 +1231,7 @@ static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, Scope *scope, AstNod...@@ -1225,7 +1231,7 @@ static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, Scope *scope, AstNod
1225 instruction->value = value;1231 instruction->value = value;
1226 instruction->safety_check_on = safety_check_on;1232 instruction->safety_check_on = safety_check_on;
12271233
1228 ir_ref_instruction(value);1234 ir_ref_instruction(value, irb->current_basic_block);
12291235
1230 return &instruction->base;1236 return &instruction->base;
1231}1237}
...@@ -1243,7 +1249,7 @@ static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode...@@ -1243,7 +1249,7 @@ static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode
1243 IrInstructionMaybeWrap *instruction = ir_build_instruction<IrInstructionMaybeWrap>(irb, scope, source_node);1249 IrInstructionMaybeWrap *instruction = ir_build_instruction<IrInstructionMaybeWrap>(irb, scope, source_node);
1244 instruction->value = value;1250 instruction->value = value;
12451251
1246 ir_ref_instruction(value);1252 ir_ref_instruction(value, irb->current_basic_block);
12471253
1248 return &instruction->base;1254 return &instruction->base;
1249}1255}
...@@ -1252,7 +1258,7 @@ static IrInstruction *ir_build_err_wrap_payload(IrBuilder *irb, Scope *scope, As...@@ -1252,7 +1258,7 @@ static IrInstruction *ir_build_err_wrap_payload(IrBuilder *irb, Scope *scope, As
1252 IrInstructionErrWrapPayload *instruction = ir_build_instruction<IrInstructionErrWrapPayload>(irb, scope, source_node);1258 IrInstructionErrWrapPayload *instruction = ir_build_instruction<IrInstructionErrWrapPayload>(irb, scope, source_node);
1253 instruction->value = value;1259 instruction->value = value;
12541260
1255 ir_ref_instruction(value);1261 ir_ref_instruction(value, irb->current_basic_block);
12561262
1257 return &instruction->base;1263 return &instruction->base;
1258}1264}
...@@ -1261,7 +1267,7 @@ static IrInstruction *ir_build_err_wrap_code(IrBuilder *irb, Scope *scope, AstNo...@@ -1261,7 +1267,7 @@ static IrInstruction *ir_build_err_wrap_code(IrBuilder *irb, Scope *scope, AstNo
1261 IrInstructionErrWrapCode *instruction = ir_build_instruction<IrInstructionErrWrapCode>(irb, scope, source_node);1267 IrInstructionErrWrapCode *instruction = ir_build_instruction<IrInstructionErrWrapCode>(irb, scope, source_node);
1262 instruction->value = value;1268 instruction->value = value;
12631269
1264 ir_ref_instruction(value);1270 ir_ref_instruction(value, irb->current_basic_block);
12651271
1266 return &instruction->base;1272 return &instruction->base;
1267}1273}
...@@ -1270,7 +1276,7 @@ static IrInstruction *ir_build_clz(IrBuilder *irb, Scope *scope, AstNode *source...@@ -1270,7 +1276,7 @@ static IrInstruction *ir_build_clz(IrBuilder *irb, Scope *scope, AstNode *source
1270 IrInstructionClz *instruction = ir_build_instruction<IrInstructionClz>(irb, scope, source_node);1276 IrInstructionClz *instruction = ir_build_instruction<IrInstructionClz>(irb, scope, source_node);
1271 instruction->value = value;1277 instruction->value = value;
12721278
1273 ir_ref_instruction(value);1279 ir_ref_instruction(value, irb->current_basic_block);
12741280
1275 return &instruction->base;1281 return &instruction->base;
1276}1282}
...@@ -1285,7 +1291,7 @@ static IrInstruction *ir_build_ctz(IrBuilder *irb, Scope *scope, AstNode *source...@@ -1285,7 +1291,7 @@ static IrInstruction *ir_build_ctz(IrBuilder *irb, Scope *scope, AstNode *source
1285 IrInstructionCtz *instruction = ir_build_instruction<IrInstructionCtz>(irb, scope, source_node);1291 IrInstructionCtz *instruction = ir_build_instruction<IrInstructionCtz>(irb, scope, source_node);
1286 instruction->value = value;1292 instruction->value = value;
12871293
1288 ir_ref_instruction(value);1294 ir_ref_instruction(value, irb->current_basic_block);
12891295
1290 return &instruction->base;1296 return &instruction->base;
1291}1297}
...@@ -1308,12 +1314,12 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *...@@ -1308,12 +1314,12 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *
1308 instruction->cases = cases;1314 instruction->cases = cases;
1309 instruction->is_comptime = is_comptime;1315 instruction->is_comptime = is_comptime;
13101316
1311 ir_ref_instruction(target_value);1317 ir_ref_instruction(target_value, irb->current_basic_block);
1312 if (is_comptime) ir_ref_instruction(is_comptime);1318 if (is_comptime) ir_ref_instruction(is_comptime, irb->current_basic_block);
1313 ir_ref_bb(else_block);1319 ir_ref_bb(else_block);
13141320
1315 for (size_t i = 0; i < case_count; i += 1) {1321 for (size_t i = 0; i < case_count; i += 1) {
1316 ir_ref_instruction(cases[i].value);1322 ir_ref_instruction(cases[i].value, irb->current_basic_block);
1317 ir_ref_bb(cases[i].block);1323 ir_ref_bb(cases[i].block);
1318 }1324 }
13191325
...@@ -1336,7 +1342,7 @@ static IrInstruction *ir_build_switch_target(IrBuilder *irb, Scope *scope, AstNo...@@ -1336,7 +1342,7 @@ static IrInstruction *ir_build_switch_target(IrBuilder *irb, Scope *scope, AstNo
1336 IrInstructionSwitchTarget *instruction = ir_build_instruction<IrInstructionSwitchTarget>(irb, scope, source_node);1342 IrInstructionSwitchTarget *instruction = ir_build_instruction<IrInstructionSwitchTarget>(irb, scope, source_node);
1337 instruction->target_value_ptr = target_value_ptr;1343 instruction->target_value_ptr = target_value_ptr;
13381344
1339 ir_ref_instruction(target_value_ptr);1345 ir_ref_instruction(target_value_ptr, irb->current_basic_block);
13401346
1341 return &instruction->base;1347 return &instruction->base;
1342}1348}
...@@ -1348,8 +1354,8 @@ static IrInstruction *ir_build_switch_var(IrBuilder *irb, Scope *scope, AstNode...@@ -1348,8 +1354,8 @@ static IrInstruction *ir_build_switch_var(IrBuilder *irb, Scope *scope, AstNode
1348 instruction->target_value_ptr = target_value_ptr;1354 instruction->target_value_ptr = target_value_ptr;
1349 instruction->prong_value = prong_value;1355 instruction->prong_value = prong_value;
13501356
1351 ir_ref_instruction(target_value_ptr);1357 ir_ref_instruction(target_value_ptr, irb->current_basic_block);
1352 ir_ref_instruction(prong_value);1358 ir_ref_instruction(prong_value, irb->current_basic_block);
13531359
1354 return &instruction->base;1360 return &instruction->base;
1355}1361}
...@@ -1358,7 +1364,7 @@ static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1358,7 +1364,7 @@ static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *s
1358 IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, scope, source_node);1364 IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, scope, source_node);
1359 instruction->value = value;1365 instruction->value = value;
13601366
1361 ir_ref_instruction(value);1367 ir_ref_instruction(value, irb->current_basic_block);
13621368
1363 return &instruction->base;1369 return &instruction->base;
1364}1370}
...@@ -1374,7 +1380,7 @@ static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode...@@ -1374,7 +1380,7 @@ static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode
1374 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, scope, source_node);1380 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, scope, source_node);
1375 instruction->value = value;1381 instruction->value = value;
13761382
1377 ir_ref_instruction(value);1383 ir_ref_instruction(value, irb->current_basic_block);
13781384
1379 return &instruction->base;1385 return &instruction->base;
1380}1386}
...@@ -1383,7 +1389,7 @@ static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1383,7 +1389,7 @@ static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *sou
1383 IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node);1389 IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node);
1384 instruction->name = name;1390 instruction->name = name;
13851391
1386 ir_ref_instruction(name);1392 ir_ref_instruction(name, irb->current_basic_block);
13871393
1388 return &instruction->base;1394 return &instruction->base;
1389}1395}
...@@ -1392,7 +1398,7 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *...@@ -1392,7 +1398,7 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *
1392 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, scope, source_node);1398 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, scope, source_node);
1393 instruction->array_value = array_value;1399 instruction->array_value = array_value;
13941400
1395 ir_ref_instruction(array_value);1401 ir_ref_instruction(array_value, irb->current_basic_block);
13961402
1397 return &instruction->base;1403 return &instruction->base;
1398}1404}
...@@ -1404,7 +1410,7 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source...@@ -1404,7 +1410,7 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source
1404 instruction->value = value;1410 instruction->value = value;
1405 instruction->is_const = is_const;1411 instruction->is_const = is_const;
14061412
1407 ir_ref_instruction(value);1413 ir_ref_instruction(value, irb->current_basic_block);
14081414
1409 return &instruction->base;1415 return &instruction->base;
1410}1416}
...@@ -1422,7 +1428,7 @@ static IrInstruction *ir_build_min_value(IrBuilder *irb, Scope *scope, AstNode *...@@ -1422,7 +1428,7 @@ static IrInstruction *ir_build_min_value(IrBuilder *irb, Scope *scope, AstNode *
1422 IrInstructionMinValue *instruction = ir_build_instruction<IrInstructionMinValue>(irb, scope, source_node);1428 IrInstructionMinValue *instruction = ir_build_instruction<IrInstructionMinValue>(irb, scope, source_node);
1423 instruction->value = value;1429 instruction->value = value;
14241430
1425 ir_ref_instruction(value);1431 ir_ref_instruction(value, irb->current_basic_block);
14261432
1427 return &instruction->base;1433 return &instruction->base;
1428}1434}
...@@ -1431,7 +1437,7 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *...@@ -1431,7 +1437,7 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *
1431 IrInstructionMaxValue *instruction = ir_build_instruction<IrInstructionMaxValue>(irb, scope, source_node);1437 IrInstructionMaxValue *instruction = ir_build_instruction<IrInstructionMaxValue>(irb, scope, source_node);
1432 instruction->value = value;1438 instruction->value = value;
14331439
1434 ir_ref_instruction(value);1440 ir_ref_instruction(value, irb->current_basic_block);
14351441
1436 return &instruction->base;1442 return &instruction->base;
1437}1443}
...@@ -1440,7 +1446,7 @@ static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode...@@ -1440,7 +1446,7 @@ static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode
1440 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);1446 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
1441 instruction->msg = msg;1447 instruction->msg = msg;
14421448
1443 ir_ref_instruction(msg);1449 ir_ref_instruction(msg, irb->current_basic_block);
14441450
1445 return &instruction->base;1451 return &instruction->base;
1446}1452}
...@@ -1449,7 +1455,7 @@ static IrInstruction *ir_build_err_name(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1449,7 +1455,7 @@ static IrInstruction *ir_build_err_name(IrBuilder *irb, Scope *scope, AstNode *s
1449 IrInstructionErrName *instruction = ir_build_instruction<IrInstructionErrName>(irb, scope, source_node);1455 IrInstructionErrName *instruction = ir_build_instruction<IrInstructionErrName>(irb, scope, source_node);
1450 instruction->value = value;1456 instruction->value = value;
14511457
1452 ir_ref_instruction(value);1458 ir_ref_instruction(value, irb->current_basic_block);
14531459
1454 return &instruction->base;1460 return &instruction->base;
1455}1461}
...@@ -1470,7 +1476,7 @@ static IrInstruction *ir_build_c_include(IrBuilder *irb, Scope *scope, AstNode *...@@ -1470,7 +1476,7 @@ static IrInstruction *ir_build_c_include(IrBuilder *irb, Scope *scope, AstNode *
1470 IrInstructionCInclude *instruction = ir_build_instruction<IrInstructionCInclude>(irb, scope, source_node);1476 IrInstructionCInclude *instruction = ir_build_instruction<IrInstructionCInclude>(irb, scope, source_node);
1471 instruction->name = name;1477 instruction->name = name;
14721478
1473 ir_ref_instruction(name);1479 ir_ref_instruction(name, irb->current_basic_block);
14741480
1475 return &instruction->base;1481 return &instruction->base;
1476}1482}
...@@ -1480,8 +1486,8 @@ static IrInstruction *ir_build_c_define(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1480,8 +1486,8 @@ static IrInstruction *ir_build_c_define(IrBuilder *irb, Scope *scope, AstNode *s
1480 instruction->name = name;1486 instruction->name = name;
1481 instruction->value = value;1487 instruction->value = value;
14821488
1483 ir_ref_instruction(name);1489 ir_ref_instruction(name, irb->current_basic_block);
1484 ir_ref_instruction(value);1490 ir_ref_instruction(value, irb->current_basic_block);
14851491
1486 return &instruction->base;1492 return &instruction->base;
1487}1493}
...@@ -1490,7 +1496,7 @@ static IrInstruction *ir_build_c_undef(IrBuilder *irb, Scope *scope, AstNode *so...@@ -1490,7 +1496,7 @@ static IrInstruction *ir_build_c_undef(IrBuilder *irb, Scope *scope, AstNode *so
1490 IrInstructionCUndef *instruction = ir_build_instruction<IrInstructionCUndef>(irb, scope, source_node);1496 IrInstructionCUndef *instruction = ir_build_instruction<IrInstructionCUndef>(irb, scope, source_node);
1491 instruction->name = name;1497 instruction->name = name;
14921498
1493 ir_ref_instruction(name);1499 ir_ref_instruction(name, irb->current_basic_block);
14941500
1495 return &instruction->base;1501 return &instruction->base;
1496}1502}
...@@ -1499,7 +1505,7 @@ static IrInstruction *ir_build_embed_file(IrBuilder *irb, Scope *scope, AstNode...@@ -1499,7 +1505,7 @@ static IrInstruction *ir_build_embed_file(IrBuilder *irb, Scope *scope, AstNode
1499 IrInstructionEmbedFile *instruction = ir_build_instruction<IrInstructionEmbedFile>(irb, scope, source_node);1505 IrInstructionEmbedFile *instruction = ir_build_instruction<IrInstructionEmbedFile>(irb, scope, source_node);
1500 instruction->name = name;1506 instruction->name = name;
15011507
1502 ir_ref_instruction(name);1508 ir_ref_instruction(name, irb->current_basic_block);
15031509
1504 return &instruction->base;1510 return &instruction->base;
1505}1511}
...@@ -1517,11 +1523,11 @@ static IrInstruction *ir_build_cmpxchg(IrBuilder *irb, Scope *scope, AstNode *so...@@ -1517,11 +1523,11 @@ static IrInstruction *ir_build_cmpxchg(IrBuilder *irb, Scope *scope, AstNode *so
1517 instruction->success_order = success_order;1523 instruction->success_order = success_order;
1518 instruction->failure_order = failure_order;1524 instruction->failure_order = failure_order;
15191525
1520 ir_ref_instruction(ptr);1526 ir_ref_instruction(ptr, irb->current_basic_block);
1521 ir_ref_instruction(cmp_value);1527 ir_ref_instruction(cmp_value, irb->current_basic_block);
1522 ir_ref_instruction(new_value);1528 ir_ref_instruction(new_value, irb->current_basic_block);
1523 ir_ref_instruction(success_order_value);1529 ir_ref_instruction(success_order_value, irb->current_basic_block);
1524 ir_ref_instruction(failure_order_value);1530 ir_ref_instruction(failure_order_value, irb->current_basic_block);
15251531
1526 return &instruction->base;1532 return &instruction->base;
1527}1533}
...@@ -1541,7 +1547,7 @@ static IrInstruction *ir_build_fence(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -1541,7 +1547,7 @@ static IrInstruction *ir_build_fence(IrBuilder *irb, Scope *scope, AstNode *sour
1541 instruction->order_value = order_value;1547 instruction->order_value = order_value;
1542 instruction->order = order;1548 instruction->order = order;
15431549
1544 ir_ref_instruction(order_value);1550 ir_ref_instruction(order_value, irb->current_basic_block);
15451551
1546 return &instruction->base;1552 return &instruction->base;
1547}1553}
...@@ -1557,8 +1563,8 @@ static IrInstruction *ir_build_div_exact(IrBuilder *irb, Scope *scope, AstNode *...@@ -1557,8 +1563,8 @@ static IrInstruction *ir_build_div_exact(IrBuilder *irb, Scope *scope, AstNode *
1557 instruction->op1 = op1;1563 instruction->op1 = op1;
1558 instruction->op2 = op2;1564 instruction->op2 = op2;
15591565
1560 ir_ref_instruction(op1);1566 ir_ref_instruction(op1, irb->current_basic_block);
1561 ir_ref_instruction(op2);1567 ir_ref_instruction(op2, irb->current_basic_block);
15621568
1563 return &instruction->base;1569 return &instruction->base;
1564}1570}
...@@ -1574,8 +1580,8 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1574,8 +1580,8 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s
1574 instruction->dest_type = dest_type;1580 instruction->dest_type = dest_type;
1575 instruction->target = target;1581 instruction->target = target;
15761582
1577 ir_ref_instruction(dest_type);1583 ir_ref_instruction(dest_type, irb->current_basic_block);
1578 ir_ref_instruction(target);1584 ir_ref_instruction(target, irb->current_basic_block);
15791585
1580 return &instruction->base;1586 return &instruction->base;
1581}1587}
...@@ -1591,8 +1597,8 @@ static IrInstruction *ir_build_int_type(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1591,8 +1597,8 @@ static IrInstruction *ir_build_int_type(IrBuilder *irb, Scope *scope, AstNode *s
1591 instruction->is_signed = is_signed;1597 instruction->is_signed = is_signed;
1592 instruction->bit_count = bit_count;1598 instruction->bit_count = bit_count;
15931599
1594 ir_ref_instruction(is_signed);1600 ir_ref_instruction(is_signed, irb->current_basic_block);
1595 ir_ref_instruction(bit_count);1601 ir_ref_instruction(bit_count, irb->current_basic_block);
15961602
1597 return &instruction->base;1603 return &instruction->base;
1598}1604}
...@@ -1601,7 +1607,7 @@ static IrInstruction *ir_build_bool_not(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1601,7 +1607,7 @@ static IrInstruction *ir_build_bool_not(IrBuilder *irb, Scope *scope, AstNode *s
1601 IrInstructionBoolNot *instruction = ir_build_instruction<IrInstructionBoolNot>(irb, scope, source_node);1607 IrInstructionBoolNot *instruction = ir_build_instruction<IrInstructionBoolNot>(irb, scope, source_node);
1602 instruction->value = value;1608 instruction->value = value;
16031609
1604 ir_ref_instruction(value);1610 ir_ref_instruction(value, irb->current_basic_block);
16051611
1606 return &instruction->base;1612 return &instruction->base;
1607}1613}
...@@ -1619,8 +1625,8 @@ static IrInstruction *ir_build_alloca(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1619,8 +1625,8 @@ static IrInstruction *ir_build_alloca(IrBuilder *irb, Scope *scope, AstNode *sou
1619 instruction->type_value = type_value;1625 instruction->type_value = type_value;
1620 instruction->count = count;1626 instruction->count = count;
16211627
1622 ir_ref_instruction(type_value);1628 ir_ref_instruction(type_value, irb->current_basic_block);
1623 ir_ref_instruction(count);1629 ir_ref_instruction(count, irb->current_basic_block);
16241630
1625 return &instruction->base;1631 return &instruction->base;
1626}1632}
...@@ -1641,9 +1647,9 @@ static IrInstruction *ir_build_memset(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1641,9 +1647,9 @@ static IrInstruction *ir_build_memset(IrBuilder *irb, Scope *scope, AstNode *sou
1641 instruction->byte = byte;1647 instruction->byte = byte;
1642 instruction->count = count;1648 instruction->count = count;
16431649
1644 ir_ref_instruction(dest_ptr);1650 ir_ref_instruction(dest_ptr, irb->current_basic_block);
1645 ir_ref_instruction(byte);1651 ir_ref_instruction(byte, irb->current_basic_block);
1646 ir_ref_instruction(count);1652 ir_ref_instruction(count, irb->current_basic_block);
16471653
1648 return &instruction->base;1654 return &instruction->base;
1649}1655}
...@@ -1664,9 +1670,9 @@ static IrInstruction *ir_build_memcpy(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1664,9 +1670,9 @@ static IrInstruction *ir_build_memcpy(IrBuilder *irb, Scope *scope, AstNode *sou
1664 instruction->src_ptr = src_ptr;1670 instruction->src_ptr = src_ptr;
1665 instruction->count = count;1671 instruction->count = count;
16661672
1667 ir_ref_instruction(dest_ptr);1673 ir_ref_instruction(dest_ptr, irb->current_basic_block);
1668 ir_ref_instruction(src_ptr);1674 ir_ref_instruction(src_ptr, irb->current_basic_block);
1669 ir_ref_instruction(count);1675 ir_ref_instruction(count, irb->current_basic_block);
16701676
1671 return &instruction->base;1677 return &instruction->base;
1672}1678}
...@@ -1689,9 +1695,9 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -1689,9 +1695,9 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour
1689 instruction->is_const = is_const;1695 instruction->is_const = is_const;
1690 instruction->safety_check_on = safety_check_on;1696 instruction->safety_check_on = safety_check_on;
16911697
1692 ir_ref_instruction(ptr);1698 ir_ref_instruction(ptr, irb->current_basic_block);
1693 ir_ref_instruction(start);1699 ir_ref_instruction(start, irb->current_basic_block);
1694 if (end) ir_ref_instruction(end);1700 if (end) ir_ref_instruction(end, irb->current_basic_block);
16951701
1696 return &instruction->base;1702 return &instruction->base;
1697}1703}
...@@ -1709,7 +1715,7 @@ static IrInstruction *ir_build_member_count(IrBuilder *irb, Scope *scope, AstNod...@@ -1709,7 +1715,7 @@ static IrInstruction *ir_build_member_count(IrBuilder *irb, Scope *scope, AstNod
1709 IrInstructionMemberCount *instruction = ir_build_instruction<IrInstructionMemberCount>(irb, scope, source_node);1715 IrInstructionMemberCount *instruction = ir_build_instruction<IrInstructionMemberCount>(irb, scope, source_node);
1710 instruction->container = container;1716 instruction->container = container;
17111717
1712 ir_ref_instruction(container);1718 ir_ref_instruction(container, irb->current_basic_block);
17131719
1714 return &instruction->base;1720 return &instruction->base;
1715}1721}
...@@ -1759,10 +1765,10 @@ static IrInstruction *ir_build_overflow_op(IrBuilder *irb, Scope *scope, AstNode...@@ -1759,10 +1765,10 @@ static IrInstruction *ir_build_overflow_op(IrBuilder *irb, Scope *scope, AstNode
1759 instruction->result_ptr = result_ptr;1765 instruction->result_ptr = result_ptr;
1760 instruction->result_ptr_type = result_ptr_type;1766 instruction->result_ptr_type = result_ptr_type;
17611767
1762 ir_ref_instruction(type_value);1768 ir_ref_instruction(type_value, irb->current_basic_block);
1763 ir_ref_instruction(op1);1769 ir_ref_instruction(op1, irb->current_basic_block);
1764 ir_ref_instruction(op2);1770 ir_ref_instruction(op2, irb->current_basic_block);
1765 ir_ref_instruction(result_ptr);1771 ir_ref_instruction(result_ptr, irb->current_basic_block);
17661772
1767 return &instruction->base;1773 return &instruction->base;
1768}1774}
...@@ -1781,7 +1787,7 @@ static IrInstruction *ir_build_alignof(IrBuilder *irb, Scope *scope, AstNode *so...@@ -1781,7 +1787,7 @@ static IrInstruction *ir_build_alignof(IrBuilder *irb, Scope *scope, AstNode *so
1781 IrInstructionAlignOf *instruction = ir_build_instruction<IrInstructionAlignOf>(irb, scope, source_node);1787 IrInstructionAlignOf *instruction = ir_build_instruction<IrInstructionAlignOf>(irb, scope, source_node);
1782 instruction->type_value = type_value;1788 instruction->type_value = type_value;
17831789
1784 ir_ref_instruction(type_value);1790 ir_ref_instruction(type_value, irb->current_basic_block);
17851791
1786 return &instruction->base;1792 return &instruction->base;
1787}1793}
...@@ -1792,7 +1798,7 @@ static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1792,7 +1798,7 @@ static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *s
1792 IrInstructionTestErr *instruction = ir_build_instruction<IrInstructionTestErr>(irb, scope, source_node);1798 IrInstructionTestErr *instruction = ir_build_instruction<IrInstructionTestErr>(irb, scope, source_node);
1793 instruction->value = value;1799 instruction->value = value;
17941800
1795 ir_ref_instruction(value);1801 ir_ref_instruction(value, irb->current_basic_block);
17961802
1797 return &instruction->base;1803 return &instruction->base;
1798}1804}
...@@ -1810,7 +1816,7 @@ static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, Ast...@@ -1810,7 +1816,7 @@ static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, Ast
1810 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);1816 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);
1811 instruction->value = value;1817 instruction->value = value;
18121818
1813 ir_ref_instruction(value);1819 ir_ref_instruction(value, irb->current_basic_block);
18141820
1815 return &instruction->base;1821 return &instruction->base;
1816}1822}
...@@ -1831,7 +1837,7 @@ static IrInstruction *ir_build_unwrap_err_payload(IrBuilder *irb, Scope *scope,...@@ -1831,7 +1837,7 @@ static IrInstruction *ir_build_unwrap_err_payload(IrBuilder *irb, Scope *scope,
1831 instruction->value = value;1837 instruction->value = value;
1832 instruction->safety_check_on = safety_check_on;1838 instruction->safety_check_on = safety_check_on;
18331839
1834 ir_ref_instruction(value);1840 ir_ref_instruction(value, irb->current_basic_block);
18351841
1836 return &instruction->base;1842 return &instruction->base;
1837}1843}
...@@ -1854,9 +1860,9 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1854,9 +1860,9 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s
18541860
1855 assert(source_node->type == NodeTypeFnProto);1861 assert(source_node->type == NodeTypeFnProto);
1856 for (size_t i = 0; i < source_node->data.fn_proto.params.length; i += 1) {1862 for (size_t i = 0; i < source_node->data.fn_proto.params.length; i += 1) {
1857 ir_ref_instruction(param_types[i]);1863 ir_ref_instruction(param_types[i], irb->current_basic_block);
1858 }1864 }
1859 ir_ref_instruction(return_type);1865 ir_ref_instruction(return_type, irb->current_basic_block);
18601866
1861 return &instruction->base;1867 return &instruction->base;
1862}1868}
...@@ -1865,7 +1871,7 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo...@@ -1865,7 +1871,7 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo
1865 IrInstructionTestComptime *instruction = ir_build_instruction<IrInstructionTestComptime>(irb, scope, source_node);1871 IrInstructionTestComptime *instruction = ir_build_instruction<IrInstructionTestComptime>(irb, scope, source_node);
1866 instruction->value = value;1872 instruction->value = value;
18671873
1868 ir_ref_instruction(value);1874 ir_ref_instruction(value, irb->current_basic_block);
18691875
1870 return &instruction->base;1876 return &instruction->base;
1871}1877}
...@@ -1878,7 +1884,7 @@ static IrInstruction *ir_build_init_enum(IrBuilder *irb, Scope *scope, AstNode *...@@ -1878,7 +1884,7 @@ static IrInstruction *ir_build_init_enum(IrBuilder *irb, Scope *scope, AstNode *
1878 instruction->field = field;1884 instruction->field = field;
1879 instruction->init_value = init_value;1885 instruction->init_value = init_value;
18801886
1881 ir_ref_instruction(init_value);1887 ir_ref_instruction(init_value, irb->current_basic_block);
18821888
1883 return &instruction->base;1889 return &instruction->base;
1884}1890}
...@@ -1899,7 +1905,7 @@ static IrInstruction *ir_build_pointer_reinterpret(IrBuilder *irb, Scope *scope,...@@ -1899,7 +1905,7 @@ static IrInstruction *ir_build_pointer_reinterpret(IrBuilder *irb, Scope *scope,
1899 irb, scope, source_node);1905 irb, scope, source_node);
1900 instruction->ptr = ptr;1906 instruction->ptr = ptr;
19011907
1902 ir_ref_instruction(ptr);1908 ir_ref_instruction(ptr, irb->current_basic_block);
19031909
1904 return &instruction->base;1910 return &instruction->base;
1905}1911}
...@@ -1911,7 +1917,7 @@ static IrInstruction *ir_build_widen_or_shorten(IrBuilder *irb, Scope *scope, As...@@ -1911,7 +1917,7 @@ static IrInstruction *ir_build_widen_or_shorten(IrBuilder *irb, Scope *scope, As
1911 irb, scope, source_node);1917 irb, scope, source_node);
1912 instruction->target = target;1918 instruction->target = target;
19131919
1914 ir_ref_instruction(target);1920 ir_ref_instruction(target, irb->current_basic_block);
19151921
1916 return &instruction->base;1922 return &instruction->base;
1917}1923}
...@@ -1923,7 +1929,7 @@ static IrInstruction *ir_build_int_to_ptr(IrBuilder *irb, Scope *scope, AstNode...@@ -1923,7 +1929,7 @@ static IrInstruction *ir_build_int_to_ptr(IrBuilder *irb, Scope *scope, AstNode
1923 irb, scope, source_node);1929 irb, scope, source_node);
1924 instruction->target = target;1930 instruction->target = target;
19251931
1926 ir_ref_instruction(target);1932 ir_ref_instruction(target, irb->current_basic_block);
19271933
1928 return &instruction->base;1934 return &instruction->base;
1929}1935}
...@@ -1935,7 +1941,7 @@ static IrInstruction *ir_build_ptr_to_int(IrBuilder *irb, Scope *scope, AstNode...@@ -1935,7 +1941,7 @@ static IrInstruction *ir_build_ptr_to_int(IrBuilder *irb, Scope *scope, AstNode
1935 irb, scope, source_node);1941 irb, scope, source_node);
1936 instruction->target = target;1942 instruction->target = target;
19371943
1938 ir_ref_instruction(target);1944 ir_ref_instruction(target, irb->current_basic_block);
19391945
1940 return &instruction->base;1946 return &instruction->base;
1941}1947}
...@@ -1947,7 +1953,7 @@ static IrInstruction *ir_build_int_to_enum(IrBuilder *irb, Scope *scope, AstNode...@@ -1947,7 +1953,7 @@ static IrInstruction *ir_build_int_to_enum(IrBuilder *irb, Scope *scope, AstNode
1947 irb, scope, source_node);1953 irb, scope, source_node);
1948 instruction->target = target;1954 instruction->target = target;
19491955
1950 ir_ref_instruction(target);1956 ir_ref_instruction(target, irb->current_basic_block);
19511957
1952 return &instruction->base;1958 return &instruction->base;
1953}1959}
...@@ -2736,6 +2742,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco...@@ -2736,6 +2742,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
2736 }2742 }
2737}2743}
27382744
2745static IrInstruction *ir_mark_gen(IrInstruction *instruction) {
2746 instruction->is_gen = true;
2747 return instruction;
2748}
2749
2739static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,2750static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
2740 bool gen_error_defers, bool gen_maybe_defers)2751 bool gen_error_defers, bool gen_maybe_defers)
2741{2752{
...@@ -2986,6 +2997,29 @@ static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *s...@@ -2986,6 +2997,29 @@ static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *s
2986 return var;2997 return var;
2987}2998}
29882999
3000static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name) {
3001 while (scope) {
3002 if (scope->id == ScopeIdBlock) {
3003 ScopeBlock *block_scope = (ScopeBlock *)scope;
3004 auto entry = block_scope->label_table.maybe_get(name);
3005 if (entry)
3006 return entry->value;
3007 }
3008 scope = scope->parent;
3009 }
3010
3011 return nullptr;
3012}
3013
3014static ScopeBlock *find_block_scope(IrExecutable *exec, Scope *scope) {
3015 while (scope) {
3016 if (scope->id == ScopeIdBlock)
3017 return (ScopeBlock *)scope;
3018 scope = scope->parent;
3019 }
3020 return nullptr;
3021}
3022
2989static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) {3023static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) {
2990 assert(block_node->type == NodeTypeBlock);3024 assert(block_node->type == NodeTypeBlock);
29913025
...@@ -3001,6 +3035,43 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3001,6 +3035,43 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3001 IrInstruction *return_value = nullptr;3035 IrInstruction *return_value = nullptr;
3002 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {3036 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
3003 AstNode *statement_node = block_node->data.block.statements.at(i);3037 AstNode *statement_node = block_node->data.block.statements.at(i);
3038
3039 if (statement_node->type == NodeTypeLabel) {
3040 Buf *label_name = statement_node->data.label.name;
3041 IrBasicBlock *label_block = ir_build_basic_block(irb, child_scope, buf_ptr(label_name));
3042 LabelTableEntry *label = allocate<LabelTableEntry>(1);
3043 label->decl_node = statement_node;
3044 label->bb = label_block;
3045 irb->exec->all_labels.append(label);
3046
3047 LabelTableEntry *existing_label = find_label(irb->exec, child_scope, label_name);
3048 if (existing_label) {
3049 ErrorMsg *msg = add_node_error(irb->codegen, statement_node,
3050 buf_sprintf("duplicate label name '%s'", buf_ptr(label_name)));
3051 add_error_note(irb->codegen, msg, existing_label->decl_node, buf_sprintf("other label here"));
3052 return irb->codegen->invalid_instruction;
3053 } else {
3054 ScopeBlock *scope_block = find_block_scope(irb->exec, child_scope);
3055 scope_block->label_table.put(label_name, label);
3056 }
3057
3058 if (!return_value || !instr_is_unreachable(return_value)) {
3059 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,
3060 ir_should_inline(irb)));
3061 ir_mark_gen(ir_build_br(irb, child_scope, statement_node, label_block, is_comptime));
3062 }
3063 ir_set_cursor_at_end(irb, label_block);
3064
3065 return_value = nullptr;
3066 continue;
3067 }
3068
3069 if (return_value && instr_is_unreachable(return_value)) {
3070 if (is_node_void_expr(statement_node))
3071 continue;
3072 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
3073 }
3074
3004 return_value = ir_gen_node(irb, statement_node, child_scope);3075 return_value = ir_gen_node(irb, statement_node, child_scope);
3005 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {3076 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
3006 // defer starts a new scope3077 // defer starts a new scope
...@@ -3014,9 +3085,10 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3014,9 +3085,10 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3014 }3085 }
30153086
3016 if (!return_value)3087 if (!return_value)
3017 return_value = ir_build_const_void(irb, child_scope, block_node);3088 return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
30183089
3019 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);3090 if (!instr_is_unreachable(return_value))
3091 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
30203092
3021 return return_value;3093 return return_value;
3022}3094}
...@@ -3167,7 +3239,8 @@ static IrInstruction *ir_gen_maybe_ok_or(IrBuilder *irb, Scope *parent_scope, As...@@ -3167,7 +3239,8 @@ static IrInstruction *ir_gen_maybe_ok_or(IrBuilder *irb, Scope *parent_scope, As
3167 if (null_result == irb->codegen->invalid_instruction)3239 if (null_result == irb->codegen->invalid_instruction)
3168 return irb->codegen->invalid_instruction;3240 return irb->codegen->invalid_instruction;
3169 IrBasicBlock *after_null_block = irb->current_basic_block;3241 IrBasicBlock *after_null_block = irb->current_basic_block;
3170 ir_build_br(irb, parent_scope, node, end_block, is_comptime);3242 if (!instr_is_unreachable(null_result))
3243 ir_mark_gen(ir_build_br(irb, parent_scope, node, end_block, is_comptime));
31713244
3172 ir_set_cursor_at_end(irb, ok_block);3245 ir_set_cursor_at_end(irb, ok_block);
3173 IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, parent_scope, node, maybe_ptr, false);3246 IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, parent_scope, node, maybe_ptr, false);
...@@ -3886,7 +3959,7 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node...@@ -3886,7 +3959,7 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
3886 }3959 }
38873960
3888 bool is_comptime = node->data.fn_call_expr.is_comptime;3961 bool is_comptime = node->data.fn_call_expr.is_comptime;
3889 return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, is_comptime);3962 return ir_mark_gen(ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, is_comptime));
3890}3963}
38913964
3892static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {3965static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
...@@ -3917,7 +3990,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode...@@ -3917,7 +3990,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
3917 if (then_expr_result == irb->codegen->invalid_instruction)3990 if (then_expr_result == irb->codegen->invalid_instruction)
3918 return then_expr_result;3991 return then_expr_result;
3919 IrBasicBlock *after_then_block = irb->current_basic_block;3992 IrBasicBlock *after_then_block = irb->current_basic_block;
3920 ir_build_br(irb, scope, node, endif_block, is_comptime);3993 if (!instr_is_unreachable(then_expr_result))
3994 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
39213995
3922 ir_set_cursor_at_end(irb, else_block);3996 ir_set_cursor_at_end(irb, else_block);
3923 IrInstruction *else_expr_result;3997 IrInstruction *else_expr_result;
...@@ -3929,7 +4003,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode...@@ -3929,7 +4003,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
3929 else_expr_result = ir_build_const_void(irb, scope, node);4003 else_expr_result = ir_build_const_void(irb, scope, node);
3930 }4004 }
3931 IrBasicBlock *after_else_block = irb->current_basic_block;4005 IrBasicBlock *after_else_block = irb->current_basic_block;
3932 ir_build_br(irb, scope, node, endif_block, is_comptime);4006 if (!instr_is_unreachable(else_expr_result))
4007 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
39334008
3934 ir_set_cursor_at_end(irb, endif_block);4009 ir_set_cursor_at_end(irb, endif_block);
3935 IrInstruction **incoming_values = allocate<IrInstruction *>(2);4010 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
...@@ -4158,13 +4233,17 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -4158,13 +4233,17 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
41584233
4159 if (continue_expr_node) {4234 if (continue_expr_node) {
4160 ir_set_cursor_at_end(irb, continue_block);4235 ir_set_cursor_at_end(irb, continue_block);
4161 ir_gen_node(irb, continue_expr_node, scope);4236 IrInstruction *expr_result = ir_gen_node(irb, continue_expr_node, scope);
4162 ir_build_br(irb, scope, node, cond_block, is_comptime);4237 if (!instr_is_unreachable(expr_result))
4238 ir_mark_gen(ir_build_br(irb, scope, node, cond_block, is_comptime));
4163 }4239 }
41644240
4165 ir_set_cursor_at_end(irb, cond_block);4241 ir_set_cursor_at_end(irb, cond_block);
4166 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, scope);4242 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, scope);
4167 ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val, body_block, end_block, is_comptime);4243 if (!instr_is_unreachable(cond_val)) {
4244 ir_mark_gen(ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val,
4245 body_block, end_block, is_comptime));
4246 }
41684247
4169 ir_set_cursor_at_end(irb, body_block);4248 ir_set_cursor_at_end(irb, body_block);
41704249
...@@ -4172,10 +4251,11 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -4172,10 +4251,11 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
4172 loop_stack_item->break_block = end_block;4251 loop_stack_item->break_block = end_block;
4173 loop_stack_item->continue_block = continue_block;4252 loop_stack_item->continue_block = continue_block;
4174 loop_stack_item->is_comptime = is_comptime;4253 loop_stack_item->is_comptime = is_comptime;
4175 ir_gen_node(irb, node->data.while_expr.body, scope);4254 IrInstruction *body_result = ir_gen_node(irb, node->data.while_expr.body, scope);
4176 irb->loop_stack.pop();4255 irb->loop_stack.pop();
41774256
4178 ir_build_br(irb, scope, node, continue_block, is_comptime);4257 if (!instr_is_unreachable(body_result))
4258 ir_mark_gen(ir_build_br(irb, scope, node, continue_block, is_comptime));
4179 ir_set_cursor_at_end(irb, end_block);4259 ir_set_cursor_at_end(irb, end_block);
41804260
4181 return ir_build_const_void(irb, scope, node);4261 return ir_build_const_void(irb, scope, node);
...@@ -4270,10 +4350,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -4270,10 +4350,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
4270 loop_stack_item->break_block = end_block;4350 loop_stack_item->break_block = end_block;
4271 loop_stack_item->continue_block = continue_block;4351 loop_stack_item->continue_block = continue_block;
4272 loop_stack_item->is_comptime = is_comptime;4352 loop_stack_item->is_comptime = is_comptime;
4273 ir_gen_node(irb, body_node, child_scope);4353 IrInstruction *body_result = ir_gen_node(irb, body_node, child_scope);
4274 irb->loop_stack.pop();4354 irb->loop_stack.pop();
42754355
4276 ir_build_br(irb, child_scope, node, continue_block, is_comptime);4356 if (!instr_is_unreachable(body_result))
4357 ir_mark_gen(ir_build_br(irb, child_scope, node, continue_block, is_comptime));
42774358
4278 ir_set_cursor_at_end(irb, continue_block);4359 ir_set_cursor_at_end(irb, continue_block);
4279 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);4360 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
...@@ -4457,7 +4538,8 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -4457,7 +4538,8 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *
4457 if (then_expr_result == irb->codegen->invalid_instruction)4538 if (then_expr_result == irb->codegen->invalid_instruction)
4458 return then_expr_result;4539 return then_expr_result;
4459 IrBasicBlock *after_then_block = irb->current_basic_block;4540 IrBasicBlock *after_then_block = irb->current_basic_block;
4460 ir_build_br(irb, scope, node, endif_block, is_comptime);4541 if (!instr_is_unreachable(then_expr_result))
4542 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
44614543
4462 ir_set_cursor_at_end(irb, else_block);4544 ir_set_cursor_at_end(irb, else_block);
4463 IrInstruction *else_expr_result;4545 IrInstruction *else_expr_result;
...@@ -4469,7 +4551,8 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -4469,7 +4551,8 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *
4469 else_expr_result = ir_build_const_void(irb, scope, node);4551 else_expr_result = ir_build_const_void(irb, scope, node);
4470 }4552 }
4471 IrBasicBlock *after_else_block = irb->current_basic_block;4553 IrBasicBlock *after_else_block = irb->current_basic_block;
4472 ir_build_br(irb, scope, node, endif_block, is_comptime);4554 if (!instr_is_unreachable(else_expr_result))
4555 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
44734556
4474 ir_set_cursor_at_end(irb, endif_block);4557 ir_set_cursor_at_end(irb, endif_block);
4475 IrInstruction **incoming_values = allocate<IrInstruction *>(2);4558 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
...@@ -4518,7 +4601,8 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit...@@ -4518,7 +4601,8 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit
4518 IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope);4601 IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope);
4519 if (expr_result == irb->codegen->invalid_instruction)4602 if (expr_result == irb->codegen->invalid_instruction)
4520 return false;4603 return false;
4521 ir_build_br(irb, scope, switch_node, end_block, is_comptime);4604 if (!instr_is_unreachable(expr_result))
4605 ir_mark_gen(ir_build_br(irb, scope, switch_node, end_block, is_comptime));
4522 incoming_blocks->append(irb->current_basic_block);4606 incoming_blocks->append(irb->current_basic_block);
4523 incoming_values->append(expr_result);4607 incoming_values->append(expr_result);
4524 return true;4608 return true;
...@@ -4684,57 +4768,6 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -4684,57 +4768,6 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
4684 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);4768 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
4685}4769}
46864770
4687static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name) {
4688 while (scope) {
4689 if (scope->id == ScopeIdBlock) {
4690 ScopeBlock *block_scope = (ScopeBlock *)scope;
4691 auto entry = block_scope->label_table.maybe_get(name);
4692 if (entry)
4693 return entry->value;
4694 }
4695 scope = scope->parent;
4696 }
4697
4698 return nullptr;
4699}
4700
4701static ScopeBlock *find_block_scope(IrExecutable *exec, Scope *scope) {
4702 while (scope) {
4703 if (scope->id == ScopeIdBlock)
4704 return (ScopeBlock *)scope;
4705 scope = scope->parent;
4706 }
4707 return nullptr;
4708}
4709
4710static IrInstruction *ir_gen_label(IrBuilder *irb, Scope *scope, AstNode *node) {
4711 assert(node->type == NodeTypeLabel);
4712
4713 Buf *label_name = node->data.label.name;
4714 IrBasicBlock *label_block = ir_build_basic_block(irb, scope, buf_ptr(label_name));
4715 LabelTableEntry *label = allocate<LabelTableEntry>(1);
4716 label->decl_node = node;
4717 label->bb = label_block;
4718 irb->exec->all_labels.append(label);
4719
4720 LabelTableEntry *existing_label = find_label(irb->exec, scope, label_name);
4721 if (existing_label) {
4722 ErrorMsg *msg = add_node_error(irb->codegen, node,
4723 buf_sprintf("duplicate label name '%s'", buf_ptr(label_name)));
4724 add_error_note(irb->codegen, msg, existing_label->decl_node, buf_sprintf("other label here"));
4725 return irb->codegen->invalid_instruction;
4726 } else {
4727 ScopeBlock *scope_block = find_block_scope(irb->exec, scope);
4728 scope_block->label_table.put(label_name, label);
4729 }
4730
4731 IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node,
4732 ir_should_inline(irb));
4733 ir_build_br(irb, scope, node, label_block, is_comptime);
4734 ir_set_cursor_at_end(irb, label_block);
4735 return ir_build_const_void(irb, scope, node);
4736}
4737
4738static IrInstruction *ir_gen_goto(IrBuilder *irb, Scope *scope, AstNode *node) {4771static IrInstruction *ir_gen_goto(IrBuilder *irb, Scope *scope, AstNode *node) {
4739 assert(node->type == NodeTypeGoto);4772 assert(node->type == NodeTypeGoto);
47404773
...@@ -4894,7 +4927,8 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN...@@ -4894,7 +4927,8 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN
4894 if (err_result == irb->codegen->invalid_instruction)4927 if (err_result == irb->codegen->invalid_instruction)
4895 return irb->codegen->invalid_instruction;4928 return irb->codegen->invalid_instruction;
4896 IrBasicBlock *after_err_block = irb->current_basic_block;4929 IrBasicBlock *after_err_block = irb->current_basic_block;
4897 ir_build_br(irb, err_scope, node, end_block, is_comptime);4930 if (!instr_is_unreachable(err_result))
4931 ir_mark_gen(ir_build_br(irb, err_scope, node, end_block, is_comptime));
48984932
4899 ir_set_cursor_at_end(irb, ok_block);4933 ir_set_cursor_at_end(irb, ok_block);
4900 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false);4934 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false);
...@@ -4989,6 +5023,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop...@@ -4989,6 +5023,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
4989 case NodeTypeSwitchProng:5023 case NodeTypeSwitchProng:
4990 case NodeTypeSwitchRange:5024 case NodeTypeSwitchRange:
4991 case NodeTypeStructField:5025 case NodeTypeStructField:
5026 case NodeTypeLabel:
4992 zig_unreachable();5027 zig_unreachable();
4993 case NodeTypeBlock:5028 case NodeTypeBlock:
4994 return ir_lval_wrap(irb, scope, ir_gen_block(irb, scope, node), lval);5029 return ir_lval_wrap(irb, scope, ir_gen_block(irb, scope, node), lval);
...@@ -5040,8 +5075,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop...@@ -5040,8 +5075,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
5040 return ir_lval_wrap(irb, scope, ir_gen_if_var_expr(irb, scope, node), lval);5075 return ir_lval_wrap(irb, scope, ir_gen_if_var_expr(irb, scope, node), lval);
5041 case NodeTypeSwitchExpr:5076 case NodeTypeSwitchExpr:
5042 return ir_lval_wrap(irb, scope, ir_gen_switch_expr(irb, scope, node), lval);5077 return ir_lval_wrap(irb, scope, ir_gen_switch_expr(irb, scope, node), lval);
5043 case NodeTypeLabel:
5044 return ir_lval_wrap(irb, scope, ir_gen_label(irb, scope, node), lval);
5045 case NodeTypeGoto:5078 case NodeTypeGoto:
5046 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);5079 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
5047 case NodeTypeTypeLiteral:5080 case NodeTypeTypeLiteral:
...@@ -5130,7 +5163,7 @@ static bool ir_goto_pass2(IrBuilder *irb) {...@@ -5130,7 +5163,7 @@ static bool ir_goto_pass2(IrBuilder *irb) {
5130 return true;5163 return true;
5131}5164}
51325165
5133IrInstruction *ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {5166bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {
5134 assert(node->owner);5167 assert(node->owner);
51355168
5136 IrBuilder ir_builder = {0};5169 IrBuilder ir_builder = {0};
...@@ -5146,20 +5179,21 @@ IrInstruction *ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutabl...@@ -5146,20 +5179,21 @@ IrInstruction *ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutabl
5146 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValPurposeNone);5179 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValPurposeNone);
5147 assert(result);5180 assert(result);
5148 if (irb->exec->invalid)5181 if (irb->exec->invalid)
5149 return codegen->invalid_instruction;5182 return false;
51505183
5151 IrInstruction *return_instruction = ir_build_return(irb, scope, result->source_node, result);5184 if (!instr_is_unreachable(result)) {
5152 assert(return_instruction);5185 ir_mark_gen(ir_build_return(irb, scope, result->source_node, result));
5186 }
51535187
5154 if (!ir_goto_pass2(irb)) {5188 if (!ir_goto_pass2(irb)) {
5155 irb->exec->invalid = true;5189 irb->exec->invalid = true;
5156 return codegen->invalid_instruction;5190 return false;
5157 }5191 }
51585192
5159 return return_instruction;5193 return true;
5160}5194}
51615195
5162IrInstruction *ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {5196bool ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {
5163 assert(fn_entry);5197 assert(fn_entry);
51645198
5165 IrExecutable *ir_executable = &fn_entry->ir_executable;5199 IrExecutable *ir_executable = &fn_entry->ir_executable;
...@@ -5622,6 +5656,16 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons...@@ -5622,6 +5656,16 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons
5622}5656}
56235657
5624static void ir_finish_bb(IrAnalyze *ira) {5658static void ir_finish_bb(IrAnalyze *ira) {
5659 ira->instruction_index += 1;
5660 while (ira->instruction_index < ira->old_irb.current_basic_block->instruction_list.length) {
5661 IrInstruction *next_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
5662 if (!next_instruction->is_gen) {
5663 ir_add_error(ira, next_instruction, buf_sprintf("unreachable code"));
5664 break;
5665 }
5666 ira->instruction_index += 1;
5667 }
5668
5625 ira->block_queue_index += 1;5669 ira->block_queue_index += 1;
56265670
5627 if (ira->block_queue_index < ira->old_bb_queue.length) {5671 if (ira->block_queue_index < ira->old_bb_queue.length) {
...@@ -5711,6 +5755,11 @@ static TypeTableEntry *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instr...@@ -5711,6 +5755,11 @@ static TypeTableEntry *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instr
5711{5755{
5712 if (pointee_type->id == TypeTableEntryIdMetaType) {5756 if (pointee_type->id == TypeTableEntryIdMetaType) {
5713 TypeTableEntry *type_entry = pointee->data.x_type;5757 TypeTableEntry *type_entry = pointee->data.x_type;
5758 if (type_entry->id == TypeTableEntryIdUnreachable) {
5759 ir_add_error(ira, instruction, buf_sprintf("pointer to unreachable not allowed"));
5760 return ira->codegen->builtin_types.entry_invalid;
5761 }
5762
5714 ConstExprValue *const_val = ir_build_const_from(ira, instruction,5763 ConstExprValue *const_val = ir_build_const_from(ira, instruction,
5715 depends_on_compile_var || pointee->depends_on_compile_var);5764 depends_on_compile_var || pointee->depends_on_compile_var);
5716 type_ensure_zero_bits_known(ira->codegen, type_entry);5765 type_ensure_zero_bits_known(ira->codegen, type_entry);
...@@ -7903,6 +7952,10 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct...@@ -7903,6 +7952,10 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
79037952
7904 if (is_comptime || old_dest_block->ref_count == 1)7953 if (is_comptime || old_dest_block->ref_count == 1)
7905 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);7954 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);
7955
7956 IrBasicBlock *new_dest_block = ir_get_new_bb(ira, old_dest_block);
7957 ir_build_br_from(&ira->new_irb, &cond_br_instruction->base, new_dest_block);
7958 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
7906 }7959 }
79077960
7908 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;7961 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
src/ir.hpp+2-2
...@@ -10,8 +10,8 @@...@@ -10,8 +10,8 @@
1010
11#include "all_types.hpp"11#include "all_types.hpp"
1212
13IrInstruction *ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable);13bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable);
14IrInstruction *ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);14bool ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);
1515
16IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,16IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
17 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,17 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
src/parseh.cpp+2-1
...@@ -688,6 +688,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)...@@ -688,6 +688,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
688 if (!enum_def) {688 if (!enum_def) {
689 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,689 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
690 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));690 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
691 enum_type->data.enumeration.zero_bits_known = true;
691 c->enum_type_table.put(bare_name, enum_type);692 c->enum_type_table.put(bare_name, enum_type);
692 c->decl_table.put(enum_decl, enum_type);693 c->decl_table.put(enum_decl, enum_type);
693 replace_with_fwd_decl(c, enum_type, full_type_name);694 replace_with_fwd_decl(c, enum_type, full_type_name);
...@@ -852,6 +853,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_...@@ -852,6 +853,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
852853
853 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,854 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
854 ContainerKindStruct, c->source_node, buf_ptr(full_type_name));855 ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
856 struct_type->data.structure.zero_bits_known = true;
855857
856 c->struct_type_table.put(bare_name, struct_type);858 c->struct_type_table.put(bare_name, struct_type);
857 c->decl_table.put(record_decl, struct_type);859 c->decl_table.put(record_decl, struct_type);
...@@ -938,7 +940,6 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_...@@ -938,7 +940,6 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
938940
939 struct_type->data.structure.gen_field_count = field_count;941 struct_type->data.structure.gen_field_count = field_count;
940 struct_type->data.structure.complete = true;942 struct_type->data.structure.complete = true;
941 struct_type->data.structure.zero_bits_known = true;
942943
943 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(c->codegen->target_data_ref, struct_type->type_ref);944 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(c->codegen->target_data_ref, struct_type->type_ref);
944 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(c->codegen->target_data_ref, struct_type->type_ref);945 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(c->codegen->target_data_ref, struct_type->type_ref);
test/run_tests.cpp+75-104
...@@ -280,8 +280,6 @@ pub fn bar_function() {...@@ -280,8 +280,6 @@ pub fn bar_function() {
280280
281 add_source_file(tc, "other.zig", R"SOURCE(281 add_source_file(tc, "other.zig", R"SOURCE(
282pub fn foo_function() -> bool {282pub fn foo_function() -> bool {
283 @setFnStaticEval(this, false);
284
285 // this one conflicts with the one from foo283 // this one conflicts with the one from foo
286 return true;284 return true;
287}285}
...@@ -503,7 +501,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {...@@ -503,7 +501,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
503 } else if (*a_int > *b_int) {501 } else if (*a_int > *b_int) {
504 1502 1
505 } else {503 } else {
506 0504 c_int(0)
507 }505 }
508}506}
509507
...@@ -539,21 +537,21 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {...@@ -539,21 +537,21 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
539537
540 add_simple_case("incomplete struct parameter top level decl", R"SOURCE(538 add_simple_case("incomplete struct parameter top level decl", R"SOURCE(
541const io = @import("std").io;539const io = @import("std").io;
542struct A {540const A = struct {
543 b: B,541 b: B,
544}542};
545543
546struct B {544const B = struct {
547 c: C,545 c: C,
548}546};
549547
550struct C {548const C = struct {
551 x: i32,549 x: i32,
552550
553 fn d(c: C) {551 fn d(c: C) {
554 %%io.stdout.printf("OK\n");552 %%io.stdout.printf("OK\n");
555 }553 }
556}554};
557555
558fn foo(a: A) {556fn foo(a: A) {
559 a.b.c.d();557 a.b.c.d();
...@@ -576,17 +574,17 @@ pub fn main(args: [][]u8) -> %void {...@@ -576,17 +574,17 @@ pub fn main(args: [][]u8) -> %void {
576 add_simple_case("same named methods in incomplete struct", R"SOURCE(574 add_simple_case("same named methods in incomplete struct", R"SOURCE(
577const io = @import("std").io;575const io = @import("std").io;
578576
579struct Foo {577const Foo = struct {
580 field1: Bar,578 field1: Bar,
581579
582 fn method(a: &Foo) -> bool { true }580 fn method(a: &Foo) -> bool { true }
583}581};
584582
585struct Bar {583const Bar = struct {
586 field2: i32,584 field2: i32,
587585
588 fn method(b: &Bar) -> bool { true }586 fn method(b: &Bar) -> bool { true }
589}587};
590588
591pub fn main(args: [][]u8) -> %void {589pub fn main(args: [][]u8) -> %void {
592 const bar = Bar {.field2 = 13,};590 const bar = Bar {.field2 = 13,};
...@@ -689,11 +687,11 @@ fn a() {}...@@ -689,11 +687,11 @@ fn a() {}
689687
690 add_compile_fail_case("unreachable with return", R"SOURCE(688 add_compile_fail_case("unreachable with return", R"SOURCE(
691fn a() -> unreachable {return;}689fn a() -> unreachable {return;}
692 )SOURCE", 1, ".tmp_source.zig:2:24: error: expected type 'unreachable', got 'void'");690 )SOURCE", 1, ".tmp_source.zig:2:24: error: expected type 'unreachable', found 'void'");
693691
694 add_compile_fail_case("control reaches end of non-void function", R"SOURCE(692 add_compile_fail_case("control reaches end of non-void function", R"SOURCE(
695fn a() -> i32 {}693fn a() -> i32 {}
696 )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got 'void'");694 )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', found 'void'");
697695
698 add_compile_fail_case("undefined function call", R"SOURCE(696 add_compile_fail_case("undefined function call", R"SOURCE(
699fn a() {697fn a() {
...@@ -706,7 +704,7 @@ fn a() {...@@ -706,7 +704,7 @@ fn a() {
706 b(1);704 b(1);
707}705}
708fn b(a: i32, b: i32, c: i32) { }706fn b(a: i32, b: i32, c: i32) { }
709 )SOURCE", 1, ".tmp_source.zig:3:6: error: expected 3 arguments, got 1");707 )SOURCE", 1, ".tmp_source.zig:3:6: error: expected 3 arguments, found 1");
710708
711 add_compile_fail_case("invalid type", R"SOURCE(709 add_compile_fail_case("invalid type", R"SOURCE(
712fn a() -> bogus {}710fn a() -> bogus {}
...@@ -714,7 +712,7 @@ fn a() -> bogus {}...@@ -714,7 +712,7 @@ fn a() -> bogus {}
714712
715 add_compile_fail_case("pointer to unreachable", R"SOURCE(713 add_compile_fail_case("pointer to unreachable", R"SOURCE(
716fn a() -> &unreachable {}714fn a() -> &unreachable {}
717 )SOURCE", 1, ".tmp_source.zig:2:11: error: pointer to unreachable not allowed");715 )SOURCE", 1, ".tmp_source.zig:2:12: error: pointer to unreachable not allowed");
718716
719 add_compile_fail_case("unreachable code", R"SOURCE(717 add_compile_fail_case("unreachable code", R"SOURCE(
720fn a() {718fn a() {
...@@ -723,7 +721,7 @@ fn a() {...@@ -723,7 +721,7 @@ fn a() {
723}721}
724722
725fn b() {}723fn b() {}
726 )SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");724 )SOURCE", 1, ".tmp_source.zig:4:6: error: unreachable code");
727725
728 add_compile_fail_case("bad import", R"SOURCE(726 add_compile_fail_case("bad import", R"SOURCE(
729const bogus = @import("bogus-does-not-exist.zig");727const bogus = @import("bogus-does-not-exist.zig");
...@@ -761,7 +759,7 @@ fn f() -> i32 {...@@ -761,7 +759,7 @@ fn f() -> i32 {
761 const a = c"a";759 const a = c"a";
762 a760 a
763}761}
764 )SOURCE", 1, ".tmp_source.zig:4:5: error: expected type 'i32', got '&const u8'");762 )SOURCE", 1, ".tmp_source.zig:4:5: error: expected type 'i32', found '&const u8'");
765763
766 add_compile_fail_case("if condition is bool, not int", R"SOURCE(764 add_compile_fail_case("if condition is bool, not int", R"SOURCE(
767fn f() {765fn f() {
...@@ -819,7 +817,7 @@ fn f() {...@@ -819,7 +817,7 @@ fn f() {
819 )SOURCE", 4, ".tmp_source.zig:4:5: error: use of undeclared identifier 'i'",817 )SOURCE", 4, ".tmp_source.zig:4:5: error: use of undeclared identifier 'i'",
820 ".tmp_source.zig:4:7: error: use of undeclared identifier 'i'",818 ".tmp_source.zig:4:7: error: use of undeclared identifier 'i'",
821 ".tmp_source.zig:5:8: error: array access of non-array",819 ".tmp_source.zig:5:8: error: array access of non-array",
822 ".tmp_source.zig:5:9: error: expected type 'usize', got 'bool'");820 ".tmp_source.zig:5:9: error: expected type 'usize', found 'bool'");
823821
824 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(822 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
825fn f(...) {}823fn f(...) {}
...@@ -838,21 +836,21 @@ fn f(b: bool) {...@@ -838,21 +836,21 @@ fn f(b: bool) {
838 const x : i32 = if (b) { 1 };836 const x : i32 = if (b) { 1 };
839 const y = if (b) { i32(1) };837 const y = if (b) { i32(1) };
840}838}
841 )SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",839 )SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', found 'void'",
842 ".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");840 ".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");
843841
844 add_compile_fail_case("direct struct loop", R"SOURCE(842 add_compile_fail_case("direct struct loop", R"SOURCE(
845struct A { a : A, }843const A = struct { a : A, };
846 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");844 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");
847845
848 add_compile_fail_case("indirect struct loop", R"SOURCE(846 add_compile_fail_case("indirect struct loop", R"SOURCE(
849struct A { b : B, }847const A = struct { b : B, };
850struct B { c : C, }848const B = struct { c : C, };
851struct C { a : A, }849const C = struct { a : A, };
852 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");850 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");
853851
854 add_compile_fail_case("invalid struct field", R"SOURCE(852 add_compile_fail_case("invalid struct field", R"SOURCE(
855struct A { x : i32, }853const A = struct { x : i32, };
856fn f() {854fn f() {
857 var a : A = undefined;855 var a : A = undefined;
858 a.foo = 1;856 a.foo = 1;
...@@ -863,13 +861,13 @@ fn f() {...@@ -863,13 +861,13 @@ fn f() {
863 ".tmp_source.zig:6:16: error: no member named 'bar' in 'A'");861 ".tmp_source.zig:6:16: error: no member named 'bar' in 'A'");
864862
865 add_compile_fail_case("redefinition of struct", R"SOURCE(863 add_compile_fail_case("redefinition of struct", R"SOURCE(
866struct A { x : i32, }864const A = struct { x : i32, };
867struct A { y : i32, }865const A = struct { y : i32, };
868 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");866 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");
869867
870 add_compile_fail_case("redefinition of enums", R"SOURCE(868 add_compile_fail_case("redefinition of enums", R"SOURCE(
871enum A {}869const A = enum {};
872enum A {}870const A = enum {};
873 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");871 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");
874872
875 add_compile_fail_case("redefinition of global variables", R"SOURCE(873 add_compile_fail_case("redefinition of global variables", R"SOURCE(
...@@ -878,23 +876,23 @@ var a : i32 = 2;...@@ -878,23 +876,23 @@ var a : i32 = 2;
878 )SOURCE", 1, ".tmp_source.zig:3:1: error: redeclaration of variable 'a'");876 )SOURCE", 1, ".tmp_source.zig:3:1: error: redeclaration of variable 'a'");
879877
880 add_compile_fail_case("byvalue struct parameter in exported function", R"SOURCE(878 add_compile_fail_case("byvalue struct parameter in exported function", R"SOURCE(
881struct A { x : i32, }879const A = struct { x : i32, };
882export fn f(a : A) {}880export fn f(a : A) {}
883 )SOURCE", 1, ".tmp_source.zig:3:13: error: byvalue types not yet supported on extern function parameters");881 )SOURCE", 1, ".tmp_source.zig:3:13: error: byvalue types not yet supported on extern function parameters");
884882
885 add_compile_fail_case("byvalue struct return value in exported function", R"SOURCE(883 add_compile_fail_case("byvalue struct return value in exported function", R"SOURCE(
886struct A { x: i32, }884const A = struct { x: i32, };
887export fn f() -> A {885export fn f() -> A {
888 A {.x = 1234 }886 A {.x = 1234 }
889}887}
890 )SOURCE", 1, ".tmp_source.zig:3:18: error: byvalue types not yet supported on extern function return values");888 )SOURCE", 1, ".tmp_source.zig:3:18: error: byvalue types not yet supported on extern function return values");
891889
892 add_compile_fail_case("duplicate field in struct value expression", R"SOURCE(890 add_compile_fail_case("duplicate field in struct value expression", R"SOURCE(
893struct A {891const A = struct {
894 x : i32,892 x : i32,
895 y : i32,893 y : i32,
896 z : i32,894 z : i32,
897}895};
898fn f() {896fn f() {
899 const a = A {897 const a = A {
900 .z = 1,898 .z = 1,
...@@ -906,11 +904,11 @@ fn f() {...@@ -906,11 +904,11 @@ fn f() {
906 )SOURCE", 1, ".tmp_source.zig:12:9: error: duplicate field");904 )SOURCE", 1, ".tmp_source.zig:12:9: error: duplicate field");
907905
908 add_compile_fail_case("missing field in struct value expression", R"SOURCE(906 add_compile_fail_case("missing field in struct value expression", R"SOURCE(
909struct A {907const A = struct {
910 x : i32,908 x : i32,
911 y : i32,909 y : i32,
912 z : i32,910 z : i32,
913}911};
914fn f() {912fn f() {
915 // we want the error on the '{' not the 'A' because913 // we want the error on the '{' not the 'A' because
916 // the A could be a complicated expression914 // the A could be a complicated expression
...@@ -922,11 +920,11 @@ fn f() {...@@ -922,11 +920,11 @@ fn f() {
922 )SOURCE", 1, ".tmp_source.zig:10:17: error: missing field: 'x'");920 )SOURCE", 1, ".tmp_source.zig:10:17: error: missing field: 'x'");
923921
924 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(922 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(
925struct A {923const A = struct {
926 x : i32,924 x : i32,
927 y : i32,925 y : i32,
928 z : i32,926 z : i32,
929}927};
930fn f() {928fn f() {
931 const a = A {929 const a = A {
932 .z = 4,930 .z = 4,
...@@ -983,8 +981,8 @@ var foo = u8;...@@ -983,8 +981,8 @@ var foo = u8;
983 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");981 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");
984982
985 add_compile_fail_case("variables shadowing types", R"SOURCE(983 add_compile_fail_case("variables shadowing types", R"SOURCE(
986struct Foo {}984const Foo = struct {};
987struct Bar {}985const Bar = struct {};
988986
989fn f(Foo: i32) {987fn f(Foo: i32) {
990 var Bar : i32 = undefined;988 var Bar : i32 = undefined;
...@@ -1014,7 +1012,7 @@ const x = foo();...@@ -1014,7 +1012,7 @@ const x = foo();
1014fn f(s: []u8) -> []u8 {1012fn f(s: []u8) -> []u8 {
1015 s ++ "foo"1013 s ++ "foo"
1016}1014}
1017 )SOURCE", 1, ".tmp_source.zig:3:5: error: expected array or C string literal, got '[]u8'");1015 )SOURCE", 1, ".tmp_source.zig:3:5: error: expected array or C string literal, found '[]u8'");
10181016
1019 add_compile_fail_case("non compile time array concatenation", R"SOURCE(1017 add_compile_fail_case("non compile time array concatenation", R"SOURCE(
1020fn f(s: [10]u8) -> []u8 {1018fn f(s: [10]u8) -> []u8 {
...@@ -1034,9 +1032,9 @@ const y = &x;...@@ -1034,9 +1032,9 @@ const y = &x;
10341032
1035 add_compile_fail_case("@typeOf number literal", R"SOURCE(1033 add_compile_fail_case("@typeOf number literal", R"SOURCE(
1036const x = 3;1034const x = 3;
1037struct Foo {1035const Foo = struct {
1038 index: @typeOf(x),1036 index: @typeOf(x),
1039}1037};
1040 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeOf");1038 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeOf");
10411039
1042 add_compile_fail_case("integer overflow error", R"SOURCE(1040 add_compile_fail_case("integer overflow error", R"SOURCE(
...@@ -1048,7 +1046,7 @@ const x = 2 == 2.0;...@@ -1048,7 +1046,7 @@ const x = 2 == 2.0;
1048 )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");1046 )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");
10491047
1050 add_compile_fail_case("missing function call param", R"SOURCE(1048 add_compile_fail_case("missing function call param", R"SOURCE(
1051struct Foo {1049const Foo = struct {
1052 a: i32,1050 a: i32,
1053 b: i32,1051 b: i32,
10541052
...@@ -1058,7 +1056,7 @@ struct Foo {...@@ -1058,7 +1056,7 @@ struct Foo {
1058 fn member_b(foo: Foo) -> i32 {1056 fn member_b(foo: Foo) -> i32 {
1059 return foo.b;1057 return foo.b;
1060 }1058 }
1061}1059};
10621060
1063const member_fn_type = @typeOf(Foo.member_a);1061const member_fn_type = @typeOf(Foo.member_a);
1064const members = []member_fn_type {1062const members = []member_fn_type {
...@@ -1069,7 +1067,7 @@ const members = []member_fn_type {...@@ -1069,7 +1067,7 @@ const members = []member_fn_type {
1069fn f(foo: Foo, index: i32) {1067fn f(foo: Foo, index: i32) {
1070 const result = members[index]();1068 const result = members[index]();
1071}1069}
1072 )SOURCE", 1, ".tmp_source.zig:21:34: error: expected 1 arguments, got 0");1070 )SOURCE", 1, ".tmp_source.zig:21:34: error: expected 1 arguments, found 0");
10731071
1074 add_compile_fail_case("missing function name and param name", R"SOURCE(1072 add_compile_fail_case("missing function name and param name", R"SOURCE(
1075fn () {}1073fn () {}
...@@ -1084,22 +1082,22 @@ fn a() -> i32 {0}...@@ -1084,22 +1082,22 @@ fn a() -> i32 {0}
1084fn b() -> i32 {1}1082fn b() -> i32 {1}
1085fn c() -> i32 {2}1083fn c() -> i32 {2}
1086 )SOURCE", 3,1084 )SOURCE", 3,
1087 ".tmp_source.zig:2:21: error: expected type 'fn()', got 'fn() -> i32'",1085 ".tmp_source.zig:2:21: error: expected type 'fn()', found 'fn() -> i32'",
1088 ".tmp_source.zig:2:24: error: expected type 'fn()', got 'fn() -> i32'",1086 ".tmp_source.zig:2:24: error: expected type 'fn()', found 'fn() -> i32'",
1089 ".tmp_source.zig:2:27: error: expected type 'fn()', got 'fn() -> i32'");1087 ".tmp_source.zig:2:27: error: expected type 'fn()', found 'fn() -> i32'");
10901088
1091 add_compile_fail_case("extern function pointer mismatch", R"SOURCE(1089 add_compile_fail_case("extern function pointer mismatch", R"SOURCE(
1092const fns = [](fn(i32)->i32){ a, b, c };1090const fns = [](fn(i32)->i32){ a, b, c };
1093pub fn a(x: i32) -> i32 {x + 0}1091pub fn a(x: i32) -> i32 {x + 0}
1094pub fn b(x: i32) -> i32 {x + 1}1092pub fn b(x: i32) -> i32 {x + 1}
1095export fn c(x: i32) -> i32 {x + 2}1093export fn c(x: i32) -> i32 {x + 2}
1096 )SOURCE", 1, ".tmp_source.zig:2:37: error: expected type 'fn(i32) -> i32', got 'extern fn(i32) -> i32'");1094 )SOURCE", 1, ".tmp_source.zig:2:37: error: expected type 'fn(i32) -> i32', found 'extern fn(i32) -> i32'");
10971095
10981096
1099 add_compile_fail_case("implicit cast from f64 to f32", R"SOURCE(1097 add_compile_fail_case("implicit cast from f64 to f32", R"SOURCE(
1100const x : f64 = 1.0;1098const x : f64 = 1.0;
1101const y : f32 = x;1099const y : f32 = x;
1102 )SOURCE", 1, ".tmp_source.zig:3:17: error: expected type 'f32', got 'f64'");1100 )SOURCE", 1, ".tmp_source.zig:3:17: error: expected type 'f32', found 'f64'");
11031101
11041102
1105 add_compile_fail_case("colliding invalid top level functions", R"SOURCE(1103 add_compile_fail_case("colliding invalid top level functions", R"SOURCE(
...@@ -1122,9 +1120,9 @@ fn a(x: i32) {...@@ -1122,9 +1120,9 @@ fn a(x: i32) {
1122 )SOURCE", 1, ".tmp_source.zig:3:26: error: unable to evaluate constant expression");1120 )SOURCE", 1, ".tmp_source.zig:3:26: error: unable to evaluate constant expression");
11231121
1124 add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(1122 add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(
1125struct Foo {1123const Foo = struct {
1126 y: [get()]u8,1124 y: [get()]u8,
1127}1125};
1128var global_var: usize = 1;1126var global_var: usize = 1;
1129fn get() -> usize { global_var }1127fn get() -> usize { global_var }
1130 )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");1128 )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");
...@@ -1138,9 +1136,9 @@ fn f() {...@@ -1138,9 +1136,9 @@ fn f() {
11381136
11391137
1140 add_compile_fail_case("addition with non numbers", R"SOURCE(1138 add_compile_fail_case("addition with non numbers", R"SOURCE(
1141struct Foo {1139const Foo = struct {
1142 field: i32,1140 field: i32,
1143}1141};
1144const x = Foo {.field = 1} + Foo {.field = 2};1142const x = Foo {.field = 1} + Foo {.field = 2};
1145 )SOURCE", 1, ".tmp_source.zig:5:28: error: invalid operands to binary expression: 'Foo' and 'Foo'");1143 )SOURCE", 1, ".tmp_source.zig:5:28: error: invalid operands to binary expression: 'Foo' and 'Foo'");
11461144
...@@ -1158,12 +1156,12 @@ const float_x = f32(1.0) / f32(0.0);...@@ -1158,12 +1156,12 @@ const float_x = f32(1.0) / f32(0.0);
11581156
11591157
1160 add_compile_fail_case("missing switch prong", R"SOURCE(1158 add_compile_fail_case("missing switch prong", R"SOURCE(
1161enum Number {1159const Number = enum {
1162 One,1160 One,
1163 Two,1161 Two,
1164 Three,1162 Three,
1165 Four,1163 Four,
1166}1164};
1167fn f(n: Number) -> i32 {1165fn f(n: Number) -> i32 {
1168 switch (n) {1166 switch (n) {
1169 One => 1,1167 One => 1,
...@@ -1221,7 +1219,7 @@ fn derp(){}...@@ -1221,7 +1219,7 @@ fn derp(){}
12211219
1222 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(1220 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
1223const a: &u8 = null;1221const a: &u8 = null;
1224 )SOURCE", 1, ".tmp_source.zig:2:16: error: expected type '&u8', got '(null)'");1222 )SOURCE", 1, ".tmp_source.zig:2:16: error: expected type '&u8', found '(null)'");
12251223
1226 add_compile_fail_case("indexing an array of size zero", R"SOURCE(1224 add_compile_fail_case("indexing an array of size zero", R"SOURCE(
1227const array = []u8{};1225const array = []u8{};
...@@ -1261,22 +1259,23 @@ const resource = @embedFile("bogus.txt");...@@ -1261,22 +1259,23 @@ const resource = @embedFile("bogus.txt");
12611259
12621260
1263 add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE(1261 add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE(
1264struct Foo {1262const Foo = {
1265 x: i32,1263 x: i32,
1266}1264};
1267const a = Foo {.x = get_it()};1265const a = Foo {.x = get_it()};
1268extern fn get_it() -> i32;1266extern fn get_it() -> i32;
1269 )SOURCE", 1, ".tmp_source.zig:5:27: error: unable to evaluate constant expression");1267 )SOURCE", 1, ".tmp_source.zig:5:27: error: unable to evaluate constant expression");
12701268
1271 add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(1269 add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(
1272struct Foo {1270const Foo = {
1273 x: i32,1271 x: i32,
1274}1272};
1275const a = get_it();1273const a = get_it();
1276fn get_it() -> Foo {1274fn get_it() -> Foo {
1277 @setFnStaticEval(this, false);1275 global_side_effect = true;
1278 Foo {.x = 13}1276 Foo {.x = 13}
1279}1277}
1278var global_side_effect = false;
12801279
1281 )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");1280 )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");
12821281
...@@ -1293,10 +1292,10 @@ fn test_a_thing() {...@@ -1293,10 +1292,10 @@ fn test_a_thing() {
1293fn bad_eql_1(a: []u8, b: []u8) -> bool {1292fn bad_eql_1(a: []u8, b: []u8) -> bool {
1294 a == b1293 a == b
1295}1294}
1296enum EnumWithData {1295const EnumWithData = enum {
1297 One,1296 One,
1298 Two: i32,1297 Two: i32,
1299}1298};
1300fn bad_eql_2(a: EnumWithData, b: EnumWithData) -> bool {1299fn bad_eql_2(a: EnumWithData, b: EnumWithData) -> bool {
1301 a == b1300 a == b
1302}1301}
...@@ -1313,7 +1312,6 @@ fn foo() {...@@ -1313,7 +1312,6 @@ fn foo() {
1313 };1312 };
1314}1313}
1315fn bar() -> i32 {1314fn bar() -> i32 {
1316 @setFnStaticEval(this, false);
1317 21315 2
1318}1316}
13191317
...@@ -1383,7 +1381,7 @@ fn f() {...@@ -1383,7 +1381,7 @@ fn f() {
1383 const x: u32 = 10;1381 const x: u32 = 10;
1384 @truncate(i8, x);1382 @truncate(i8, x);
1385}1383}
1386 )SOURCE", 1, ".tmp_source.zig:4:19: error: expected signed integer type, got 'u32'");1384 )SOURCE", 1, ".tmp_source.zig:4:19: error: expected signed integer type, found 'u32'");
13871385
1388 add_compile_fail_case("truncate same bit count", R"SOURCE(1386 add_compile_fail_case("truncate same bit count", R"SOURCE(
1389fn f() {1387fn f() {
...@@ -1474,14 +1472,14 @@ fn f(m: []const u8) {...@@ -1474,14 +1472,14 @@ fn f(m: []const u8) {
1474 )SOURCE", 1, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");1472 )SOURCE", 1, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");
14751473
1476 add_compile_fail_case("wrong number of arguments for method fn call", R"SOURCE(1474 add_compile_fail_case("wrong number of arguments for method fn call", R"SOURCE(
1477struct Foo {1475const Foo = {
1478 fn method(self: &const Foo, a: i32) {}1476 fn method(self: &const Foo, a: i32) {}
1479}1477};
1480fn f(foo: &const Foo) {1478fn f(foo: &const Foo) {
14811479
1482 foo.method(1, 2);1480 foo.method(1, 2);
1483}1481}
1484 )SOURCE", 1, ".tmp_source.zig:7:15: error: expected 1 arguments, got 2");1482 )SOURCE", 1, ".tmp_source.zig:7:15: error: expected 1 arguments, found 2");
14851483
1486 add_compile_fail_case("assign through constant pointer", R"SOURCE(1484 add_compile_fail_case("assign through constant pointer", R"SOURCE(
1487fn f() {1485fn f() {
...@@ -1511,12 +1509,12 @@ fn foo(blah: []u8) {...@@ -1511,12 +1509,12 @@ fn foo(blah: []u8) {
1511const JasonHM = u8;1509const JasonHM = u8;
1512const JasonList = &JsonNode;1510const JasonList = &JsonNode;
15131511
1514enum JsonOA {1512const JsonOA = enum {
1515 JSONArray: JsonList,1513 JSONArray: JsonList,
1516 JSONObject: JasonHM,1514 JSONObject: JasonHM,
1517}1515};
15181516
1519enum JsonType {1517const JsonType = enum {
1520 JSONNull: void,1518 JSONNull: void,
1521 JSONInteger: isize,1519 JSONInteger: isize,
1522 JSONDouble: f64,1520 JSONDouble: f64,
...@@ -1524,7 +1522,7 @@ enum JsonType {...@@ -1524,7 +1522,7 @@ enum JsonType {
1524 JSONString: []u8,1522 JSONString: []u8,
1525 JSONArray,1523 JSONArray,
1526 JSONObject,1524 JSONObject,
1527}1525};
15281526
1529pub struct JsonNode {1527pub struct JsonNode {
1530 kind: JsonType,1528 kind: JsonType,
...@@ -1541,7 +1539,7 @@ fn foo() {...@@ -1541,7 +1539,7 @@ fn foo() {
1541 ".tmp_source.zig:27:8: error: no member named 'init' in 'JsonNode'");1539 ".tmp_source.zig:27:8: error: no member named 'init' in 'JsonNode'");
15421540
1543 add_compile_fail_case("method call with first arg type primitive", R"SOURCE(1541 add_compile_fail_case("method call with first arg type primitive", R"SOURCE(
1544struct Foo {1542const Foo = {
1545 x: i32,1543 x: i32,
15461544
1547 fn init(x: i32) -> Foo {1545 fn init(x: i32) -> Foo {
...@@ -1549,7 +1547,7 @@ struct Foo {...@@ -1549,7 +1547,7 @@ struct Foo {
1549 .x = x,1547 .x = x,
1550 }1548 }
1551 }1549 }
1552}1550};
15531551
1554fn f() {1552fn f() {
1555 const derp = Foo.init(3);1553 const derp = Foo.init(3);
...@@ -1622,14 +1620,9 @@ pub fn main(args: [][]u8) -> %void {...@@ -1622,14 +1620,9 @@ pub fn main(args: [][]u8) -> %void {
1622 baz(bar(a));1620 baz(bar(a));
1623}1621}
1624fn bar(a: []i32) -> i32 {1622fn bar(a: []i32) -> i32 {
1625 @setFnStaticEval(this, false);
1626
1627 a[4]1623 a[4]
1628}1624}
1629fn baz(a: i32) {1625fn baz(a: i32) { }
1630 @setFnStaticEval(this, false);
1631}
1632
1633 )SOURCE");1626 )SOURCE");
16341627
1635 add_debug_safety_case("integer addition overflow", R"SOURCE(1628 add_debug_safety_case("integer addition overflow", R"SOURCE(
...@@ -1639,8 +1632,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1639,8 +1632,6 @@ pub fn main(args: [][]u8) -> %void {
1639 if (x == 0) return error.Whatever;1632 if (x == 0) return error.Whatever;
1640}1633}
1641fn add(a: u16, b: u16) -> u16 {1634fn add(a: u16, b: u16) -> u16 {
1642 @setFnStaticEval(this, false);
1643
1644 a + b1635 a + b
1645}1636}
1646 )SOURCE");1637 )SOURCE");
...@@ -1652,8 +1643,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1652,8 +1643,6 @@ pub fn main(args: [][]u8) -> %void {
1652 if (x == 0) return error.Whatever;1643 if (x == 0) return error.Whatever;
1653}1644}
1654fn sub(a: u16, b: u16) -> u16 {1645fn sub(a: u16, b: u16) -> u16 {
1655 @setFnStaticEval(this, false);
1656
1657 a - b1646 a - b
1658}1647}
1659 )SOURCE");1648 )SOURCE");
...@@ -1665,8 +1654,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1665,8 +1654,6 @@ pub fn main(args: [][]u8) -> %void {
1665 if (x == 0) return error.Whatever;1654 if (x == 0) return error.Whatever;
1666}1655}
1667fn mul(a: u16, b: u16) -> u16 {1656fn mul(a: u16, b: u16) -> u16 {
1668 @setFnStaticEval(this, false);
1669
1670 a * b1657 a * b
1671}1658}
1672 )SOURCE");1659 )SOURCE");
...@@ -1678,8 +1665,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1678,8 +1665,6 @@ pub fn main(args: [][]u8) -> %void {
1678 if (x == 0) return error.Whatever;1665 if (x == 0) return error.Whatever;
1679}1666}
1680fn neg(a: i16) -> i16 {1667fn neg(a: i16) -> i16 {
1681 @setFnStaticEval(this, false);
1682
1683 -a1668 -a
1684}1669}
1685 )SOURCE");1670 )SOURCE");
...@@ -1691,8 +1676,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1691,8 +1676,6 @@ pub fn main(args: [][]u8) -> %void {
1691 if (x == 0) return error.Whatever;1676 if (x == 0) return error.Whatever;
1692}1677}
1693fn shl(a: i16, b: i16) -> i16 {1678fn shl(a: i16, b: i16) -> i16 {
1694 @setFnStaticEval(this, false);
1695
1696 a << b1679 a << b
1697}1680}
1698 )SOURCE");1681 )SOURCE");
...@@ -1704,8 +1687,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1704,8 +1687,6 @@ pub fn main(args: [][]u8) -> %void {
1704 if (x == 0) return error.Whatever;1687 if (x == 0) return error.Whatever;
1705}1688}
1706fn shl(a: u16, b: u16) -> u16 {1689fn shl(a: u16, b: u16) -> u16 {
1707 @setFnStaticEval(this, false);
1708
1709 a << b1690 a << b
1710}1691}
1711 )SOURCE");1692 )SOURCE");
...@@ -1716,8 +1697,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1716,8 +1697,6 @@ pub fn main(args: [][]u8) -> %void {
1716 const x = div0(999, 0);1697 const x = div0(999, 0);
1717}1698}
1718fn div0(a: i32, b: i32) -> i32 {1699fn div0(a: i32, b: i32) -> i32 {
1719 @setFnStaticEval(this, false);
1720
1721 a / b1700 a / b
1722}1701}
1723 )SOURCE");1702 )SOURCE");
...@@ -1729,8 +1708,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1729,8 +1708,6 @@ pub fn main(args: [][]u8) -> %void {
1729 if (x == 0) return error.Whatever;1708 if (x == 0) return error.Whatever;
1730}1709}
1731fn divExact(a: i32, b: i32) -> i32 {1710fn divExact(a: i32, b: i32) -> i32 {
1732 @setFnStaticEval(this, false);
1733
1734 @divExact(a, b)1711 @divExact(a, b)
1735}1712}
1736 )SOURCE");1713 )SOURCE");
...@@ -1742,8 +1719,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1742,8 +1719,6 @@ pub fn main(args: [][]u8) -> %void {
1742 if (x.len == 0) return error.Whatever;1719 if (x.len == 0) return error.Whatever;
1743}1720}
1744fn widenSlice(slice: []u8) -> []i32 {1721fn widenSlice(slice: []u8) -> []i32 {
1745 @setFnStaticEval(this, false);
1746
1747 ([]i32)(slice)1722 ([]i32)(slice)
1748}1723}
1749 )SOURCE");1724 )SOURCE");
...@@ -1755,8 +1730,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1755,8 +1730,6 @@ pub fn main(args: [][]u8) -> %void {
1755 if (x == 0) return error.Whatever;1730 if (x == 0) return error.Whatever;
1756}1731}
1757fn shorten_cast(x: i32) -> i8 {1732fn shorten_cast(x: i32) -> i8 {
1758 @setFnStaticEval(this, false);
1759
1760 i8(x)1733 i8(x)
1761}1734}
1762 )SOURCE");1735 )SOURCE");
...@@ -1768,8 +1741,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1768,8 +1741,6 @@ pub fn main(args: [][]u8) -> %void {
1768 if (x == 0) return error.Whatever;1741 if (x == 0) return error.Whatever;
1769}1742}
1770fn unsigned_cast(x: i32) -> u32 {1743fn unsigned_cast(x: i32) -> u32 {
1771 @setFnStaticEval(this, false);
1772
1773 u32(x)1744 u32(x)
1774}1745}
1775 )SOURCE");1746 )SOURCE");