authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-09 14:52:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-09 14:52:06-04:00
log010963ce431db2648374368dfc886aa8482494a9
tree5779df01f7f570e9adf8c9e56b7d9217e25a1bf3
parentc459edac18a53854dbfdebe50a370d169e715145
signaturelock-open Commit is signed but in an unrecognized format.

stage1: make some asserts print source location


3 files changed, 46 insertions(+), 25 deletions(-)

src/analyze.cpp+13
...@@ -7230,3 +7230,16 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {...@@ -7230,3 +7230,16 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
7230 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));7230 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
7231 return type->llvm_di_type;7231 return type->llvm_di_type;
7232}7232}
7233
7234void src_assert(bool ok, AstNode *source_node) {
7235 if (ok) return;
7236 if (source_node == nullptr) {
7237 fprintf(stderr, "when analyzing (unknown source location): ");
7238 } else {
7239 fprintf(stderr, "when analyzing %s:%u:%u: ",
7240 buf_ptr(source_node->owner->data.structure.root_struct->path),
7241 (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
7242 }
7243 const char *msg = "assertion failed";
7244 stage2_panic(msg, strlen(msg));
7245}
src/analyze.hpp+2
...@@ -249,4 +249,6 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);...@@ -249,4 +249,6 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
249249
250void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c);250void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c);
251251
252void src_assert(bool ok, AstNode *source_node);
253
252#endif254#endif
src/ir.cpp+31-25
...@@ -7933,6 +7933,11 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction,...@@ -7933,6 +7933,11 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction,
7933 return ir_add_error_node(ira, source_instruction->source_node, msg);7933 return ir_add_error_node(ira, source_instruction->source_node, msg);
7934}7934}
79357935
7936static void ir_assert(bool ok, IrInstruction *source_instruction) {
7937 if (ok) return;
7938 src_assert(ok, source_instruction->source_node);
7939}
7940
7936// This function takes a comptime ptr and makes the child const value conform to the type7941// This function takes a comptime ptr and makes the child const value conform to the type
7937// described by the pointer.7942// described by the pointer.
7938static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,7943static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
...@@ -13944,11 +13949,12 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i...@@ -13944,11 +13949,12 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i
13944 zig_unreachable();13949 zig_unreachable();
13945}13950}
1394613951
13947static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, ZigFn *fn_entry, ZigType *fn_type,13952static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, ZigFn *fn_entry,
13948 IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst)13953 ZigType *fn_type, IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count,
13954 IrInstruction *async_allocator_inst)
13949{13955{
13950 Buf *realloc_field_name = buf_create_from_str(ASYNC_REALLOC_FIELD_NAME);13956 Buf *realloc_field_name = buf_create_from_str(ASYNC_REALLOC_FIELD_NAME);
13951 assert(async_allocator_inst->value.type->id == ZigTypeIdPointer);13957 ir_assert(async_allocator_inst->value.type->id == ZigTypeIdPointer, &call_instruction->base);
13952 ZigType *container_type = async_allocator_inst->value.type->data.pointer.child_type;13958 ZigType *container_type = async_allocator_inst->value.type->data.pointer.child_type;
13953 IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, realloc_field_name, &call_instruction->base,13959 IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, realloc_field_name, &call_instruction->base,
13954 async_allocator_inst, container_type);13960 async_allocator_inst, container_type);
...@@ -13956,7 +13962,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c...@@ -13956,7 +13962,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c
13956 return ira->codegen->invalid_instruction;13962 return ira->codegen->invalid_instruction;
13957 }13963 }
13958 ZigType *ptr_to_realloc_fn_type = field_ptr_inst->value.type;13964 ZigType *ptr_to_realloc_fn_type = field_ptr_inst->value.type;
13959 assert(ptr_to_realloc_fn_type->id == ZigTypeIdPointer);13965 ir_assert(ptr_to_realloc_fn_type->id == ZigTypeIdPointer, &call_instruction->base);
1396013966
13961 ZigType *realloc_fn_type = ptr_to_realloc_fn_type->data.pointer.child_type;13967 ZigType *realloc_fn_type = ptr_to_realloc_fn_type->data.pointer.child_type;
13962 if (realloc_fn_type->id != ZigTypeIdFn) {13968 if (realloc_fn_type->id != ZigTypeIdFn) {
...@@ -21227,10 +21233,10 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -21227,10 +21233,10 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
21227 if (type_is_invalid(end_value->value.type))21233 if (type_is_invalid(end_value->value.type))
21228 return ira->codegen->invalid_instruction;21234 return ira->codegen->invalid_instruction;
2122921235
21230 assert(start_value->value.type->id == ZigTypeIdErrorSet);21236 ir_assert(start_value->value.type->id == ZigTypeIdErrorSet, &instruction->base);
21231 uint32_t start_index = start_value->value.data.x_err_set->value;21237 uint32_t start_index = start_value->value.data.x_err_set->value;
2123221238
21233 assert(end_value->value.type->id == ZigTypeIdErrorSet);21239 ir_assert(end_value->value.type->id == ZigTypeIdErrorSet, &instruction->base);
21234 uint32_t end_index = end_value->value.data.x_err_set->value;21240 uint32_t end_index = end_value->value.data.x_err_set->value;
2123521241
21236 if (start_index != end_index) {21242 if (start_index != end_index) {
...@@ -21755,7 +21761,7 @@ static Error buf_read_value_bytes_array(IrAnalyze *ira, CodeGen *codegen, AstNod...@@ -21755,7 +21761,7 @@ static Error buf_read_value_bytes_array(IrAnalyze *ira, CodeGen *codegen, AstNod
2175521761
21756static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val) {21762static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val) {
21757 Error err;21763 Error err;
21758 assert(val->special == ConstValSpecialStatic);21764 src_assert(val->special == ConstValSpecialStatic, source_node);
21759 switch (val->type->id) {21765 switch (val->type->id) {
21760 case ZigTypeIdInvalid:21766 case ZigTypeIdInvalid:
21761 case ZigTypeIdMetaType:21767 case ZigTypeIdMetaType:
...@@ -21805,7 +21811,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21805,7 +21811,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21805 zig_panic("TODO buf_read_value_bytes enum packed");21811 zig_panic("TODO buf_read_value_bytes enum packed");
21806 case ContainerLayoutExtern: {21812 case ContainerLayoutExtern: {
21807 ZigType *tag_int_type = val->type->data.enumeration.tag_int_type;21813 ZigType *tag_int_type = val->type->data.enumeration.tag_int_type;
21808 assert(tag_int_type->id == ZigTypeIdInt);21814 src_assert(tag_int_type->id == ZigTypeIdInt, source_node);
21809 bigint_read_twos_complement(&val->data.x_enum_tag, buf, tag_int_type->data.integral.bit_count,21815 bigint_read_twos_complement(&val->data.x_enum_tag, buf, tag_int_type->data.integral.bit_count,
21810 codegen->is_big_endian, tag_int_type->data.integral.is_signed);21816 codegen->is_big_endian, tag_int_type->data.integral.is_signed);
21811 return ErrorNone;21817 return ErrorNone;
...@@ -21860,7 +21866,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21860,7 +21866,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21860 bigint_read_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian, false);21866 bigint_read_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian, false);
21861 while (src_i < src_field_count) {21867 while (src_i < src_field_count) {
21862 TypeStructField *field = &val->type->data.structure.fields[src_i];21868 TypeStructField *field = &val->type->data.structure.fields[src_i];
21863 assert(field->gen_index != SIZE_MAX);21869 src_assert(field->gen_index != SIZE_MAX, source_node);
21864 if (field->gen_index != gen_i)21870 if (field->gen_index != gen_i)
21865 break;21871 break;
21866 ConstExprValue *field_val = &val->data.x_struct.fields[src_i];21872 ConstExprValue *field_val = &val->data.x_struct.fields[src_i];
...@@ -21936,10 +21942,10 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_...@@ -21936,10 +21942,10 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_
21936 Error err;21942 Error err;
2193721943
21938 ZigType *src_type = value->value.type;21944 ZigType *src_type = value->value.type;
21939 assert(get_codegen_ptr_type(src_type) == nullptr);21945 ir_assert(get_codegen_ptr_type(src_type) == nullptr, source_instr);
21940 assert(type_can_bit_cast(src_type));21946 ir_assert(type_can_bit_cast(src_type), source_instr);
21941 assert(get_codegen_ptr_type(dest_type) == nullptr);21947 ir_assert(get_codegen_ptr_type(dest_type) == nullptr, source_instr);
21942 assert(type_can_bit_cast(dest_type));21948 ir_assert(type_can_bit_cast(dest_type), source_instr);
2194321949
21944 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusSizeKnown)))21950 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusSizeKnown)))
21945 return ira->codegen->invalid_instruction;21951 return ira->codegen->invalid_instruction;
...@@ -22029,8 +22035,8 @@ static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruct...@@ -22029,8 +22035,8 @@ static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruct
22029static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,22035static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
22030 ZigType *ptr_type)22036 ZigType *ptr_type)
22031{22037{
22032 assert(get_src_ptr_type(ptr_type) != nullptr);22038 ir_assert(get_src_ptr_type(ptr_type) != nullptr, source_instr);
22033 assert(type_has_bits(ptr_type));22039 ir_assert(type_has_bits(ptr_type), source_instr);
2203422040
22035 IrInstruction *casted_int = ir_implicit_cast(ira, target, ira->codegen->builtin_types.entry_usize);22041 IrInstruction *casted_int = ir_implicit_cast(ira, target, ira->codegen->builtin_types.entry_usize);
22036 if (type_is_invalid(casted_int->value.type))22042 if (type_is_invalid(casted_int->value.type))
...@@ -22129,7 +22135,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,...@@ -22129,7 +22135,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
22129 case TldIdFn: {22135 case TldIdFn: {
22130 TldFn *tld_fn = (TldFn *)tld;22136 TldFn *tld_fn = (TldFn *)tld;
22131 ZigFn *fn_entry = tld_fn->fn_entry;22137 ZigFn *fn_entry = tld_fn->fn_entry;
22132 assert(fn_entry->type_entry);22138 ir_assert(fn_entry->type_entry, &instruction->base);
2213322139
22134 if (tld_fn->extern_lib_name != nullptr) {22140 if (tld_fn->extern_lib_name != nullptr) {
22135 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, instruction->base.source_node);22141 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, instruction->base.source_node);
...@@ -22327,7 +22333,7 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct...@@ -22327,7 +22333,7 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct
22327 ZigType *result_type = fn_type_id->param_info[arg_index].type;22333 ZigType *result_type = fn_type_id->param_info[arg_index].type;
22328 if (result_type == nullptr) {22334 if (result_type == nullptr) {
22329 // Args are only unresolved if our function is generic.22335 // Args are only unresolved if our function is generic.
22330 assert(fn_type->data.fn.is_generic);22336 ir_assert(fn_type->data.fn.is_generic, &instruction->base);
2233122337
22332 ir_add_error(ira, arg_index_inst,22338 ir_add_error(ira, arg_index_inst,
22333 buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_u64 " because '%s' is generic",22339 buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_u64 " because '%s' is generic",
...@@ -22413,7 +22419,7 @@ static IrInstruction *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstru...@@ -22413,7 +22419,7 @@ static IrInstruction *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstru
22413 return ira->codegen->invalid_instruction;22419 return ira->codegen->invalid_instruction;
2241422420
22415 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);22421 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
22416 assert(fn_entry != nullptr);22422 ir_assert(fn_entry != nullptr, &instruction->base);
22417 IrInstruction *result = ir_build_coro_begin(&ira->new_irb, instruction->base.scope, instruction->base.source_node,22423 IrInstruction *result = ir_build_coro_begin(&ira->new_irb, instruction->base.scope, instruction->base.source_node,
22418 coro_id, coro_mem_ptr);22424 coro_id, coro_mem_ptr);
22419 result->value.type = get_promise_type(ira->codegen, fn_entry->type_entry->data.fn.fn_type_id.return_type);22425 result->value.type = get_promise_type(ira->codegen, fn_entry->type_entry->data.fn.fn_type_id.return_type);
...@@ -22651,7 +22657,7 @@ static IrInstruction *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstr...@@ -22651,7 +22657,7 @@ static IrInstruction *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstr
22651 }22657 }
2265222658
22653 if (ordering == AtomicOrderRelease || ordering == AtomicOrderAcqRel) {22659 if (ordering == AtomicOrderRelease || ordering == AtomicOrderAcqRel) {
22654 assert(instruction->ordering != nullptr);22660 ir_assert(instruction->ordering != nullptr, &instruction->base);
22655 ir_add_error(ira, instruction->ordering,22661 ir_add_error(ira, instruction->ordering,
22656 buf_sprintf("@atomicLoad atomic ordering must not be Release or AcqRel"));22662 buf_sprintf("@atomicLoad atomic ordering must not be Release or AcqRel"));
22657 return ira->codegen->invalid_instruction;22663 return ira->codegen->invalid_instruction;
...@@ -22659,7 +22665,7 @@ static IrInstruction *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstr...@@ -22659,7 +22665,7 @@ static IrInstruction *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstr
2265922665
22660 if (instr_is_comptime(casted_ptr)) {22666 if (instr_is_comptime(casted_ptr)) {
22661 IrInstruction *result = ir_get_deref(ira, &instruction->base, casted_ptr);22667 IrInstruction *result = ir_get_deref(ira, &instruction->base, casted_ptr);
22662 assert(result->value.type != nullptr);22668 ir_assert(result->value.type != nullptr, &instruction->base);
22663 return result;22669 return result;
22664 }22670 }
2266522671
...@@ -22689,7 +22695,7 @@ static IrInstruction *ir_analyze_instruction_await_bookkeeping(IrAnalyze *ira, I...@@ -22689,7 +22695,7 @@ static IrInstruction *ir_analyze_instruction_await_bookkeeping(IrAnalyze *ira, I
22689 return ira->codegen->invalid_instruction;22695 return ira->codegen->invalid_instruction;
2269022696
22691 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);22697 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
22692 assert(fn_entry != nullptr);22698 ir_assert(fn_entry != nullptr, &instruction->base);
2269322699
22694 if (type_can_fail(promise_result_type)) {22700 if (type_can_fail(promise_result_type)) {
22695 fn_entry->calls_or_awaits_errorable_fn = true;22701 fn_entry->calls_or_awaits_errorable_fn = true;
...@@ -22705,9 +22711,9 @@ static IrInstruction *ir_analyze_instruction_merge_err_ret_traces(IrAnalyze *ira...@@ -22705,9 +22711,9 @@ static IrInstruction *ir_analyze_instruction_merge_err_ret_traces(IrAnalyze *ira
22705 if (type_is_invalid(coro_promise_ptr->value.type))22711 if (type_is_invalid(coro_promise_ptr->value.type))
22706 return ira->codegen->invalid_instruction;22712 return ira->codegen->invalid_instruction;
2270722713
22708 assert(coro_promise_ptr->value.type->id == ZigTypeIdPointer);22714 ir_assert(coro_promise_ptr->value.type->id == ZigTypeIdPointer, &instruction->base);
22709 ZigType *promise_frame_type = coro_promise_ptr->value.type->data.pointer.child_type;22715 ZigType *promise_frame_type = coro_promise_ptr->value.type->data.pointer.child_type;
22710 assert(promise_frame_type->id == ZigTypeIdStruct);22716 ir_assert(promise_frame_type->id == ZigTypeIdStruct, &instruction->base);
22711 ZigType *promise_result_type = promise_frame_type->data.structure.fields[1].type_entry;22717 ZigType *promise_result_type = promise_frame_type->data.structure.fields[1].type_entry;
2271222718
22713 if (!type_can_fail(promise_result_type)) {22719 if (!type_can_fail(promise_result_type)) {
...@@ -22799,7 +22805,7 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS...@@ -22799,7 +22805,7 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS
22799 return result;22805 return result;
22800 }22806 }
2280122807
22802 assert(float_type->id == ZigTypeIdFloat);22808 ir_assert(float_type->id == ZigTypeIdFloat, &instruction->base);
22803 if (float_type->data.floating.bit_count != 16 &&22809 if (float_type->data.floating.bit_count != 16 &&
22804 float_type->data.floating.bit_count != 32 &&22810 float_type->data.floating.bit_count != 32 &&
22805 float_type->data.floating.bit_count != 64) {22811 float_type->data.floating.bit_count != 64) {
...@@ -23290,7 +23296,7 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio...@@ -23290,7 +23296,7 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2329023296
23291static IrInstruction *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *old_instruction) {23297static IrInstruction *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *old_instruction) {
23292 IrInstruction *new_instruction = ir_analyze_instruction_nocast(ira, old_instruction);23298 IrInstruction *new_instruction = ir_analyze_instruction_nocast(ira, old_instruction);
23293 assert(new_instruction->value.type != nullptr);23299 ir_assert(new_instruction->value.type != nullptr, old_instruction);
23294 old_instruction->child = new_instruction;23300 old_instruction->child = new_instruction;
23295 return new_instruction;23301 return new_instruction;
23296}23302}