zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit acfdad858138de029abcb1c9bf20df0e58738eb3 (tree)
parent f296eec294f7263c9e809c1d825a13a395e5234b
Author: David Rubin <daviru007@icloud.com>
Date:   Wed, 12 Mar 2025 22:48:25 +0000

Sema: convert slice sentinel to single pointer correctly

Diffstat:
Msrc/Sema.zig | 2++
Mtest/behavior/memcpy.zig | 31+++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -25726,12 +25726,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 @@ -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(); +}