authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-03-12 22:48:25+00:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-31 17:36:45+02:00
logaca8ed9deccda88c79daf31b9a427df8871ec3b3
treeaf82854b31127f074acab85d780eac7ce081c936
parent6ac462b08827a70a6bc2c1c695372a2f34075234
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Sema: convert slice sentinel to single pointer correctly


2 files changed, 33 insertions(+), 0 deletions(-)

src/Sema.zig+2
......@@ -26350,12 +26350,14 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2635026350 var info = dest_ty.ptrInfo(zcu);
2635126351 info.flags.size = .one;
2635226352 info.child = array_ty.toIntern();
26353 info.sentinel = .none;
2635326354 break :info info;
2635426355 });
2635526356 const src_array_ptr_ty = try pt.ptrType(info: {
2635626357 var info = src_ty.ptrInfo(zcu);
2635726358 info.flags.size = .one;
2635826359 info.child = array_ty.toIntern();
26360 info.sentinel = .none;
2635926361 break :info info;
2636026362 });
2636126363
test/behavior/memcpy.zig+31
......@@ -133,3 +133,34 @@ test "@memcpy zero-bit type with aliasing" {
133133 S.doTheTest();
134134 comptime S.doTheTest();
135135}
136
137test "@memcpy with sentinel" {
138 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
139
140 const S = struct {
141 fn doTheTest() void {
142 const field = @typeInfo(struct { a: u32 }).@"struct".fields[0];
143 var buffer: [field.name.len]u8 = undefined;
144 @memcpy(&buffer, field.name);
145 }
146 };
147
148 S.doTheTest();
149 comptime S.doTheTest();
150}
151
152test "@memcpy no sentinel source into sentinel destination" {
153 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
154
155 const S = struct {
156 fn doTheTest() void {
157 const src: []const u8 = &.{ 1, 2, 3 };
158 comptime var dest_buf: [3:0]u8 = @splat(0);
159 const dest: [:0]u8 = &dest_buf;
160 @memcpy(dest, src);
161 }
162 };
163
164 S.doTheTest();
165 comptime S.doTheTest();
166}