mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Merge 51b16081d0 into e4be00f949
This commit is contained in:
commit
eb0496951d
2 changed files with 21 additions and 2 deletions
14
src/Sema.zig
14
src/Sema.zig
|
|
@ -28278,7 +28278,7 @@ fn elemPtrArray(
|
|||
block: *Block,
|
||||
src: LazySrcLoc,
|
||||
array_ptr_src: LazySrcLoc,
|
||||
array_ptr: Air.Inst.Ref,
|
||||
array_ptr_orig: Air.Inst.Ref,
|
||||
elem_index_src: LazySrcLoc,
|
||||
elem_index: Air.Inst.Ref,
|
||||
init: bool,
|
||||
|
|
@ -28286,7 +28286,17 @@ fn elemPtrArray(
|
|||
) CompileError!Air.Inst.Ref {
|
||||
const pt = sema.pt;
|
||||
const zcu = pt.zcu;
|
||||
const array_ptr_ty = sema.typeOf(array_ptr);
|
||||
const array_ptr_ty_orig = sema.typeOf(array_ptr_orig);
|
||||
var array_ptr_info = array_ptr_ty_orig.ptrInfo(zcu);
|
||||
|
||||
// When indexing into a C pointer of an array type, treat it as a single-item pointer to the array: ([*c][_]T)[idx] -> (*[_]T)[idx]
|
||||
const array_ptr, const array_ptr_ty = if (array_ptr_info.flags.size == .c) blk: {
|
||||
array_ptr_info.flags.size = .one;
|
||||
const array_ptr_one_ty = try pt.ptrTypeSema(array_ptr_info);
|
||||
const array_ptr_one = try block.addBitCast(array_ptr_one_ty, array_ptr_orig);
|
||||
break :blk .{ array_ptr_one, array_ptr_one_ty };
|
||||
} else .{ array_ptr_orig, array_ptr_ty_orig };
|
||||
|
||||
const array_ty = array_ptr_ty.childType(zcu);
|
||||
const array_sent = array_ty.sentinel(zcu) != null;
|
||||
const array_len = array_ty.arrayLen(zcu);
|
||||
|
|
|
|||
|
|
@ -786,3 +786,12 @@ test "comptime C pointer to optional pointer" {
|
|||
comptime assert(@TypeOf(inner_ptr) == [*c]const *u8);
|
||||
comptime assert(@intFromPtr(inner_ptr.*) == 0x1000);
|
||||
}
|
||||
|
||||
test "dereference C pointer of array" {
|
||||
var arr: [1]u8 = .{0};
|
||||
const arr_c_ptr = @as([*c][1]u8, &arr);
|
||||
const arr_ptr = &arr;
|
||||
arr_c_ptr.*[0] = 1;
|
||||
try expect(arr[0] == 1);
|
||||
try expect(&arr_ptr.*[0] == &arr_c_ptr.*[0]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue