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 {
14941494 size_t ref_count;
14951495 IrInstruction *other;
14961496 IrBasicBlock *owner_bb;
1497 // true if this instruction was generated by zig and not from user code
1498 bool is_gen;
14971499};
14981500
14991501struct IrInstructionCondBr {
src/analyze.cpp+2
......@@ -2620,6 +2620,8 @@ bool is_node_void_expr(AstNode *node) {
26202620 {
26212621 return true;
26222622 }
2623 } else if (node->type == NodeTypeBlock && node->data.block.statements.length == 0) {
2624 return true;
26232625 }
26242626
26252627 return false;
src/ir.cpp+241-188
......@@ -98,6 +98,10 @@ static bool instr_is_comptime(IrInstruction *instruction) {
9898 return instruction->value.special != ConstValSpecialRuntime;
9999}
100100
101static bool instr_is_unreachable(IrInstruction *instruction) {
102 return instruction->value.type && instruction->value.type->id == TypeTableEntryIdUnreachable;
103}
104
101105static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {
102106 new_instruction->other = old_instruction;
103107 old_instruction->other = new_instruction;
......@@ -112,8 +116,10 @@ static void ir_ref_bb(IrBasicBlock *bb) {
112116 bb->ref_count += 1;
113117}
114118
115static void ir_ref_instruction(IrInstruction *instruction) {
119static void ir_ref_instruction(IrInstruction *instruction, IrBasicBlock *cur_bb) {
116120 instruction->ref_count += 1;
121 if (instruction->owner_bb != cur_bb)
122 ir_ref_bb(instruction->owner_bb);
117123}
118124
119125static void ir_ref_var(VariableTableEntry *var) {
......@@ -499,7 +505,7 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, Scope *scope, AstNode *sourc
499505 cast_instruction->value = value;
500506 cast_instruction->cast_op = cast_op;
501507
502 ir_ref_instruction(value);
508 ir_ref_instruction(value, irb->current_basic_block);
503509
504510 return &cast_instruction->base;
505511}
......@@ -515,10 +521,10 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so
515521 cond_br_instruction->else_block = else_block;
516522 cond_br_instruction->is_comptime = is_comptime;
517523
518 ir_ref_instruction(condition);
524 ir_ref_instruction(condition, irb->current_basic_block);
519525 ir_ref_bb(then_block);
520526 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
523529 return &cond_br_instruction->base;
524530}
......@@ -538,7 +544,7 @@ static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *sou
538544 return_instruction->base.value.special = ConstValSpecialStatic;
539545 return_instruction->value = return_value;
540546
541 ir_ref_instruction(return_value);
547 ir_ref_instruction(return_value, irb->current_basic_block);
542548
543549 return &return_instruction->base;
544550}
......@@ -699,8 +705,8 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *sou
699705 bin_op_instruction->op2 = op2;
700706 bin_op_instruction->safety_check_on = safety_check_on;
701707
702 ir_ref_instruction(op1);
703 ir_ref_instruction(op2);
708 ir_ref_instruction(op1, irb->current_basic_block);
709 ir_ref_instruction(op2, irb->current_basic_block);
704710
705711 return &bin_op_instruction->base;
706712}
......@@ -738,8 +744,8 @@ static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *s
738744 instruction->elem_index = elem_index;
739745 instruction->safety_check_on = safety_check_on;
740746
741 ir_ref_instruction(array_ptr);
742 ir_ref_instruction(elem_index);
747 ir_ref_instruction(array_ptr, irb->current_basic_block);
748 ir_ref_instruction(elem_index, irb->current_basic_block);
743749
744750 return &instruction->base;
745751}
......@@ -760,7 +766,7 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *
760766 instruction->container_ptr = container_ptr;
761767 instruction->field_name = field_name;
762768
763 ir_ref_instruction(container_ptr);
769 ir_ref_instruction(container_ptr, irb->current_basic_block);
764770
765771 return &instruction->base;
766772}
......@@ -772,7 +778,7 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
772778 instruction->struct_ptr = struct_ptr;
773779 instruction->field = field;
774780
775 ir_ref_instruction(struct_ptr);
781 ir_ref_instruction(struct_ptr, irb->current_basic_block);
776782
777783 return &instruction->base;
778784}
......@@ -793,7 +799,7 @@ static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, Scope *scope, AstN
793799 instruction->enum_ptr = enum_ptr;
794800 instruction->field = field;
795801
796 ir_ref_instruction(enum_ptr);
802 ir_ref_instruction(enum_ptr, irb->current_basic_block);
797803
798804 return &instruction->base;
799805}
......@@ -819,9 +825,9 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
819825 call_instruction->arg_count = arg_count;
820826
821827 if (fn_ref)
822 ir_ref_instruction(fn_ref);
828 ir_ref_instruction(fn_ref, irb->current_basic_block);
823829 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
826832 return &call_instruction->base;
827833}
......@@ -849,7 +855,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source
849855
850856 for (size_t i = 0; i < incoming_count; i += 1) {
851857 ir_ref_bb(incoming_blocks[i]);
852 ir_ref_instruction(incoming_values[i]);
858 ir_ref_instruction(incoming_values[i], irb->current_basic_block);
853859 }
854860
855861 return &phi_instruction->base;
......@@ -874,7 +880,7 @@ static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source
874880 br_instruction->is_comptime = is_comptime;
875881
876882 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
879885 return &br_instruction->base;
880886}
......@@ -898,7 +904,7 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
898904 br_instruction->op_id = op_id;
899905 br_instruction->value = value;
900906
901 ir_ref_instruction(value);
907 ir_ref_instruction(value, irb->current_basic_block);
902908
903909 return &br_instruction->base;
904910}
......@@ -921,9 +927,9 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,
921927 container_init_list_instruction->item_count = item_count;
922928 container_init_list_instruction->items = items;
923929
924 ir_ref_instruction(container_type);
930 ir_ref_instruction(container_type, irb->current_basic_block);
925931 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);
927933 }
928934
929935 return &container_init_list_instruction->base;
......@@ -947,9 +953,9 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scop
947953 container_init_fields_instruction->field_count = field_count;
948954 container_init_fields_instruction->fields = fields;
949955
950 ir_ref_instruction(container_type);
956 ir_ref_instruction(container_type, irb->current_basic_block);
951957 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);
953959 }
954960
955961 return &container_init_fields_instruction->base;
......@@ -964,7 +970,7 @@ static IrInstruction *ir_build_struct_init(IrBuilder *irb, Scope *scope, AstNode
964970 struct_init_instruction->fields = fields;
965971
966972 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
969975 return &struct_init_instruction->base;
970976}
......@@ -1001,8 +1007,8 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
10011007 instruction->ptr = ptr;
10021008 instruction->value = value;
10031009
1004 ir_ref_instruction(ptr);
1005 ir_ref_instruction(value);
1010 ir_ref_instruction(ptr, irb->current_basic_block);
1011 ir_ref_instruction(value, irb->current_basic_block);
10061012
10071013 return &instruction->base;
10081014}
......@@ -1026,8 +1032,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s
10261032 decl_var_instruction->var_type = var_type;
10271033 decl_var_instruction->init_value = init_value;
10281034
1029 if (var_type) ir_ref_instruction(var_type);
1030 ir_ref_instruction(init_value);
1035 if (var_type) ir_ref_instruction(var_type, irb->current_basic_block);
1036 ir_ref_instruction(init_value, irb->current_basic_block);
10311037
10321038 return &decl_var_instruction->base;
10331039}
......@@ -1045,7 +1051,7 @@ static IrInstruction *ir_build_load_ptr(IrBuilder *irb, Scope *scope, AstNode *s
10451051 IrInstructionLoadPtr *instruction = ir_build_instruction<IrInstructionLoadPtr>(irb, scope, source_node);
10461052 instruction->ptr = ptr;
10471053
1048 ir_ref_instruction(ptr);
1054 ir_ref_instruction(ptr, irb->current_basic_block);
10491055
10501056 return &instruction->base;
10511057}
......@@ -1061,7 +1067,7 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
10611067 IrInstructionTypeOf *instruction = ir_build_instruction<IrInstructionTypeOf>(irb, scope, source_node);
10621068 instruction->value = value;
10631069
1064 ir_ref_instruction(value);
1070 ir_ref_instruction(value, irb->current_basic_block);
10651071
10661072 return &instruction->base;
10671073}
......@@ -1070,7 +1076,7 @@ static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode
10701076 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
10711077 instruction->value = value;
10721078
1073 ir_ref_instruction(value);
1079 ir_ref_instruction(value, irb->current_basic_block);
10741080
10751081 return &instruction->base;
10761082}
......@@ -1082,7 +1088,7 @@ static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstN
10821088 irb, scope, source_node);
10831089 instruction->value = value;
10841090
1085 ir_ref_instruction(value);
1091 ir_ref_instruction(value, irb->current_basic_block);
10861092
10871093 return &instruction->base;
10881094}
......@@ -1093,7 +1099,7 @@ static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, Scope *scope, AstNode
10931099 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, scope, source_node);
10941100 instruction->fn_value = fn_value;
10951101
1096 ir_ref_instruction(fn_value);
1102 ir_ref_instruction(fn_value, irb->current_basic_block);
10971103
10981104 return &instruction->base;
10991105}
......@@ -1105,8 +1111,8 @@ static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, Scope *scope, AstN
11051111 instruction->fn_value = fn_value;
11061112 instruction->is_visible = is_visible;
11071113
1108 ir_ref_instruction(fn_value);
1109 ir_ref_instruction(is_visible);
1114 ir_ref_instruction(fn_value, irb->current_basic_block);
1115 ir_ref_instruction(is_visible, irb->current_basic_block);
11101116
11111117 return &instruction->base;
11121118}
......@@ -1118,8 +1124,8 @@ static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, Scope *scope, As
11181124 instruction->scope_value = scope_value;
11191125 instruction->debug_safety_on = debug_safety_on;
11201126
1121 ir_ref_instruction(scope_value);
1122 ir_ref_instruction(debug_safety_on);
1127 ir_ref_instruction(scope_value, irb->current_basic_block);
1128 ir_ref_instruction(debug_safety_on, irb->current_basic_block);
11231129
11241130 return &instruction->base;
11251131}
......@@ -1131,8 +1137,8 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode
11311137 instruction->size = size;
11321138 instruction->child_type = child_type;
11331139
1134 ir_ref_instruction(size);
1135 ir_ref_instruction(child_type);
1140 ir_ref_instruction(size, irb->current_basic_block);
1141 ir_ref_instruction(child_type, irb->current_basic_block);
11361142
11371143 return &instruction->base;
11381144}
......@@ -1144,7 +1150,7 @@ static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode
11441150 instruction->is_const = is_const;
11451151 instruction->child_type = child_type;
11461152
1147 ir_ref_instruction(child_type);
1153 ir_ref_instruction(child_type, irb->current_basic_block);
11481154
11491155 return &instruction->base;
11501156}
......@@ -1162,12 +1168,12 @@ static IrInstruction *ir_build_asm(IrBuilder *irb, Scope *scope, AstNode *source
11621168 assert(source_node->type == NodeTypeAsmExpr);
11631169 for (size_t i = 0; i < source_node->data.asm_expr.output_list.length; i += 1) {
11641170 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);
11661172 }
11671173
11681174 for (size_t i = 0; i < source_node->data.asm_expr.input_list.length; i += 1) {
11691175 IrInstruction *input_value = input_list[i];
1170 ir_ref_instruction(input_value);
1176 ir_ref_instruction(input_value, irb->current_basic_block);
11711177 }
11721178
11731179 return &instruction->base;
......@@ -1186,7 +1192,7 @@ static IrInstruction *ir_build_compile_var(IrBuilder *irb, Scope *scope, AstNode
11861192 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, scope, source_node);
11871193 instruction->name = name;
11881194
1189 ir_ref_instruction(name);
1195 ir_ref_instruction(name, irb->current_basic_block);
11901196
11911197 return &instruction->base;
11921198}
......@@ -1195,7 +1201,7 @@ static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *so
11951201 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);
11961202 instruction->type_value = type_value;
11971203
1198 ir_ref_instruction(type_value);
1204 ir_ref_instruction(type_value, irb->current_basic_block);
11991205
12001206 return &instruction->base;
12011207}
......@@ -1204,7 +1210,7 @@ static IrInstruction *ir_build_test_null(IrBuilder *irb, Scope *scope, AstNode *
12041210 IrInstructionTestNonNull *instruction = ir_build_instruction<IrInstructionTestNonNull>(irb, scope, source_node);
12051211 instruction->value = value;
12061212
1207 ir_ref_instruction(value);
1213 ir_ref_instruction(value, irb->current_basic_block);
12081214
12091215 return &instruction->base;
12101216}
......@@ -1225,7 +1231,7 @@ static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, Scope *scope, AstNod
12251231 instruction->value = value;
12261232 instruction->safety_check_on = safety_check_on;
12271233
1228 ir_ref_instruction(value);
1234 ir_ref_instruction(value, irb->current_basic_block);
12291235
12301236 return &instruction->base;
12311237}
......@@ -1243,7 +1249,7 @@ static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode
12431249 IrInstructionMaybeWrap *instruction = ir_build_instruction<IrInstructionMaybeWrap>(irb, scope, source_node);
12441250 instruction->value = value;
12451251
1246 ir_ref_instruction(value);
1252 ir_ref_instruction(value, irb->current_basic_block);
12471253
12481254 return &instruction->base;
12491255}
......@@ -1252,7 +1258,7 @@ static IrInstruction *ir_build_err_wrap_payload(IrBuilder *irb, Scope *scope, As
12521258 IrInstructionErrWrapPayload *instruction = ir_build_instruction<IrInstructionErrWrapPayload>(irb, scope, source_node);
12531259 instruction->value = value;
12541260
1255 ir_ref_instruction(value);
1261 ir_ref_instruction(value, irb->current_basic_block);
12561262
12571263 return &instruction->base;
12581264}
......@@ -1261,7 +1267,7 @@ static IrInstruction *ir_build_err_wrap_code(IrBuilder *irb, Scope *scope, AstNo
12611267 IrInstructionErrWrapCode *instruction = ir_build_instruction<IrInstructionErrWrapCode>(irb, scope, source_node);
12621268 instruction->value = value;
12631269
1264 ir_ref_instruction(value);
1270 ir_ref_instruction(value, irb->current_basic_block);
12651271
12661272 return &instruction->base;
12671273}
......@@ -1270,7 +1276,7 @@ static IrInstruction *ir_build_clz(IrBuilder *irb, Scope *scope, AstNode *source
12701276 IrInstructionClz *instruction = ir_build_instruction<IrInstructionClz>(irb, scope, source_node);
12711277 instruction->value = value;
12721278
1273 ir_ref_instruction(value);
1279 ir_ref_instruction(value, irb->current_basic_block);
12741280
12751281 return &instruction->base;
12761282}
......@@ -1285,7 +1291,7 @@ static IrInstruction *ir_build_ctz(IrBuilder *irb, Scope *scope, AstNode *source
12851291 IrInstructionCtz *instruction = ir_build_instruction<IrInstructionCtz>(irb, scope, source_node);
12861292 instruction->value = value;
12871293
1288 ir_ref_instruction(value);
1294 ir_ref_instruction(value, irb->current_basic_block);
12891295
12901296 return &instruction->base;
12911297}
......@@ -1308,12 +1314,12 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *
13081314 instruction->cases = cases;
13091315 instruction->is_comptime = is_comptime;
13101316
1311 ir_ref_instruction(target_value);
1312 if (is_comptime) ir_ref_instruction(is_comptime);
1317 ir_ref_instruction(target_value, irb->current_basic_block);
1318 if (is_comptime) ir_ref_instruction(is_comptime, irb->current_basic_block);
13131319 ir_ref_bb(else_block);
13141320
13151321 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);
13171323 ir_ref_bb(cases[i].block);
13181324 }
13191325
......@@ -1336,7 +1342,7 @@ static IrInstruction *ir_build_switch_target(IrBuilder *irb, Scope *scope, AstNo
13361342 IrInstructionSwitchTarget *instruction = ir_build_instruction<IrInstructionSwitchTarget>(irb, scope, source_node);
13371343 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
13411347 return &instruction->base;
13421348}
......@@ -1348,8 +1354,8 @@ static IrInstruction *ir_build_switch_var(IrBuilder *irb, Scope *scope, AstNode
13481354 instruction->target_value_ptr = target_value_ptr;
13491355 instruction->prong_value = prong_value;
13501356
1351 ir_ref_instruction(target_value_ptr);
1352 ir_ref_instruction(prong_value);
1357 ir_ref_instruction(target_value_ptr, irb->current_basic_block);
1358 ir_ref_instruction(prong_value, irb->current_basic_block);
13531359
13541360 return &instruction->base;
13551361}
......@@ -1358,7 +1364,7 @@ static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *s
13581364 IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, scope, source_node);
13591365 instruction->value = value;
13601366
1361 ir_ref_instruction(value);
1367 ir_ref_instruction(value, irb->current_basic_block);
13621368
13631369 return &instruction->base;
13641370}
......@@ -1374,7 +1380,7 @@ static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode
13741380 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, scope, source_node);
13751381 instruction->value = value;
13761382
1377 ir_ref_instruction(value);
1383 ir_ref_instruction(value, irb->current_basic_block);
13781384
13791385 return &instruction->base;
13801386}
......@@ -1383,7 +1389,7 @@ static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *sou
13831389 IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node);
13841390 instruction->name = name;
13851391
1386 ir_ref_instruction(name);
1392 ir_ref_instruction(name, irb->current_basic_block);
13871393
13881394 return &instruction->base;
13891395}
......@@ -1392,7 +1398,7 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *
13921398 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, scope, source_node);
13931399 instruction->array_value = array_value;
13941400
1395 ir_ref_instruction(array_value);
1401 ir_ref_instruction(array_value, irb->current_basic_block);
13961402
13971403 return &instruction->base;
13981404}
......@@ -1404,7 +1410,7 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source
14041410 instruction->value = value;
14051411 instruction->is_const = is_const;
14061412
1407 ir_ref_instruction(value);
1413 ir_ref_instruction(value, irb->current_basic_block);
14081414
14091415 return &instruction->base;
14101416}
......@@ -1422,7 +1428,7 @@ static IrInstruction *ir_build_min_value(IrBuilder *irb, Scope *scope, AstNode *
14221428 IrInstructionMinValue *instruction = ir_build_instruction<IrInstructionMinValue>(irb, scope, source_node);
14231429 instruction->value = value;
14241430
1425 ir_ref_instruction(value);
1431 ir_ref_instruction(value, irb->current_basic_block);
14261432
14271433 return &instruction->base;
14281434}
......@@ -1431,7 +1437,7 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *
14311437 IrInstructionMaxValue *instruction = ir_build_instruction<IrInstructionMaxValue>(irb, scope, source_node);
14321438 instruction->value = value;
14331439
1434 ir_ref_instruction(value);
1440 ir_ref_instruction(value, irb->current_basic_block);
14351441
14361442 return &instruction->base;
14371443}
......@@ -1440,7 +1446,7 @@ static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode
14401446 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
14411447 instruction->msg = msg;
14421448
1443 ir_ref_instruction(msg);
1449 ir_ref_instruction(msg, irb->current_basic_block);
14441450
14451451 return &instruction->base;
14461452}
......@@ -1449,7 +1455,7 @@ static IrInstruction *ir_build_err_name(IrBuilder *irb, Scope *scope, AstNode *s
14491455 IrInstructionErrName *instruction = ir_build_instruction<IrInstructionErrName>(irb, scope, source_node);
14501456 instruction->value = value;
14511457
1452 ir_ref_instruction(value);
1458 ir_ref_instruction(value, irb->current_basic_block);
14531459
14541460 return &instruction->base;
14551461}
......@@ -1470,7 +1476,7 @@ static IrInstruction *ir_build_c_include(IrBuilder *irb, Scope *scope, AstNode *
14701476 IrInstructionCInclude *instruction = ir_build_instruction<IrInstructionCInclude>(irb, scope, source_node);
14711477 instruction->name = name;
14721478
1473 ir_ref_instruction(name);
1479 ir_ref_instruction(name, irb->current_basic_block);
14741480
14751481 return &instruction->base;
14761482}
......@@ -1480,8 +1486,8 @@ static IrInstruction *ir_build_c_define(IrBuilder *irb, Scope *scope, AstNode *s
14801486 instruction->name = name;
14811487 instruction->value = value;
14821488
1483 ir_ref_instruction(name);
1484 ir_ref_instruction(value);
1489 ir_ref_instruction(name, irb->current_basic_block);
1490 ir_ref_instruction(value, irb->current_basic_block);
14851491
14861492 return &instruction->base;
14871493}
......@@ -1490,7 +1496,7 @@ static IrInstruction *ir_build_c_undef(IrBuilder *irb, Scope *scope, AstNode *so
14901496 IrInstructionCUndef *instruction = ir_build_instruction<IrInstructionCUndef>(irb, scope, source_node);
14911497 instruction->name = name;
14921498
1493 ir_ref_instruction(name);
1499 ir_ref_instruction(name, irb->current_basic_block);
14941500
14951501 return &instruction->base;
14961502}
......@@ -1499,7 +1505,7 @@ static IrInstruction *ir_build_embed_file(IrBuilder *irb, Scope *scope, AstNode
14991505 IrInstructionEmbedFile *instruction = ir_build_instruction<IrInstructionEmbedFile>(irb, scope, source_node);
15001506 instruction->name = name;
15011507
1502 ir_ref_instruction(name);
1508 ir_ref_instruction(name, irb->current_basic_block);
15031509
15041510 return &instruction->base;
15051511}
......@@ -1517,11 +1523,11 @@ static IrInstruction *ir_build_cmpxchg(IrBuilder *irb, Scope *scope, AstNode *so
15171523 instruction->success_order = success_order;
15181524 instruction->failure_order = failure_order;
15191525
1520 ir_ref_instruction(ptr);
1521 ir_ref_instruction(cmp_value);
1522 ir_ref_instruction(new_value);
1523 ir_ref_instruction(success_order_value);
1524 ir_ref_instruction(failure_order_value);
1526 ir_ref_instruction(ptr, irb->current_basic_block);
1527 ir_ref_instruction(cmp_value, irb->current_basic_block);
1528 ir_ref_instruction(new_value, irb->current_basic_block);
1529 ir_ref_instruction(success_order_value, irb->current_basic_block);
1530 ir_ref_instruction(failure_order_value, irb->current_basic_block);
15251531
15261532 return &instruction->base;
15271533}
......@@ -1541,7 +1547,7 @@ static IrInstruction *ir_build_fence(IrBuilder *irb, Scope *scope, AstNode *sour
15411547 instruction->order_value = order_value;
15421548 instruction->order = order;
15431549
1544 ir_ref_instruction(order_value);
1550 ir_ref_instruction(order_value, irb->current_basic_block);
15451551
15461552 return &instruction->base;
15471553}
......@@ -1557,8 +1563,8 @@ static IrInstruction *ir_build_div_exact(IrBuilder *irb, Scope *scope, AstNode *
15571563 instruction->op1 = op1;
15581564 instruction->op2 = op2;
15591565
1560 ir_ref_instruction(op1);
1561 ir_ref_instruction(op2);
1566 ir_ref_instruction(op1, irb->current_basic_block);
1567 ir_ref_instruction(op2, irb->current_basic_block);
15621568
15631569 return &instruction->base;
15641570}
......@@ -1574,8 +1580,8 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s
15741580 instruction->dest_type = dest_type;
15751581 instruction->target = target;
15761582
1577 ir_ref_instruction(dest_type);
1578 ir_ref_instruction(target);
1583 ir_ref_instruction(dest_type, irb->current_basic_block);
1584 ir_ref_instruction(target, irb->current_basic_block);
15791585
15801586 return &instruction->base;
15811587}
......@@ -1591,8 +1597,8 @@ static IrInstruction *ir_build_int_type(IrBuilder *irb, Scope *scope, AstNode *s
15911597 instruction->is_signed = is_signed;
15921598 instruction->bit_count = bit_count;
15931599
1594 ir_ref_instruction(is_signed);
1595 ir_ref_instruction(bit_count);
1600 ir_ref_instruction(is_signed, irb->current_basic_block);
1601 ir_ref_instruction(bit_count, irb->current_basic_block);
15961602
15971603 return &instruction->base;
15981604}
......@@ -1601,7 +1607,7 @@ static IrInstruction *ir_build_bool_not(IrBuilder *irb, Scope *scope, AstNode *s
16011607 IrInstructionBoolNot *instruction = ir_build_instruction<IrInstructionBoolNot>(irb, scope, source_node);
16021608 instruction->value = value;
16031609
1604 ir_ref_instruction(value);
1610 ir_ref_instruction(value, irb->current_basic_block);
16051611
16061612 return &instruction->base;
16071613}
......@@ -1619,8 +1625,8 @@ static IrInstruction *ir_build_alloca(IrBuilder *irb, Scope *scope, AstNode *sou
16191625 instruction->type_value = type_value;
16201626 instruction->count = count;
16211627
1622 ir_ref_instruction(type_value);
1623 ir_ref_instruction(count);
1628 ir_ref_instruction(type_value, irb->current_basic_block);
1629 ir_ref_instruction(count, irb->current_basic_block);
16241630
16251631 return &instruction->base;
16261632}
......@@ -1641,9 +1647,9 @@ static IrInstruction *ir_build_memset(IrBuilder *irb, Scope *scope, AstNode *sou
16411647 instruction->byte = byte;
16421648 instruction->count = count;
16431649
1644 ir_ref_instruction(dest_ptr);
1645 ir_ref_instruction(byte);
1646 ir_ref_instruction(count);
1650 ir_ref_instruction(dest_ptr, irb->current_basic_block);
1651 ir_ref_instruction(byte, irb->current_basic_block);
1652 ir_ref_instruction(count, irb->current_basic_block);
16471653
16481654 return &instruction->base;
16491655}
......@@ -1664,9 +1670,9 @@ static IrInstruction *ir_build_memcpy(IrBuilder *irb, Scope *scope, AstNode *sou
16641670 instruction->src_ptr = src_ptr;
16651671 instruction->count = count;
16661672
1667 ir_ref_instruction(dest_ptr);
1668 ir_ref_instruction(src_ptr);
1669 ir_ref_instruction(count);
1673 ir_ref_instruction(dest_ptr, irb->current_basic_block);
1674 ir_ref_instruction(src_ptr, irb->current_basic_block);
1675 ir_ref_instruction(count, irb->current_basic_block);
16701676
16711677 return &instruction->base;
16721678}
......@@ -1689,9 +1695,9 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour
16891695 instruction->is_const = is_const;
16901696 instruction->safety_check_on = safety_check_on;
16911697
1692 ir_ref_instruction(ptr);
1693 ir_ref_instruction(start);
1694 if (end) ir_ref_instruction(end);
1698 ir_ref_instruction(ptr, irb->current_basic_block);
1699 ir_ref_instruction(start, irb->current_basic_block);
1700 if (end) ir_ref_instruction(end, irb->current_basic_block);
16951701
16961702 return &instruction->base;
16971703}
......@@ -1709,7 +1715,7 @@ static IrInstruction *ir_build_member_count(IrBuilder *irb, Scope *scope, AstNod
17091715 IrInstructionMemberCount *instruction = ir_build_instruction<IrInstructionMemberCount>(irb, scope, source_node);
17101716 instruction->container = container;
17111717
1712 ir_ref_instruction(container);
1718 ir_ref_instruction(container, irb->current_basic_block);
17131719
17141720 return &instruction->base;
17151721}
......@@ -1759,10 +1765,10 @@ static IrInstruction *ir_build_overflow_op(IrBuilder *irb, Scope *scope, AstNode
17591765 instruction->result_ptr = result_ptr;
17601766 instruction->result_ptr_type = result_ptr_type;
17611767
1762 ir_ref_instruction(type_value);
1763 ir_ref_instruction(op1);
1764 ir_ref_instruction(op2);
1765 ir_ref_instruction(result_ptr);
1768 ir_ref_instruction(type_value, irb->current_basic_block);
1769 ir_ref_instruction(op1, irb->current_basic_block);
1770 ir_ref_instruction(op2, irb->current_basic_block);
1771 ir_ref_instruction(result_ptr, irb->current_basic_block);
17661772
17671773 return &instruction->base;
17681774}
......@@ -1781,7 +1787,7 @@ static IrInstruction *ir_build_alignof(IrBuilder *irb, Scope *scope, AstNode *so
17811787 IrInstructionAlignOf *instruction = ir_build_instruction<IrInstructionAlignOf>(irb, scope, source_node);
17821788 instruction->type_value = type_value;
17831789
1784 ir_ref_instruction(type_value);
1790 ir_ref_instruction(type_value, irb->current_basic_block);
17851791
17861792 return &instruction->base;
17871793}
......@@ -1792,7 +1798,7 @@ static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *s
17921798 IrInstructionTestErr *instruction = ir_build_instruction<IrInstructionTestErr>(irb, scope, source_node);
17931799 instruction->value = value;
17941800
1795 ir_ref_instruction(value);
1801 ir_ref_instruction(value, irb->current_basic_block);
17961802
17971803 return &instruction->base;
17981804}
......@@ -1810,7 +1816,7 @@ static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, Ast
18101816 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);
18111817 instruction->value = value;
18121818
1813 ir_ref_instruction(value);
1819 ir_ref_instruction(value, irb->current_basic_block);
18141820
18151821 return &instruction->base;
18161822}
......@@ -1831,7 +1837,7 @@ static IrInstruction *ir_build_unwrap_err_payload(IrBuilder *irb, Scope *scope,
18311837 instruction->value = value;
18321838 instruction->safety_check_on = safety_check_on;
18331839
1834 ir_ref_instruction(value);
1840 ir_ref_instruction(value, irb->current_basic_block);
18351841
18361842 return &instruction->base;
18371843}
......@@ -1854,9 +1860,9 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s
18541860
18551861 assert(source_node->type == NodeTypeFnProto);
18561862 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);
18581864 }
1859 ir_ref_instruction(return_type);
1865 ir_ref_instruction(return_type, irb->current_basic_block);
18601866
18611867 return &instruction->base;
18621868}
......@@ -1865,7 +1871,7 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo
18651871 IrInstructionTestComptime *instruction = ir_build_instruction<IrInstructionTestComptime>(irb, scope, source_node);
18661872 instruction->value = value;
18671873
1868 ir_ref_instruction(value);
1874 ir_ref_instruction(value, irb->current_basic_block);
18691875
18701876 return &instruction->base;
18711877}
......@@ -1878,7 +1884,7 @@ static IrInstruction *ir_build_init_enum(IrBuilder *irb, Scope *scope, AstNode *
18781884 instruction->field = field;
18791885 instruction->init_value = init_value;
18801886
1881 ir_ref_instruction(init_value);
1887 ir_ref_instruction(init_value, irb->current_basic_block);
18821888
18831889 return &instruction->base;
18841890}
......@@ -1899,7 +1905,7 @@ static IrInstruction *ir_build_pointer_reinterpret(IrBuilder *irb, Scope *scope,
18991905 irb, scope, source_node);
19001906 instruction->ptr = ptr;
19011907
1902 ir_ref_instruction(ptr);
1908 ir_ref_instruction(ptr, irb->current_basic_block);
19031909
19041910 return &instruction->base;
19051911}
......@@ -1911,7 +1917,7 @@ static IrInstruction *ir_build_widen_or_shorten(IrBuilder *irb, Scope *scope, As
19111917 irb, scope, source_node);
19121918 instruction->target = target;
19131919
1914 ir_ref_instruction(target);
1920 ir_ref_instruction(target, irb->current_basic_block);
19151921
19161922 return &instruction->base;
19171923}
......@@ -1923,7 +1929,7 @@ static IrInstruction *ir_build_int_to_ptr(IrBuilder *irb, Scope *scope, AstNode
19231929 irb, scope, source_node);
19241930 instruction->target = target;
19251931
1926 ir_ref_instruction(target);
1932 ir_ref_instruction(target, irb->current_basic_block);
19271933
19281934 return &instruction->base;
19291935}
......@@ -1935,7 +1941,7 @@ static IrInstruction *ir_build_ptr_to_int(IrBuilder *irb, Scope *scope, AstNode
19351941 irb, scope, source_node);
19361942 instruction->target = target;
19371943
1938 ir_ref_instruction(target);
1944 ir_ref_instruction(target, irb->current_basic_block);
19391945
19401946 return &instruction->base;
19411947}
......@@ -1947,7 +1953,7 @@ static IrInstruction *ir_build_int_to_enum(IrBuilder *irb, Scope *scope, AstNode
19471953 irb, scope, source_node);
19481954 instruction->target = target;
19491955
1950 ir_ref_instruction(target);
1956 ir_ref_instruction(target, irb->current_basic_block);
19511957
19521958 return &instruction->base;
19531959}
......@@ -2736,6 +2742,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
27362742 }
27372743}
27382744
2745static IrInstruction *ir_mark_gen(IrInstruction *instruction) {
2746 instruction->is_gen = true;
2747 return instruction;
2748}
2749
27392750static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
27402751 bool gen_error_defers, bool gen_maybe_defers)
27412752{
......@@ -2986,6 +2997,29 @@ static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *s
29862997 return var;
29872998}
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
29893023static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) {
29903024 assert(block_node->type == NodeTypeBlock);
29913025
......@@ -3001,6 +3035,43 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
30013035 IrInstruction *return_value = nullptr;
30023036 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
30033037 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
30043075 return_value = ir_gen_node(irb, statement_node, child_scope);
30053076 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
30063077 // defer starts a new scope
......@@ -3014,9 +3085,10 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
30143085 }
30153086
30163087 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
30213093 return return_value;
30223094}
......@@ -3167,7 +3239,8 @@ static IrInstruction *ir_gen_maybe_ok_or(IrBuilder *irb, Scope *parent_scope, As
31673239 if (null_result == irb->codegen->invalid_instruction)
31683240 return irb->codegen->invalid_instruction;
31693241 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
31723245 ir_set_cursor_at_end(irb, ok_block);
31733246 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
38863959 }
38873960
38883961 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));
38903963}
38913964
38923965static 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
39173990 if (then_expr_result == irb->codegen->invalid_instruction)
39183991 return then_expr_result;
39193992 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
39223996 ir_set_cursor_at_end(irb, else_block);
39233997 IrInstruction *else_expr_result;
......@@ -3929,7 +4003,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
39294003 else_expr_result = ir_build_const_void(irb, scope, node);
39304004 }
39314005 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
39344009 ir_set_cursor_at_end(irb, endif_block);
39354010 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
......@@ -4158,13 +4233,17 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
41584233
41594234 if (continue_expr_node) {
41604235 ir_set_cursor_at_end(irb, continue_block);
4161 ir_gen_node(irb, continue_expr_node, scope);
4162 ir_build_br(irb, scope, node, cond_block, is_comptime);
4236 IrInstruction *expr_result = ir_gen_node(irb, continue_expr_node, scope);
4237 if (!instr_is_unreachable(expr_result))
4238 ir_mark_gen(ir_build_br(irb, scope, node, cond_block, is_comptime));
41634239 }
41644240
41654241 ir_set_cursor_at_end(irb, cond_block);
41664242 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
41694248 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
41724251 loop_stack_item->break_block = end_block;
41734252 loop_stack_item->continue_block = continue_block;
41744253 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);
41764255 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));
41794259 ir_set_cursor_at_end(irb, end_block);
41804260
41814261 return ir_build_const_void(irb, scope, node);
......@@ -4270,10 +4350,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
42704350 loop_stack_item->break_block = end_block;
42714351 loop_stack_item->continue_block = continue_block;
42724352 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);
42744354 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
42784359 ir_set_cursor_at_end(irb, continue_block);
42794360 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 *
44574538 if (then_expr_result == irb->codegen->invalid_instruction)
44584539 return then_expr_result;
44594540 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
44624544 ir_set_cursor_at_end(irb, else_block);
44634545 IrInstruction *else_expr_result;
......@@ -4469,7 +4551,8 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *
44694551 else_expr_result = ir_build_const_void(irb, scope, node);
44704552 }
44714553 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
44744557 ir_set_cursor_at_end(irb, endif_block);
44754558 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
......@@ -4518,7 +4601,8 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit
45184601 IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope);
45194602 if (expr_result == irb->codegen->invalid_instruction)
45204603 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));
45224606 incoming_blocks->append(irb->current_basic_block);
45234607 incoming_values->append(expr_result);
45244608 return true;
......@@ -4684,57 +4768,6 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
46844768 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
46854769}
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
47384771static IrInstruction *ir_gen_goto(IrBuilder *irb, Scope *scope, AstNode *node) {
47394772 assert(node->type == NodeTypeGoto);
47404773
......@@ -4894,7 +4927,8 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN
48944927 if (err_result == irb->codegen->invalid_instruction)
48954928 return irb->codegen->invalid_instruction;
48964929 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
48994933 ir_set_cursor_at_end(irb, ok_block);
49004934 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
49895023 case NodeTypeSwitchProng:
49905024 case NodeTypeSwitchRange:
49915025 case NodeTypeStructField:
5026 case NodeTypeLabel:
49925027 zig_unreachable();
49935028 case NodeTypeBlock:
49945029 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
50405075 return ir_lval_wrap(irb, scope, ir_gen_if_var_expr(irb, scope, node), lval);
50415076 case NodeTypeSwitchExpr:
50425077 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);
50455078 case NodeTypeGoto:
50465079 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
50475080 case NodeTypeTypeLiteral:
......@@ -5130,7 +5163,7 @@ static bool ir_goto_pass2(IrBuilder *irb) {
51305163 return true;
51315164}
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) {
51345167 assert(node->owner);
51355168
51365169 IrBuilder ir_builder = {0};
......@@ -5146,20 +5179,21 @@ IrInstruction *ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutabl
51465179 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValPurposeNone);
51475180 assert(result);
51485181 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);
5152 assert(return_instruction);
5184 if (!instr_is_unreachable(result)) {
5185 ir_mark_gen(ir_build_return(irb, scope, result->source_node, result));
5186 }
51535187
51545188 if (!ir_goto_pass2(irb)) {
51555189 irb->exec->invalid = true;
5156 return codegen->invalid_instruction;
5190 return false;
51575191 }
51585192
5159 return return_instruction;
5193 return true;
51605194}
51615195
5162IrInstruction *ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {
5196bool ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {
51635197 assert(fn_entry);
51645198
51655199 IrExecutable *ir_executable = &fn_entry->ir_executable;
......@@ -5622,6 +5656,16 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons
56225656}
56235657
56245658static 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
56255669 ira->block_queue_index += 1;
56265670
56275671 if (ira->block_queue_index < ira->old_bb_queue.length) {
......@@ -5711,6 +5755,11 @@ static TypeTableEntry *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instr
57115755{
57125756 if (pointee_type->id == TypeTableEntryIdMetaType) {
57135757 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
57145763 ConstExprValue *const_val = ir_build_const_from(ira, instruction,
57155764 depends_on_compile_var || pointee->depends_on_compile_var);
57165765 type_ensure_zero_bits_known(ira->codegen, type_entry);
......@@ -7903,6 +7952,10 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
79037952
79047953 if (is_comptime || old_dest_block->ref_count == 1)
79057954 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);
79067959 }
79077960
79087961 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
src/ir.hpp+2-2
......@@ -10,8 +10,8 @@
1010
1111#include "all_types.hpp"
1212
13IrInstruction *ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable);
14IrInstruction *ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);
13bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable);
14bool ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);
1515
1616IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
src/parseh.cpp+2-1
......@@ -688,6 +688,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
688688 if (!enum_def) {
689689 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
690690 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
691 enum_type->data.enumeration.zero_bits_known = true;
691692 c->enum_type_table.put(bare_name, enum_type);
692693 c->decl_table.put(enum_decl, enum_type);
693694 replace_with_fwd_decl(c, enum_type, full_type_name);
......@@ -852,6 +853,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
852853
853854 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
854855 ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
856 struct_type->data.structure.zero_bits_known = true;
855857
856858 c->struct_type_table.put(bare_name, struct_type);
857859 c->decl_table.put(record_decl, struct_type);
......@@ -938,7 +940,6 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
938940
939941 struct_type->data.structure.gen_field_count = field_count;
940942 struct_type->data.structure.complete = true;
941 struct_type->data.structure.zero_bits_known = true;
942943
943944 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(c->codegen->target_data_ref, struct_type->type_ref);
944945 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() {
280280
281281 add_source_file(tc, "other.zig", R"SOURCE(
282282pub fn foo_function() -> bool {
283 @setFnStaticEval(this, false);
284
285283 // this one conflicts with the one from foo
286284 return true;
287285}
......@@ -503,7 +501,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
503501 } else if (*a_int > *b_int) {
504502 1
505503 } else {
506 0
504 c_int(0)
507505 }
508506}
509507
......@@ -539,21 +537,21 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
539537
540538 add_simple_case("incomplete struct parameter top level decl", R"SOURCE(
541539const io = @import("std").io;
542struct A {
540const A = struct {
543541 b: B,
544}
542};
545543
546struct B {
544const B = struct {
547545 c: C,
548}
546};
549547
550struct C {
548const C = struct {
551549 x: i32,
552550
553551 fn d(c: C) {
554552 %%io.stdout.printf("OK\n");
555553 }
556}
554};
557555
558556fn foo(a: A) {
559557 a.b.c.d();
......@@ -576,17 +574,17 @@ pub fn main(args: [][]u8) -> %void {
576574 add_simple_case("same named methods in incomplete struct", R"SOURCE(
577575const io = @import("std").io;
578576
579struct Foo {
577const Foo = struct {
580578 field1: Bar,
581579
582580 fn method(a: &Foo) -> bool { true }
583}
581};
584582
585struct Bar {
583const Bar = struct {
586584 field2: i32,
587585
588586 fn method(b: &Bar) -> bool { true }
589}
587};
590588
591589pub fn main(args: [][]u8) -> %void {
592590 const bar = Bar {.field2 = 13,};
......@@ -689,11 +687,11 @@ fn a() {}
689687
690688 add_compile_fail_case("unreachable with return", R"SOURCE(
691689fn 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
694692 add_compile_fail_case("control reaches end of non-void function", R"SOURCE(
695693fn 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
698696 add_compile_fail_case("undefined function call", R"SOURCE(
699697fn a() {
......@@ -706,7 +704,7 @@ fn a() {
706704 b(1);
707705}
708706fn 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
711709 add_compile_fail_case("invalid type", R"SOURCE(
712710fn a() -> bogus {}
......@@ -714,7 +712,7 @@ fn a() -> bogus {}
714712
715713 add_compile_fail_case("pointer to unreachable", R"SOURCE(
716714fn 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
719717 add_compile_fail_case("unreachable code", R"SOURCE(
720718fn a() {
......@@ -723,7 +721,7 @@ fn a() {
723721}
724722
725723fn b() {}
726 )SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");
724 )SOURCE", 1, ".tmp_source.zig:4:6: error: unreachable code");
727725
728726 add_compile_fail_case("bad import", R"SOURCE(
729727const bogus = @import("bogus-does-not-exist.zig");
......@@ -761,7 +759,7 @@ fn f() -> i32 {
761759 const a = c"a";
762760 a
763761}
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
766764 add_compile_fail_case("if condition is bool, not int", R"SOURCE(
767765fn f() {
......@@ -819,7 +817,7 @@ fn f() {
819817 )SOURCE", 4, ".tmp_source.zig:4:5: error: use of undeclared identifier 'i'",
820818 ".tmp_source.zig:4:7: error: use of undeclared identifier 'i'",
821819 ".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
824822 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
825823fn f(...) {}
......@@ -838,21 +836,21 @@ fn f(b: bool) {
838836 const x : i32 = if (b) { 1 };
839837 const y = if (b) { i32(1) };
840838}
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'",
842840 ".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");
843841
844842 add_compile_fail_case("direct struct loop", R"SOURCE(
845struct A { a : A, }
843const A = struct { a : A, };
846844 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");
847845
848846 add_compile_fail_case("indirect struct loop", R"SOURCE(
849struct A { b : B, }
850struct B { c : C, }
851struct C { a : A, }
847const A = struct { b : B, };
848const B = struct { c : C, };
849const C = struct { a : A, };
852850 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");
853851
854852 add_compile_fail_case("invalid struct field", R"SOURCE(
855struct A { x : i32, }
853const A = struct { x : i32, };
856854fn f() {
857855 var a : A = undefined;
858856 a.foo = 1;
......@@ -863,13 +861,13 @@ fn f() {
863861 ".tmp_source.zig:6:16: error: no member named 'bar' in 'A'");
864862
865863 add_compile_fail_case("redefinition of struct", R"SOURCE(
866struct A { x : i32, }
867struct A { y : i32, }
864const A = struct { x : i32, };
865const A = struct { y : i32, };
868866 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");
869867
870868 add_compile_fail_case("redefinition of enums", R"SOURCE(
871enum A {}
872enum A {}
869const A = enum {};
870const A = enum {};
873871 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");
874872
875873 add_compile_fail_case("redefinition of global variables", R"SOURCE(
......@@ -878,23 +876,23 @@ var a : i32 = 2;
878876 )SOURCE", 1, ".tmp_source.zig:3:1: error: redeclaration of variable 'a'");
879877
880878 add_compile_fail_case("byvalue struct parameter in exported function", R"SOURCE(
881struct A { x : i32, }
879const A = struct { x : i32, };
882880export fn f(a : A) {}
883881 )SOURCE", 1, ".tmp_source.zig:3:13: error: byvalue types not yet supported on extern function parameters");
884882
885883 add_compile_fail_case("byvalue struct return value in exported function", R"SOURCE(
886struct A { x: i32, }
884const A = struct { x: i32, };
887885export fn f() -> A {
888886 A {.x = 1234 }
889887}
890888 )SOURCE", 1, ".tmp_source.zig:3:18: error: byvalue types not yet supported on extern function return values");
891889
892890 add_compile_fail_case("duplicate field in struct value expression", R"SOURCE(
893struct A {
891const A = struct {
894892 x : i32,
895893 y : i32,
896894 z : i32,
897}
895};
898896fn f() {
899897 const a = A {
900898 .z = 1,
......@@ -906,11 +904,11 @@ fn f() {
906904 )SOURCE", 1, ".tmp_source.zig:12:9: error: duplicate field");
907905
908906 add_compile_fail_case("missing field in struct value expression", R"SOURCE(
909struct A {
907const A = struct {
910908 x : i32,
911909 y : i32,
912910 z : i32,
913}
911};
914912fn f() {
915913 // we want the error on the '{' not the 'A' because
916914 // the A could be a complicated expression
......@@ -922,11 +920,11 @@ fn f() {
922920 )SOURCE", 1, ".tmp_source.zig:10:17: error: missing field: 'x'");
923921
924922 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(
925struct A {
923const A = struct {
926924 x : i32,
927925 y : i32,
928926 z : i32,
929}
927};
930928fn f() {
931929 const a = A {
932930 .z = 4,
......@@ -983,8 +981,8 @@ var foo = u8;
983981 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");
984982
985983 add_compile_fail_case("variables shadowing types", R"SOURCE(
986struct Foo {}
987struct Bar {}
984const Foo = struct {};
985const Bar = struct {};
988986
989987fn f(Foo: i32) {
990988 var Bar : i32 = undefined;
......@@ -1014,7 +1012,7 @@ const x = foo();
10141012fn f(s: []u8) -> []u8 {
10151013 s ++ "foo"
10161014}
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
10191017 add_compile_fail_case("non compile time array concatenation", R"SOURCE(
10201018fn f(s: [10]u8) -> []u8 {
......@@ -1034,9 +1032,9 @@ const y = &x;
10341032
10351033 add_compile_fail_case("@typeOf number literal", R"SOURCE(
10361034const x = 3;
1037struct Foo {
1035const Foo = struct {
10381036 index: @typeOf(x),
1039}
1037};
10401038 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeOf");
10411039
10421040 add_compile_fail_case("integer overflow error", R"SOURCE(
......@@ -1048,7 +1046,7 @@ const x = 2 == 2.0;
10481046 )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");
10491047
10501048 add_compile_fail_case("missing function call param", R"SOURCE(
1051struct Foo {
1049const Foo = struct {
10521050 a: i32,
10531051 b: i32,
10541052
......@@ -1058,7 +1056,7 @@ struct Foo {
10581056 fn member_b(foo: Foo) -> i32 {
10591057 return foo.b;
10601058 }
1061}
1059};
10621060
10631061const member_fn_type = @typeOf(Foo.member_a);
10641062const members = []member_fn_type {
......@@ -1069,7 +1067,7 @@ const members = []member_fn_type {
10691067fn f(foo: Foo, index: i32) {
10701068 const result = members[index]();
10711069}
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
10741072 add_compile_fail_case("missing function name and param name", R"SOURCE(
10751073fn () {}
......@@ -1084,22 +1082,22 @@ fn a() -> i32 {0}
10841082fn b() -> i32 {1}
10851083fn c() -> i32 {2}
10861084 )SOURCE", 3,
1087 ".tmp_source.zig:2:21: error: expected type 'fn()', got 'fn() -> i32'",
1088 ".tmp_source.zig:2:24: error: expected type 'fn()', got 'fn() -> i32'",
1089 ".tmp_source.zig:2:27: error: expected type 'fn()', got 'fn() -> i32'");
1085 ".tmp_source.zig:2:21: error: expected type 'fn()', found 'fn() -> i32'",
1086 ".tmp_source.zig:2:24: error: expected type 'fn()', found 'fn() -> i32'",
1087 ".tmp_source.zig:2:27: error: expected type 'fn()', found 'fn() -> i32'");
10901088
10911089 add_compile_fail_case("extern function pointer mismatch", R"SOURCE(
10921090const fns = [](fn(i32)->i32){ a, b, c };
10931091pub fn a(x: i32) -> i32 {x + 0}
10941092pub fn b(x: i32) -> i32 {x + 1}
10951093export 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
10991097 add_compile_fail_case("implicit cast from f64 to f32", R"SOURCE(
11001098const x : f64 = 1.0;
11011099const 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
11051103 add_compile_fail_case("colliding invalid top level functions", R"SOURCE(
......@@ -1122,9 +1120,9 @@ fn a(x: i32) {
11221120 )SOURCE", 1, ".tmp_source.zig:3:26: error: unable to evaluate constant expression");
11231121
11241122 add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(
1125struct Foo {
1123const Foo = struct {
11261124 y: [get()]u8,
1127}
1125};
11281126var global_var: usize = 1;
11291127fn get() -> usize { global_var }
11301128 )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");
......@@ -1138,9 +1136,9 @@ fn f() {
11381136
11391137
11401138 add_compile_fail_case("addition with non numbers", R"SOURCE(
1141struct Foo {
1139const Foo = struct {
11421140 field: i32,
1143}
1141};
11441142const x = Foo {.field = 1} + Foo {.field = 2};
11451143 )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);
11581156
11591157
11601158 add_compile_fail_case("missing switch prong", R"SOURCE(
1161enum Number {
1159const Number = enum {
11621160 One,
11631161 Two,
11641162 Three,
11651163 Four,
1166}
1164};
11671165fn f(n: Number) -> i32 {
11681166 switch (n) {
11691167 One => 1,
......@@ -1221,7 +1219,7 @@ fn derp(){}
12211219
12221220 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
12231221const 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
12261224 add_compile_fail_case("indexing an array of size zero", R"SOURCE(
12271225const array = []u8{};
......@@ -1261,22 +1259,23 @@ const resource = @embedFile("bogus.txt");
12611259
12621260
12631261 add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE(
1264struct Foo {
1262const Foo = {
12651263 x: i32,
1266}
1264};
12671265const a = Foo {.x = get_it()};
12681266extern fn get_it() -> i32;
12691267 )SOURCE", 1, ".tmp_source.zig:5:27: error: unable to evaluate constant expression");
12701268
12711269 add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(
1272struct Foo {
1270const Foo = {
12731271 x: i32,
1274}
1272};
12751273const a = get_it();
12761274fn get_it() -> Foo {
1277 @setFnStaticEval(this, false);
1275 global_side_effect = true;
12781276 Foo {.x = 13}
12791277}
1278var global_side_effect = false;
12801279
12811280 )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");
12821281
......@@ -1293,10 +1292,10 @@ fn test_a_thing() {
12931292fn bad_eql_1(a: []u8, b: []u8) -> bool {
12941293 a == b
12951294}
1296enum EnumWithData {
1295const EnumWithData = enum {
12971296 One,
12981297 Two: i32,
1299}
1298};
13001299fn bad_eql_2(a: EnumWithData, b: EnumWithData) -> bool {
13011300 a == b
13021301}
......@@ -1313,7 +1312,6 @@ fn foo() {
13131312 };
13141313}
13151314fn bar() -> i32 {
1316 @setFnStaticEval(this, false);
13171315 2
13181316}
13191317
......@@ -1383,7 +1381,7 @@ fn f() {
13831381 const x: u32 = 10;
13841382 @truncate(i8, x);
13851383}
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
13881386 add_compile_fail_case("truncate same bit count", R"SOURCE(
13891387fn f() {
......@@ -1474,14 +1472,14 @@ fn f(m: []const u8) {
14741472 )SOURCE", 1, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");
14751473
14761474 add_compile_fail_case("wrong number of arguments for method fn call", R"SOURCE(
1477struct Foo {
1475const Foo = {
14781476 fn method(self: &const Foo, a: i32) {}
1479}
1477};
14801478fn f(foo: &const Foo) {
14811479
14821480 foo.method(1, 2);
14831481}
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
14861484 add_compile_fail_case("assign through constant pointer", R"SOURCE(
14871485fn f() {
......@@ -1511,12 +1509,12 @@ fn foo(blah: []u8) {
15111509const JasonHM = u8;
15121510const JasonList = &JsonNode;
15131511
1514enum JsonOA {
1512const JsonOA = enum {
15151513 JSONArray: JsonList,
15161514 JSONObject: JasonHM,
1517}
1515};
15181516
1519enum JsonType {
1517const JsonType = enum {
15201518 JSONNull: void,
15211519 JSONInteger: isize,
15221520 JSONDouble: f64,
......@@ -1524,7 +1522,7 @@ enum JsonType {
15241522 JSONString: []u8,
15251523 JSONArray,
15261524 JSONObject,
1527}
1525};
15281526
15291527pub struct JsonNode {
15301528 kind: JsonType,
......@@ -1541,7 +1539,7 @@ fn foo() {
15411539 ".tmp_source.zig:27:8: error: no member named 'init' in 'JsonNode'");
15421540
15431541 add_compile_fail_case("method call with first arg type primitive", R"SOURCE(
1544struct Foo {
1542const Foo = {
15451543 x: i32,
15461544
15471545 fn init(x: i32) -> Foo {
......@@ -1549,7 +1547,7 @@ struct Foo {
15491547 .x = x,
15501548 }
15511549 }
1552}
1550};
15531551
15541552fn f() {
15551553 const derp = Foo.init(3);
......@@ -1622,14 +1620,9 @@ pub fn main(args: [][]u8) -> %void {
16221620 baz(bar(a));
16231621}
16241622fn bar(a: []i32) -> i32 {
1625 @setFnStaticEval(this, false);
1626
16271623 a[4]
16281624}
1629fn baz(a: i32) {
1630 @setFnStaticEval(this, false);
1631}
1632
1625fn baz(a: i32) { }
16331626 )SOURCE");
16341627
16351628 add_debug_safety_case("integer addition overflow", R"SOURCE(
......@@ -1639,8 +1632,6 @@ pub fn main(args: [][]u8) -> %void {
16391632 if (x == 0) return error.Whatever;
16401633}
16411634fn add(a: u16, b: u16) -> u16 {
1642 @setFnStaticEval(this, false);
1643
16441635 a + b
16451636}
16461637 )SOURCE");
......@@ -1652,8 +1643,6 @@ pub fn main(args: [][]u8) -> %void {
16521643 if (x == 0) return error.Whatever;
16531644}
16541645fn sub(a: u16, b: u16) -> u16 {
1655 @setFnStaticEval(this, false);
1656
16571646 a - b
16581647}
16591648 )SOURCE");
......@@ -1665,8 +1654,6 @@ pub fn main(args: [][]u8) -> %void {
16651654 if (x == 0) return error.Whatever;
16661655}
16671656fn mul(a: u16, b: u16) -> u16 {
1668 @setFnStaticEval(this, false);
1669
16701657 a * b
16711658}
16721659 )SOURCE");
......@@ -1678,8 +1665,6 @@ pub fn main(args: [][]u8) -> %void {
16781665 if (x == 0) return error.Whatever;
16791666}
16801667fn neg(a: i16) -> i16 {
1681 @setFnStaticEval(this, false);
1682
16831668 -a
16841669}
16851670 )SOURCE");
......@@ -1691,8 +1676,6 @@ pub fn main(args: [][]u8) -> %void {
16911676 if (x == 0) return error.Whatever;
16921677}
16931678fn shl(a: i16, b: i16) -> i16 {
1694 @setFnStaticEval(this, false);
1695
16961679 a << b
16971680}
16981681 )SOURCE");
......@@ -1704,8 +1687,6 @@ pub fn main(args: [][]u8) -> %void {
17041687 if (x == 0) return error.Whatever;
17051688}
17061689fn shl(a: u16, b: u16) -> u16 {
1707 @setFnStaticEval(this, false);
1708
17091690 a << b
17101691}
17111692 )SOURCE");
......@@ -1716,8 +1697,6 @@ pub fn main(args: [][]u8) -> %void {
17161697 const x = div0(999, 0);
17171698}
17181699fn div0(a: i32, b: i32) -> i32 {
1719 @setFnStaticEval(this, false);
1720
17211700 a / b
17221701}
17231702 )SOURCE");
......@@ -1729,8 +1708,6 @@ pub fn main(args: [][]u8) -> %void {
17291708 if (x == 0) return error.Whatever;
17301709}
17311710fn divExact(a: i32, b: i32) -> i32 {
1732 @setFnStaticEval(this, false);
1733
17341711 @divExact(a, b)
17351712}
17361713 )SOURCE");
......@@ -1742,8 +1719,6 @@ pub fn main(args: [][]u8) -> %void {
17421719 if (x.len == 0) return error.Whatever;
17431720}
17441721fn widenSlice(slice: []u8) -> []i32 {
1745 @setFnStaticEval(this, false);
1746
17471722 ([]i32)(slice)
17481723}
17491724 )SOURCE");
......@@ -1755,8 +1730,6 @@ pub fn main(args: [][]u8) -> %void {
17551730 if (x == 0) return error.Whatever;
17561731}
17571732fn shorten_cast(x: i32) -> i8 {
1758 @setFnStaticEval(this, false);
1759
17601733 i8(x)
17611734}
17621735 )SOURCE");
......@@ -1768,8 +1741,6 @@ pub fn main(args: [][]u8) -> %void {
17681741 if (x == 0) return error.Whatever;
17691742}
17701743fn unsigned_cast(x: i32) -> u32 {
1771 @setFnStaticEval(this, false);
1772
17731744 u32(x)
17741745}
17751746 )SOURCE");