From aca8ed9deccda88c79daf31b9a427df8871ec3b3 Mon Sep 17 00:00:00 2001 From: David Rubin Date: Wed, 12 Mar 2025 22:48:25 +0000 Subject: [PATCH] Sema: convert slice sentinel to single pointer correctly --- src/Sema.zig | 2 ++ test/behavior/memcpy.zig | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index 583780d980c5b23f7c7c84eac95c9cf2d62baa10..3535cca8af71333b590b3b8da4f67d76c0733b43 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -26350,12 +26350,14 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void var info = dest_ty.ptrInfo(zcu); info.flags.size = .one; info.child = array_ty.toIntern(); + info.sentinel = .none; break :info info; }); const src_array_ptr_ty = try pt.ptrType(info: { var info = src_ty.ptrInfo(zcu); info.flags.size = .one; info.child = array_ty.toIntern(); + info.sentinel = .none; break :info info; }); diff --git a/test/behavior/memcpy.zig b/test/behavior/memcpy.zig index cf6367d07881406d0cdf5e03c9aefd584860895a..17120f9624cf0b6c021dc505db464b8f2e84d980 100644 --- a/test/behavior/memcpy.zig +++ b/test/behavior/memcpy.zig @@ -133,3 +133,34 @@ test "@memcpy zero-bit type with aliasing" { S.doTheTest(); comptime S.doTheTest(); } + +test "@memcpy with sentinel" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const S = struct { + fn doTheTest() void { + const field = @typeInfo(struct { a: u32 }).@"struct".fields[0]; + var buffer: [field.name.len]u8 = undefined; + @memcpy(&buffer, field.name); + } + }; + + S.doTheTest(); + comptime S.doTheTest(); +} + +test "@memcpy no sentinel source into sentinel destination" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const S = struct { + fn doTheTest() void { + const src: []const u8 = &.{ 1, 2, 3 }; + comptime var dest_buf: [3:0]u8 = @splat(0); + const dest: [:0]u8 = &dest_buf; + @memcpy(dest, src); + } + }; + + S.doTheTest(); + comptime S.doTheTest(); +} -- 2.54.0