@@ -172,7 +172,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
172
172
static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
173
173
ConstExprValue *out_val, ConstExprValue *ptr_val);
174
174
static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr,
175
- ZigType *dest_type, IrInstruction *dest_type_src);
175
+ ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on );
176
176
static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed);
177
177
static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs);
178
178
static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);
@@ -2202,12 +2202,13 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo
2202
2202
}
2203
2203
2204
2204
static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2205
- IrInstruction *dest_type, IrInstruction *ptr)
2205
+ IrInstruction *dest_type, IrInstruction *ptr, bool safety_check_on )
2206
2206
{
2207
2207
IrInstructionPtrCastSrc *instruction = ir_build_instruction<IrInstructionPtrCastSrc>(
2208
2208
irb, scope, source_node);
2209
2209
instruction->dest_type = dest_type;
2210
2210
instruction->ptr = ptr;
2211
+ instruction->safety_check_on = safety_check_on;
2211
2212
2212
2213
ir_ref_instruction(dest_type, irb->current_basic_block);
2213
2214
ir_ref_instruction(ptr, irb->current_basic_block);
@@ -2216,12 +2217,13 @@ static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNod
2216
2217
}
2217
2218
2218
2219
static IrInstruction *ir_build_ptr_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction,
2219
- ZigType *ptr_type, IrInstruction *ptr)
2220
+ ZigType *ptr_type, IrInstruction *ptr, bool safety_check_on )
2220
2221
{
2221
2222
IrInstructionPtrCastGen *instruction = ir_build_instruction<IrInstructionPtrCastGen>(
2222
2223
&ira->new_irb, source_instruction->scope, source_instruction->source_node);
2223
2224
instruction->base.value.type = ptr_type;
2224
2225
instruction->ptr = ptr;
2226
+ instruction->safety_check_on = safety_check_on;
2225
2227
2226
2228
ir_ref_instruction(ptr, ira->new_irb.current_basic_block);
2227
2229
@@ -4505,7 +4507,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4505
4507
if (arg1_value == irb->codegen->invalid_instruction)
4506
4508
return arg1_value;
4507
4509
4508
- IrInstruction *ptr_cast = ir_build_ptr_cast_src(irb, scope, node, arg0_value, arg1_value);
4510
+ IrInstruction *ptr_cast = ir_build_ptr_cast_src(irb, scope, node, arg0_value, arg1_value, true );
4509
4511
return ir_lval_wrap(irb, scope, ptr_cast, lval);
4510
4512
}
4511
4513
case BuiltinFnIdBitCast:
@@ -6740,7 +6742,8 @@ static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode
6740
6742
IrInstruction *is_suspended_mask = ir_build_const_usize(irb, scope, node, 0x2); // 0b010
6741
6743
6742
6744
// TODO relies on Zig not re-ordering fields
6743
- IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst);
6745
+ IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst,
6746
+ false);
6744
6747
IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
6745
6748
Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
6746
6749
IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
@@ -6818,7 +6821,8 @@ static IrInstruction *ir_gen_resume_target(IrBuilder *irb, Scope *scope, AstNode
6818
6821
get_promise_type(irb->codegen, irb->codegen->builtin_types.entry_void));
6819
6822
6820
6823
// TODO relies on Zig not re-ordering fields
6821
- IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst);
6824
+ IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst,
6825
+ false);
6822
6826
IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
6823
6827
Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
6824
6828
IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
@@ -7363,7 +7367,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7363
7367
7364
7368
u8_ptr_type = ir_build_const_type(irb, coro_scope, node,
7365
7369
get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false));
7366
- IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, coro_promise_ptr);
7370
+ IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type,
7371
+ coro_promise_ptr, false);
7367
7372
coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr);
7368
7373
coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
7369
7374
IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node);
@@ -7387,7 +7392,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7387
7392
ir_build_return(irb, coro_scope, node, undef);
7388
7393
7389
7394
ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block);
7390
- IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr);
7395
+ IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr,
7396
+ false);
7391
7397
irb->exec->coro_handle = ir_build_coro_begin(irb, coro_scope, node, coro_id, coro_mem_ptr);
7392
7398
7393
7399
Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
@@ -7465,9 +7471,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7465
7471
get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,
7466
7472
false, false, PtrLenUnknown, 0, 0, 0));
7467
7473
IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr);
7468
- IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, result_ptr);
7469
- IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len,
7470
- irb->exec->coro_result_field_ptr);
7474
+ IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len,
7475
+ result_ptr, false);
7476
+ IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node,
7477
+ u8_ptr_type_unknown_len, irb->exec->coro_result_field_ptr, false);
7471
7478
IrInstruction *return_type_inst = ir_build_const_type(irb, scope, node,
7472
7479
fn_entry->type_entry->data.fn.fn_type_id.return_type);
7473
7480
IrInstruction *size_of_ret_val = ir_build_size_of(irb, scope, node, return_type_inst);
@@ -7517,7 +7524,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7517
7524
IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
7518
7525
get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,
7519
7526
false, false, PtrLenUnknown, 0, 0, 0));
7520
- IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe);
7527
+ IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len,
7528
+ coro_mem_ptr_maybe, false);
7521
7529
IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);
7522
7530
IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var);
7523
7531
IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr);
@@ -8644,15 +8652,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
8644
8652
return err_set_type;
8645
8653
}
8646
8654
8647
- static bool ptr_allows_addr_zero(ZigType *ptr_type) {
8648
- if (ptr_type->id == ZigTypeIdPointer) {
8649
- return ptr_type->data.pointer.allow_zero;
8650
- } else if (ptr_type->id == ZigTypeIdOptional) {
8651
- return true;
8652
- }
8653
- return false;
8654
- }
8655
-
8656
8655
static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted_type,
8657
8656
ZigType *actual_type, AstNode *source_node, bool wanted_is_mutable)
8658
8657
{
@@ -11310,7 +11309,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
11310
11309
actual_type->data.pointer.host_int_bytes == dest_ptr_type->data.pointer.host_int_bytes &&
11311
11310
get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, dest_ptr_type))
11312
11311
{
11313
- return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr);
11312
+ return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true );
11314
11313
}
11315
11314
}
11316
11315
@@ -11352,7 +11351,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
11352
11351
actual_type->data.pointer.child_type, source_node,
11353
11352
!wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
11354
11353
{
11355
- return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr);
11354
+ return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true );
11356
11355
}
11357
11356
11358
11357
// cast from integer to C pointer
@@ -20616,7 +20615,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
20616
20615
}
20617
20616
20618
20617
static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr,
20619
- ZigType *dest_type, IrInstruction *dest_type_src)
20618
+ ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on )
20620
20619
{
20621
20620
Error err;
20622
20621
@@ -20685,7 +20684,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
20685
20684
return ira->codegen->invalid_instruction;
20686
20685
}
20687
20686
20688
- IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr);
20687
+ IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on );
20689
20688
20690
20689
if (type_has_bits(dest_type) && !type_has_bits(src_type)) {
20691
20690
ErrorMsg *msg = ir_add_error(ira, source_instr,
@@ -20722,7 +20721,8 @@ static IrInstruction *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruct
20722
20721
if (type_is_invalid(src_type))
20723
20722
return ira->codegen->invalid_instruction;
20724
20723
20725
- return ir_analyze_ptr_cast(ira, &instruction->base, ptr, dest_type, dest_type_value);
20724
+ return ir_analyze_ptr_cast(ira, &instruction->base, ptr, dest_type, dest_type_value,
20725
+ instruction->safety_check_on);
20726
20726
}
20727
20727
20728
20728
static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExprValue *val, size_t len) {
0 commit comments