Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24311,7 +24311,8 @@ fn analyzeMinMax(

fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !Air.Inst.Ref {
const mod = sema.mod;
const info = sema.typeOf(ptr).ptrInfo(mod);
const ptr_ty = sema.typeOf(ptr);
const info = ptr_ty.ptrInfo(mod);
if (info.flags.size == .One) {
// Already an array pointer.
return ptr;
Expand All @@ -24330,10 +24331,11 @@ fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !A
.address_space = info.flags.address_space,
},
});
if (info.flags.size == .Slice) {
return block.addTyOp(.slice_ptr, new_ty, ptr);
}
return block.addBitCast(new_ty, ptr);
const non_slice_ptr = if (info.flags.size == .Slice)
try block.addTyOp(.slice_ptr, ptr_ty.slicePtrFieldType(mod), ptr)
else
ptr;
return block.addBitCast(new_ty, non_slice_ptr);
}

fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
Expand Down
2 changes: 2 additions & 0 deletions src/link/C.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub fn deinit(self: *C) void {
self.string_bytes.deinit(gpa);
self.fwd_decl_buf.deinit(gpa);
self.code_buf.deinit(gpa);
self.lazy_fwd_decl_buf.deinit(gpa);
self.lazy_code_buf.deinit(gpa);
}

pub fn freeDecl(self: *C, decl_index: InternPool.DeclIndex) void {
Expand Down
21 changes: 21 additions & 0 deletions test/behavior/memcpy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ fn testMemcpyDestManyPtr() !void {
try expect(buf[4] == 'o');
}

test "@memcpy slice" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;

try testMemcpySlice();
try comptime testMemcpySlice();
}

fn testMemcpySlice() !void {
var buf: [5]u8 = undefined;
const dst: []u8 = &buf;
const src: []const u8 = "hello";
@memcpy(dst, src);
try expect(buf[0] == 'h');
try expect(buf[1] == 'e');
try expect(buf[2] == 'l');
try expect(buf[3] == 'l');
try expect(buf[4] == 'o');
}

comptime {
const S = struct {
buffer: [8]u8 = undefined,
Expand Down