Skip to content

Commit 95636c7

Browse files
committed
ability to @ptrCast to *void
fixes #960
1 parent 92f7474 commit 95636c7

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/codegen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,9 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
26552655
IrInstructionPtrCast *instruction)
26562656
{
26572657
TypeTableEntry *wanted_type = instruction->base.value.type;
2658+
if (!type_has_bits(wanted_type)) {
2659+
return nullptr;
2660+
}
26582661
LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
26592662
return LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");
26602663
}

src/ir.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19757,6 +19757,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1975719757
}
1975819758

1975919759
static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtrCast *instruction) {
19760+
Error err;
19761+
1976019762
IrInstruction *dest_type_value = instruction->dest_type->other;
1976119763
TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
1976219764
if (type_is_invalid(dest_type))
@@ -19810,9 +19812,13 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc
1981019812
instruction->base.source_node, nullptr, ptr);
1981119813
casted_ptr->value.type = dest_type;
1981219814

19813-
// keep the bigger alignment, it can only help
19815+
// Keep the bigger alignment, it can only help-
19816+
// unless the target is zero bits.
19817+
if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
19818+
return ira->codegen->builtin_types.entry_invalid;
19819+
1981419820
IrInstruction *result;
19815-
if (src_align_bytes > dest_align_bytes) {
19821+
if (src_align_bytes > dest_align_bytes && type_has_bits(dest_type)) {
1981619822
result = ir_align_cast(ira, casted_ptr, src_align_bytes, false);
1981719823
if (type_is_invalid(result->value.type))
1981819824
return ira->codegen->builtin_types.entry_invalid;

test/cases/cast.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,9 @@ fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
519519
@ptrCast([*]u8, array.?)[n] += 1;
520520
}
521521
}
522+
523+
test "*usize to *void" {
524+
var i = usize(0);
525+
var v = @ptrCast(*void, &i);
526+
v.* = {};
527+
}

0 commit comments

Comments
 (0)