authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-03-12 22:48:25+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-03-30 03:41:00+01:00
logacfdad858138de029abcb1c9bf20df0e58738eb3
tree845f2f5e2e8298598b2206a22ac8204dbdf9a72b
parentf296eec294f7263c9e809c1d825a13a395e5234b

Sema: convert slice sentinel to single pointer correctly


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

src/Sema.zig+2
......@@ -25726,12 +25726,14 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2572625726 var info = dest_ty.ptrInfo(zcu);
2572725727 info.flags.size = .one;
2572825728 info.child = array_ty.toIntern();
25729 info.sentinel = .none;
2572925730 break :info info;
2573025731 });
2573125732 const src_array_ptr_ty = try pt.ptrType(info: {
2573225733 var info = src_ty.ptrInfo(zcu);
2573325734 info.flags.size = .one;
2573425735 info.child = array_ty.toIntern();
25736 info.sentinel = .none;
2573525737 break :info info;
2573625738 });
2573725739
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}