authorgravatar for c-kappel@noreply.codeberg.orgc-kappel <c-kappel@noreply.codeberg.org> 2026-08-04 22:15:44+02:00
committergravatar for justusk@noreply.codeberg.orgJustus Klausecker <justusk@noreply.codeberg.org> 2026-08-04 22:15:44+02:00
log8cb4459f36704a3660dcbce30e80f6e01a66b39e
tree7e086ac20b5bf543619731a088d6e102f5748e11
parentf0354179a88d9c8274e571dc1e62a29e9c58376b

Sema: correctly check `packed_offset` when coercing to c pointer (#36097)

Co-authored-by: c-kappel <kcapcode@gmail.com> Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36097 Reviewed-by: Justus Klausecker <justusk@noreply.codeberg.org>

2 files changed, 24 insertions(+), 13 deletions(-)

src/Sema.zig+13-13
......@@ -30223,6 +30223,19 @@ fn checkPtrAttributes(sema: *Sema, dest_ty: Type, inst_ty: Type, in_memory_resul
3022330223 } };
3022430224 return false;
3022530225 }
30226
30227 if (inst_info.packed_offset.host_size != dest_info.packed_offset.host_size or
30228 inst_info.packed_offset.bit_offset != dest_info.packed_offset.bit_offset)
30229 {
30230 in_memory_result.* = .{ .ptr_bit_range = .{
30231 .actual_host = inst_info.packed_offset.host_size,
30232 .wanted_host = dest_info.packed_offset.host_size,
30233 .actual_offset = inst_info.packed_offset.bit_offset,
30234 .wanted_offset = dest_info.packed_offset.bit_offset,
30235 } };
30236 return false;
30237 }
30238
3022630239 if (inst_info.flags.alignment == .none and dest_info.flags.alignment == .none) return true;
3022730240 if (len0) return true;
3022830241
......@@ -30243,19 +30256,6 @@ fn checkPtrAttributes(sema: *Sema, dest_ty: Type, inst_ty: Type, in_memory_resul
3024330256 } };
3024430257 return false;
3024530258 }
30246
30247 if (inst_info.packed_offset.host_size != dest_info.packed_offset.host_size or
30248 inst_info.packed_offset.bit_offset != dest_info.packed_offset.bit_offset)
30249 {
30250 in_memory_result.* = .{ .ptr_bit_range = .{
30251 .actual_host = inst_info.packed_offset.host_size,
30252 .wanted_host = dest_info.packed_offset.host_size,
30253 .actual_offset = inst_info.packed_offset.bit_offset,
30254 .wanted_offset = dest_info.packed_offset.bit_offset,
30255 } };
30256 return false;
30257 }
30258
3025930259 return true;
3026030260}
3026130261
test/cases/compile_errors/coercion_from_vector_element_to_c_ptr.zig created+11
......@@ -0,0 +1,11 @@
1export fn foo() void {
2 var size: @Vector(4, c_int) = undefined;
3 bar(&size[0]);
4}
5extern fn bar([*c]c_int) void;
6
7// error
8//
9// 3:9: error: expected type '[*c]c_int', found '*align(4:0:4:0) c_int'
10// 3:9: note: pointer host size '4' cannot cast into pointer host size '0'
11// 5:15: note: parameter type declared here