authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-22 18:24:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-22 18:24:15-04:00
log79a4b7a2365dc50d01eb6bc29bbb77244a1620cf
tree2e73c9b358e39e1102c4a51614e56bd2ed7e894a
parent26b79ac90ee8cf17f28b0584c773be759e0e49bd
signaturelock-open Commit is signed but in an unrecognized format.

fix regressions


2 files changed, 254 insertions(+), 174 deletions(-)

src/analyze.cpp+16-3
......@@ -1018,10 +1018,14 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10181018 zig_unreachable();
10191019 case LazyValueIdSliceType: {
10201020 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(type_val->data.x_lazy);
1021 if (type_is_invalid(lazy_slice_type->elem_type))
1022 return ReqCompTimeInvalid;
10211023 return type_requires_comptime(g, lazy_slice_type->elem_type, parent_type);
10221024 }
10231025 case LazyValueIdPtrType: {
10241026 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
1027 if (type_is_invalid(lazy_ptr_type->elem_type))
1028 return ReqCompTimeInvalid;
10251029 return type_requires_comptime(g, lazy_ptr_type->elem_type, parent_type);
10261030 }
10271031 }
......@@ -2239,7 +2243,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
22392243 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
22402244 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
22412245 g->trace_err = add_node_error(g, decl_node,
2242 buf_sprintf("dependency loop: whether struct '%s' has non-zero size",
2246 buf_sprintf("struct '%s' depends on itself",
22432247 buf_ptr(&struct_type->name)));
22442248 }
22452249 return ErrorSemanticAnalyzeFail;
......@@ -2308,6 +2312,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
23082312 struct_type->data.structure.requires_comptime = true;
23092313 break;
23102314 case ReqCompTimeInvalid:
2315 if (g->trace_err != nullptr) {
2316 g->trace_err = add_error_note(g, g->trace_err, field_node,
2317 buf_create_from_str("while checking this field"));
2318 }
23112319 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
23122320 return ErrorSemanticAnalyzeFail;
23132321 case ReqCompTimeNo:
......@@ -2770,6 +2778,8 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node) {
27702778 fn_entry->body_node = (proto_node->data.fn_proto.fn_def_node == nullptr) ? nullptr :
27712779 proto_node->data.fn_proto.fn_def_node->data.fn_def.body;
27722780
2781 fn_entry->analyzed_executable.source_node = fn_entry->body_node;
2782
27732783 return fn_entry;
27742784}
27752785
......@@ -4863,7 +4873,10 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
48634873 case ZigTypeIdStruct:
48644874 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
48654875 TypeStructField *field = &type_entry->data.structure.fields[i];
4866 switch (type_val_resolve_has_one_possible_value(g, field->type_val)) {
4876 OnePossibleValue opv = (field->type_entry != nullptr) ?
4877 type_has_one_possible_value(g, field->type_entry) :
4878 type_val_resolve_has_one_possible_value(g, field->type_val);
4879 switch (opv) {
48674880 case OnePossibleValueInvalid:
48684881 return OnePossibleValueInvalid;
48694882 case OnePossibleValueNo:
......@@ -4901,7 +4914,6 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty, ZigType *parent_type
49014914 }
49024915 switch (ty->id) {
49034916 case ZigTypeIdInvalid:
4904 case ZigTypeIdOpaque:
49054917 zig_unreachable();
49064918 case ZigTypeIdComptimeFloat:
49074919 case ZigTypeIdComptimeInt:
......@@ -4934,6 +4946,7 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty, ZigType *parent_type
49344946 }
49354947 case ZigTypeIdFn:
49364948 return ty->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo;
4949 case ZigTypeIdOpaque:
49374950 case ZigTypeIdEnum:
49384951 case ZigTypeIdErrorSet:
49394952 case ZigTypeIdBool:
src/ir.cpp+238-171
......@@ -11058,14 +11058,21 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou
1105811058}
1105911059
1106011060static IrInstruction *ir_analyze_frame_ptr_to_anyframe(IrAnalyze *ira, IrInstruction *source_instr,
11061 IrInstruction *value, ZigType *wanted_type)
11061 IrInstruction *frame_ptr, ZigType *wanted_type)
1106211062{
11063 if (instr_is_comptime(value)) {
11064 zig_panic("TODO comptime frame pointer");
11063 if (instr_is_comptime(frame_ptr)) {
11064 ConstExprValue *ptr_val = ir_resolve_const(ira, frame_ptr, UndefBad);
11065 if (ptr_val == nullptr)
11066 return ira->codegen->invalid_instruction;
11067
11068 ir_assert(ptr_val->type->id == ZigTypeIdPointer, source_instr);
11069 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
11070 zig_panic("TODO comptime frame pointer");
11071 }
1106511072 }
1106611073
1106711074 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node,
11068 wanted_type, value, CastOpBitCast);
11075 wanted_type, frame_ptr, CastOpBitCast);
1106911076 result->value.type = wanted_type;
1107011077 return result;
1107111078}
......@@ -13618,21 +13625,34 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1361813625 if (type_is_invalid(casted_op2->value.type))
1361913626 return ira->codegen->invalid_instruction;
1362013627
13621 if (op1->value.special == ConstValSpecialUndef || casted_op2->value.special == ConstValSpecialUndef) {
13622 IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type);
13623 result->value.special = ConstValSpecialUndef;
13624 return result;
13628 // If either operand is undef, result is undef.
13629 ConstExprValue *op1_val = nullptr;
13630 ConstExprValue *op2_val = nullptr;
13631 if (instr_is_comptime(op1)) {
13632 op1_val = ir_resolve_const(ira, op1, UndefOk);
13633 if (op1_val == nullptr)
13634 return ira->codegen->invalid_instruction;
13635 if (op1_val->special == ConstValSpecialUndef)
13636 return ir_const_undef(ira, &instruction->base, op1->value.type);
1362513637 }
13626 if (casted_op2->value.special == ConstValSpecialStatic && op1->value.special == ConstValSpecialStatic &&
13638 if (instr_is_comptime(casted_op2)) {
13639 op2_val = ir_resolve_const(ira, casted_op2, UndefOk);
13640 if (op2_val == nullptr)
13641 return ira->codegen->invalid_instruction;
13642 if (op2_val->special == ConstValSpecialUndef)
13643 return ir_const_undef(ira, &instruction->base, op1->value.type);
13644 }
13645
13646 if (op2_val != nullptr && op1_val != nullptr &&
1362713647 (op1->value.data.x_ptr.special == ConstPtrSpecialHardCodedAddr ||
1362813648 op1->value.data.x_ptr.special == ConstPtrSpecialNull))
1362913649 {
13630 uint64_t start_addr = (op1->value.data.x_ptr.special == ConstPtrSpecialNull) ?
13631 0 : op1->value.data.x_ptr.data.hard_coded_addr.addr;
13650 uint64_t start_addr = (op1_val->data.x_ptr.special == ConstPtrSpecialNull) ?
13651 0 : op1_val->data.x_ptr.data.hard_coded_addr.addr;
1363213652 uint64_t elem_offset;
1363313653 if (!ir_resolve_usize(ira, casted_op2, &elem_offset))
1363413654 return ira->codegen->invalid_instruction;
13635 ZigType *elem_type = op1->value.type->data.pointer.child_type;
13655 ZigType *elem_type = op1_val->type->data.pointer.child_type;
1363613656 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown)))
1363713657 return ira->codegen->invalid_instruction;
1363813658 uint64_t byte_offset = type_size(ira->codegen, elem_type) * elem_offset;
......@@ -13644,7 +13664,7 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1364413664 } else {
1364513665 zig_unreachable();
1364613666 }
13647 IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type);
13667 IrInstruction *result = ir_const(ira, &instruction->base, op1_val->type);
1364813668 result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
1364913669 result->value.data.x_ptr.mut = ConstPtrMutRuntimeVar;
1365013670 result->value.data.x_ptr.data.hard_coded_addr.addr = new_addr;
......@@ -14213,9 +14233,13 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1421314233 }
1421414234 break;
1421514235 case ReqCompTimeNo:
14216 if (init_val != nullptr) {
14217 if (init_val->special == ConstValSpecialStatic &&
14218 init_val->type->id == ZigTypeIdFn &&
14236 if (init_val != nullptr && value_is_comptime(init_val)) {
14237 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
14238 decl_var_instruction->base.source_node, init_val, UndefOk)))
14239 {
14240 result_type = ira->codegen->builtin_types.entry_invalid;
14241 } else if (init_val->type->id == ZigTypeIdFn &&
14242 init_val->special != ConstValSpecialUndef &&
1421914243 init_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
1422014244 init_val->data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
1422114245 {
......@@ -14273,7 +14297,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1427314297 }
1427414298 }
1427514299
14276 if (init_val != nullptr && init_val->special != ConstValSpecialRuntime) {
14300 if (init_val != nullptr && value_is_comptime(init_val)) {
1427714301 // Resolve ConstPtrMutInfer
1427814302 if (var->gen_is_const) {
1427914303 var_ptr->value.data.x_ptr.mut = ConstPtrMutComptimeConst;
......@@ -14292,7 +14316,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1429214316 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref, false);
1429314317 }
1429414318
14295 if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) {
14319 if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {
1429614320 assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length);
1429714321 ConstExprValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index);
1429814322 copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const);
......@@ -15222,7 +15246,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1522215246 if (linkage_makes_it_runtime)
1522315247 goto no_mem_slot;
1522415248
15225 if (var->const_value->special == ConstValSpecialStatic) {
15249 if (value_is_comptime(var->const_value)) {
1522615250 mem_slot = var->const_value;
1522715251 } else {
1522815252 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
......@@ -19361,7 +19385,10 @@ static IrInstruction *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruct
1936119385 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
1936219386 true, false, PtrLenUnknown, 0, 0, 0, false);
1936319387 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
19364 if (casted_value->value.special == ConstValSpecialStatic) {
19388 if (instr_is_comptime(casted_value)) {
19389 ConstExprValue *val = ir_resolve_const(ira, casted_value, UndefBad);
19390 if (val == nullptr)
19391 return ira->codegen->invalid_instruction;
1936519392 ErrorTableEntry *err = casted_value->value.data.x_err_set;
1936619393 if (!err->cached_error_name_val) {
1936719394 ConstExprValue *array_val = create_const_str_lit(ira->codegen, &err->name);
......@@ -21524,64 +21551,75 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
2152421551 return ira->codegen->invalid_instruction;
2152521552
2152621553 // TODO test this at comptime with u8 and non-u8 types
21527 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
21528 casted_byte->value.special == ConstValSpecialStatic &&
21529 casted_count->value.special == ConstValSpecialStatic &&
21530 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
21531 casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
21554 if (instr_is_comptime(casted_dest_ptr) &&
21555 instr_is_comptime(casted_byte) &&
21556 instr_is_comptime(casted_count))
2153221557 {
21533 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
21558 ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad);
21559 if (dest_ptr_val == nullptr)
21560 return ira->codegen->invalid_instruction;
2153421561
21535 ConstExprValue *dest_elements;
21536 size_t start;
21537 size_t bound_end;
21538 switch (dest_ptr_val->data.x_ptr.special) {
21539 case ConstPtrSpecialInvalid:
21540 case ConstPtrSpecialDiscard:
21541 zig_unreachable();
21542 case ConstPtrSpecialRef:
21543 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21544 start = 0;
21545 bound_end = 1;
21546 break;
21547 case ConstPtrSpecialBaseArray:
21548 {
21549 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21550 expand_undef_array(ira->codegen, array_val);
21551 dest_elements = array_val->data.x_array.data.s_none.elements;
21552 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21553 bound_end = array_val->type->data.array.len;
21554 break;
21555 }
21556 case ConstPtrSpecialBaseStruct:
21557 zig_panic("TODO memset on const inner struct");
21558 case ConstPtrSpecialBaseErrorUnionCode:
21559 zig_panic("TODO memset on const inner error union code");
21560 case ConstPtrSpecialBaseErrorUnionPayload:
21561 zig_panic("TODO memset on const inner error union payload");
21562 case ConstPtrSpecialBaseOptionalPayload:
21563 zig_panic("TODO memset on const inner optional payload");
21564 case ConstPtrSpecialHardCodedAddr:
21565 zig_unreachable();
21566 case ConstPtrSpecialFunction:
21567 zig_panic("TODO memset on ptr cast from function");
21568 case ConstPtrSpecialNull:
21569 zig_panic("TODO memset on null ptr");
21570 }
21562 ConstExprValue *byte_val = ir_resolve_const(ira, casted_byte, UndefOk);
21563 if (byte_val == nullptr)
21564 return ira->codegen->invalid_instruction;
2157121565
21572 size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint);
21573 size_t end = start + count;
21574 if (end > bound_end) {
21575 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
21566 ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad);
21567 if (count_val == nullptr)
2157621568 return ira->codegen->invalid_instruction;
21577 }
2157821569
21579 ConstExprValue *byte_val = &casted_byte->value;
21580 for (size_t i = start; i < end; i += 1) {
21581 copy_const_val(&dest_elements[i], byte_val, true);
21582 }
21570 if (casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
21571 casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
21572 {
21573 ConstExprValue *dest_elements;
21574 size_t start;
21575 size_t bound_end;
21576 switch (dest_ptr_val->data.x_ptr.special) {
21577 case ConstPtrSpecialInvalid:
21578 case ConstPtrSpecialDiscard:
21579 zig_unreachable();
21580 case ConstPtrSpecialRef:
21581 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21582 start = 0;
21583 bound_end = 1;
21584 break;
21585 case ConstPtrSpecialBaseArray:
21586 {
21587 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21588 expand_undef_array(ira->codegen, array_val);
21589 dest_elements = array_val->data.x_array.data.s_none.elements;
21590 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21591 bound_end = array_val->type->data.array.len;
21592 break;
21593 }
21594 case ConstPtrSpecialBaseStruct:
21595 zig_panic("TODO memset on const inner struct");
21596 case ConstPtrSpecialBaseErrorUnionCode:
21597 zig_panic("TODO memset on const inner error union code");
21598 case ConstPtrSpecialBaseErrorUnionPayload:
21599 zig_panic("TODO memset on const inner error union payload");
21600 case ConstPtrSpecialBaseOptionalPayload:
21601 zig_panic("TODO memset on const inner optional payload");
21602 case ConstPtrSpecialHardCodedAddr:
21603 zig_unreachable();
21604 case ConstPtrSpecialFunction:
21605 zig_panic("TODO memset on ptr cast from function");
21606 case ConstPtrSpecialNull:
21607 zig_panic("TODO memset on null ptr");
21608 }
2158321609
21584 return ir_const_void(ira, &instruction->base);
21610 size_t count = bigint_as_unsigned(&count_val->data.x_bigint);
21611 size_t end = start + count;
21612 if (end > bound_end) {
21613 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
21614 return ira->codegen->invalid_instruction;
21615 }
21616
21617 for (size_t i = start; i < end; i += 1) {
21618 copy_const_val(&dest_elements[i], byte_val, true);
21619 }
21620
21621 return ir_const_void(ira, &instruction->base);
21622 }
2158521623 }
2158621624
2158721625 IrInstruction *result = ir_build_memset(&ira->new_irb, instruction->base.scope, instruction->base.source_node,
......@@ -21649,107 +21687,118 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2164921687
2165021688 // TODO test this at comptime with u8 and non-u8 types
2165121689 // TODO test with dest ptr being a global runtime variable
21652 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
21653 casted_src_ptr->value.special == ConstValSpecialStatic &&
21654 casted_count->value.special == ConstValSpecialStatic &&
21655 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
21690 if (instr_is_comptime(casted_dest_ptr) &&
21691 instr_is_comptime(casted_src_ptr) &&
21692 instr_is_comptime(casted_count))
2165621693 {
21657 size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint);
21694 ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad);
21695 if (dest_ptr_val == nullptr)
21696 return ira->codegen->invalid_instruction;
2165821697
21659 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
21660 ConstExprValue *dest_elements;
21661 size_t dest_start;
21662 size_t dest_end;
21663 switch (dest_ptr_val->data.x_ptr.special) {
21664 case ConstPtrSpecialInvalid:
21665 case ConstPtrSpecialDiscard:
21666 zig_unreachable();
21667 case ConstPtrSpecialRef:
21668 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21669 dest_start = 0;
21670 dest_end = 1;
21671 break;
21672 case ConstPtrSpecialBaseArray:
21673 {
21674 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21675 expand_undef_array(ira->codegen, array_val);
21676 dest_elements = array_val->data.x_array.data.s_none.elements;
21677 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21678 dest_end = array_val->type->data.array.len;
21679 break;
21680 }
21681 case ConstPtrSpecialBaseStruct:
21682 zig_panic("TODO memcpy on const inner struct");
21683 case ConstPtrSpecialBaseErrorUnionCode:
21684 zig_panic("TODO memcpy on const inner error union code");
21685 case ConstPtrSpecialBaseErrorUnionPayload:
21686 zig_panic("TODO memcpy on const inner error union payload");
21687 case ConstPtrSpecialBaseOptionalPayload:
21688 zig_panic("TODO memcpy on const inner optional payload");
21689 case ConstPtrSpecialHardCodedAddr:
21690 zig_unreachable();
21691 case ConstPtrSpecialFunction:
21692 zig_panic("TODO memcpy on ptr cast from function");
21693 case ConstPtrSpecialNull:
21694 zig_panic("TODO memcpy on null ptr");
21695 }
21698 ConstExprValue *src_ptr_val = ir_resolve_const(ira, casted_src_ptr, UndefBad);
21699 if (src_ptr_val == nullptr)
21700 return ira->codegen->invalid_instruction;
2169621701
21697 if (dest_start + count > dest_end) {
21698 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21702 ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad);
21703 if (count_val == nullptr)
2169921704 return ira->codegen->invalid_instruction;
21700 }
2170121705
21702 ConstExprValue *src_ptr_val = &casted_src_ptr->value;
21703 ConstExprValue *src_elements;
21704 size_t src_start;
21705 size_t src_end;
21706 if (dest_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
21707 size_t count = bigint_as_unsigned(&count_val->data.x_bigint);
2170621708
21707 switch (src_ptr_val->data.x_ptr.special) {
21708 case ConstPtrSpecialInvalid:
21709 case ConstPtrSpecialDiscard:
21710 zig_unreachable();
21711 case ConstPtrSpecialRef:
21712 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
21713 src_start = 0;
21714 src_end = 1;
21715 break;
21716 case ConstPtrSpecialBaseArray:
21717 {
21718 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
21719 expand_undef_array(ira->codegen, array_val);
21720 src_elements = array_val->data.x_array.data.s_none.elements;
21721 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
21722 src_end = array_val->type->data.array.len;
21709 ConstExprValue *dest_elements;
21710 size_t dest_start;
21711 size_t dest_end;
21712 switch (dest_ptr_val->data.x_ptr.special) {
21713 case ConstPtrSpecialInvalid:
21714 case ConstPtrSpecialDiscard:
21715 zig_unreachable();
21716 case ConstPtrSpecialRef:
21717 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21718 dest_start = 0;
21719 dest_end = 1;
2172321720 break;
21724 }
21725 case ConstPtrSpecialBaseStruct:
21726 zig_panic("TODO memcpy on const inner struct");
21727 case ConstPtrSpecialBaseErrorUnionCode:
21728 zig_panic("TODO memcpy on const inner error union code");
21729 case ConstPtrSpecialBaseErrorUnionPayload:
21730 zig_panic("TODO memcpy on const inner error union payload");
21731 case ConstPtrSpecialBaseOptionalPayload:
21732 zig_panic("TODO memcpy on const inner optional payload");
21733 case ConstPtrSpecialHardCodedAddr:
21734 zig_unreachable();
21735 case ConstPtrSpecialFunction:
21736 zig_panic("TODO memcpy on ptr cast from function");
21737 case ConstPtrSpecialNull:
21738 zig_panic("TODO memcpy on null ptr");
21739 }
21721 case ConstPtrSpecialBaseArray:
21722 {
21723 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21724 expand_undef_array(ira->codegen, array_val);
21725 dest_elements = array_val->data.x_array.data.s_none.elements;
21726 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21727 dest_end = array_val->type->data.array.len;
21728 break;
21729 }
21730 case ConstPtrSpecialBaseStruct:
21731 zig_panic("TODO memcpy on const inner struct");
21732 case ConstPtrSpecialBaseErrorUnionCode:
21733 zig_panic("TODO memcpy on const inner error union code");
21734 case ConstPtrSpecialBaseErrorUnionPayload:
21735 zig_panic("TODO memcpy on const inner error union payload");
21736 case ConstPtrSpecialBaseOptionalPayload:
21737 zig_panic("TODO memcpy on const inner optional payload");
21738 case ConstPtrSpecialHardCodedAddr:
21739 zig_unreachable();
21740 case ConstPtrSpecialFunction:
21741 zig_panic("TODO memcpy on ptr cast from function");
21742 case ConstPtrSpecialNull:
21743 zig_panic("TODO memcpy on null ptr");
21744 }
2174021745
21741 if (src_start + count > src_end) {
21742 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21743 return ira->codegen->invalid_instruction;
21744 }
21746 if (dest_start + count > dest_end) {
21747 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21748 return ira->codegen->invalid_instruction;
21749 }
2174521750
21746 // TODO check for noalias violations - this should be generalized to work for any function
21751 ConstExprValue *src_elements;
21752 size_t src_start;
21753 size_t src_end;
2174721754
21748 for (size_t i = 0; i < count; i += 1) {
21749 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
21750 }
21755 switch (src_ptr_val->data.x_ptr.special) {
21756 case ConstPtrSpecialInvalid:
21757 case ConstPtrSpecialDiscard:
21758 zig_unreachable();
21759 case ConstPtrSpecialRef:
21760 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
21761 src_start = 0;
21762 src_end = 1;
21763 break;
21764 case ConstPtrSpecialBaseArray:
21765 {
21766 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
21767 expand_undef_array(ira->codegen, array_val);
21768 src_elements = array_val->data.x_array.data.s_none.elements;
21769 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
21770 src_end = array_val->type->data.array.len;
21771 break;
21772 }
21773 case ConstPtrSpecialBaseStruct:
21774 zig_panic("TODO memcpy on const inner struct");
21775 case ConstPtrSpecialBaseErrorUnionCode:
21776 zig_panic("TODO memcpy on const inner error union code");
21777 case ConstPtrSpecialBaseErrorUnionPayload:
21778 zig_panic("TODO memcpy on const inner error union payload");
21779 case ConstPtrSpecialBaseOptionalPayload:
21780 zig_panic("TODO memcpy on const inner optional payload");
21781 case ConstPtrSpecialHardCodedAddr:
21782 zig_unreachable();
21783 case ConstPtrSpecialFunction:
21784 zig_panic("TODO memcpy on ptr cast from function");
21785 case ConstPtrSpecialNull:
21786 zig_panic("TODO memcpy on null ptr");
21787 }
2175121788
21752 return ir_const_void(ira, &instruction->base);
21789 if (src_start + count > src_end) {
21790 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21791 return ira->codegen->invalid_instruction;
21792 }
21793
21794 // TODO check for noalias violations - this should be generalized to work for any function
21795
21796 for (size_t i = 0; i < count; i += 1) {
21797 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
21798 }
21799
21800 return ir_const_void(ira, &instruction->base);
21801 }
2175321802 }
2175421803
2175521804 IrInstruction *result = ir_build_memcpy(&ira->new_irb, instruction->base.scope, instruction->base.source_node,
......@@ -22412,13 +22461,26 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr
2241222461 if (type_is_invalid(casted_result_ptr->value.type))
2241322462 return ira->codegen->invalid_instruction;
2241422463
22415 if (casted_op1->value.special == ConstValSpecialStatic &&
22416 casted_op2->value.special == ConstValSpecialStatic &&
22417 casted_result_ptr->value.special == ConstValSpecialStatic)
22464 if (instr_is_comptime(casted_op1) &&
22465 instr_is_comptime(casted_op2) &&
22466 instr_is_comptime(casted_result_ptr))
2241822467 {
22419 BigInt *op1_bigint = &casted_op1->value.data.x_bigint;
22420 BigInt *op2_bigint = &casted_op2->value.data.x_bigint;
22421 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, &casted_result_ptr->value, casted_result_ptr->source_node);
22468 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad);
22469 if (op1_val == nullptr)
22470 return ira->codegen->invalid_instruction;
22471
22472 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad);
22473 if (op2_val == nullptr)
22474 return ira->codegen->invalid_instruction;
22475
22476 ConstExprValue *result_val = ir_resolve_const(ira, casted_result_ptr, UndefBad);
22477 if (result_val == nullptr)
22478 return ira->codegen->invalid_instruction;
22479
22480 BigInt *op1_bigint = &op1_val->data.x_bigint;
22481 BigInt *op2_bigint = &op2_val->data.x_bigint;
22482 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, result_val,
22483 casted_result_ptr->source_node);
2242222484 if (pointee_val == nullptr)
2242322485 return ira->codegen->invalid_instruction;
2242422486 BigInt *dest_bigint = &pointee_val->data.x_bigint;
......@@ -22839,9 +22901,9 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2283922901 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
2284022902 } else {
2284122903 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->child;
22842 if (type_is_invalid(param_type_value->value.type))
22843 return ira->codegen->invalid_instruction;
2284422904 ZigType *param_type = ir_resolve_type(ira, param_type_value);
22905 if (type_is_invalid(param_type))
22906 return ira->codegen->invalid_instruction;
2284522907 switch (type_requires_comptime(ira->codegen, param_type, nullptr)) {
2284622908 case ReqCompTimeYes:
2284722909 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
......@@ -23271,7 +23333,12 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
2327123333 if (!val)
2327223334 return ira->codegen->invalid_instruction;
2327323335
23274 if (val->special == ConstValSpecialStatic) {
23336 if (value_is_comptime(val)) {
23337 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
23338 source_instr->source_node, val, UndefBad)))
23339 {
23340 return ira->codegen->invalid_instruction;
23341 }
2327523342 bool is_addr_zero = val->data.x_ptr.special == ConstPtrSpecialNull ||
2327623343 (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr &&
2327723344 val->data.x_ptr.data.hard_coded_addr.addr == 0);