| ... | ... | @@ -20645,12 +20645,23 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ |
| 20645 | 20645 | } |
| 20646 | 20646 | |
| 20647 | 20647 | if (instr_is_comptime(ptr)) { |
| 20648 | | // Undefined is OK here; @ptrCast is defined to reinterpret the bit pattern |
| 20649 | | // of the pointer as the new pointer type. |
| 20650 | | ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk); |
| 20648 | bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type); |
| 20649 | UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad; |
| 20650 | ConstExprValue *val = ir_resolve_const(ira, ptr, is_undef_allowed); |
| 20651 | 20651 | if (!val) |
| 20652 | 20652 | return ira->codegen->invalid_instruction; |
| 20653 | 20653 | |
| 20654 | if (val->special == ConstValSpecialStatic) { |
| 20655 | bool is_addr_zero = val->data.x_ptr.special == ConstPtrSpecialNull || |
| 20656 | (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr && |
| 20657 | val->data.x_ptr.data.hard_coded_addr.addr == 0); |
| 20658 | if (is_addr_zero && !dest_allows_addr_zero) { |
| 20659 | ir_add_error(ira, source_instr, |
| 20660 | buf_sprintf("null pointer casted to type '%s'", buf_ptr(&dest_type->name))); |
| 20661 | return ira->codegen->invalid_instruction; |
| 20662 | } |
| 20663 | } |
| 20664 | |
| 20654 | 20665 | IrInstruction *result = ir_const(ira, source_instr, dest_type); |
| 20655 | 20666 | copy_const_val(&result->value, val, false); |
| 20656 | 20667 | result->value.type = dest_type; |