diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-04-28 15:06:18 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-28 15:06:18 -0400 |
| commit | 8facd99d41b1290f9b29ec22c9dcce680d972810 (patch) | |
| tree | 0ab61812da58df4127b8b56ec43eb88c0e6d5eb2 | |
| parent | d038676a1f46ecab2bd095d3503ab05bcd8de58c (diff) | |
| parent | 8be4511061f18e1bf2b6f7bf108482c6d1e30aa6 (diff) | |
| download | zig-8facd99d41b1290f9b29ec22c9dcce680d972810.tar.gz zig-8facd99d41b1290f9b29ec22c9dcce680d972810.zip | |
Merge pull request #23708 from ziglang/memmove-followups
`@memmove` followups
| -rw-r--r-- | lib/std/debug.zig | 10 | ||||
| -rw-r--r-- | lib/std/debug/no_panic.zig | 10 | ||||
| -rw-r--r-- | lib/std/debug/simple_panic.zig | 11 | ||||
| -rw-r--r-- | lib/std/zig/Zir.zig | 6 | ||||
| -rw-r--r-- | src/Sema.zig | 48 | ||||
| -rw-r--r-- | src/Zcu.zig | 12 | ||||
| -rw-r--r-- | src/codegen/c.zig | 18 | ||||
| -rw-r--r-- | test/cases/compile_errors/bad_panic_call_signature.zig | 5 | ||||
| -rw-r--r-- | test/cases/compile_errors/bad_panic_generic_signature.zig | 5 | ||||
| -rw-r--r-- | test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig | 12 | ||||
| -rw-r--r-- | test/cases/safety/memcpy_len_mismatch.zig | 2 | ||||
| -rw-r--r-- | test/cases/safety/memmove_len_mismatch.zig | 2 | ||||
| -rw-r--r-- | test/incremental/change_panic_handler_explicit | 15 |
13 files changed, 60 insertions, 96 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 722bcc74a9..d2b17ed30d 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -126,18 +126,16 @@ pub fn FullPanic(comptime panicFn: fn ([]const u8, ?usize) noreturn) type { @branchHint(.cold); call("for loop over objects with non-equal lengths", @returnAddress()); } - pub fn memcpyLenMismatch() noreturn { + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub fn copyLenMismatch() noreturn { @branchHint(.cold); - call("@memcpy arguments have non-equal lengths", @returnAddress()); + call("source and destination arguments have non-equal lengths", @returnAddress()); } pub fn memcpyAlias() noreturn { @branchHint(.cold); call("@memcpy arguments alias", @returnAddress()); } - pub fn memmoveLenMismatch() noreturn { - @branchHint(.cold); - call("@memmove arguments have non-equal lengths", @returnAddress()); - } pub fn noreturnReturned() noreturn { @branchHint(.cold); call("'noreturn' function returned", @returnAddress()); diff --git a/lib/std/debug/no_panic.zig b/lib/std/debug/no_panic.zig index ccecc89a87..0a4996097a 100644 --- a/lib/std/debug/no_panic.zig +++ b/lib/std/debug/no_panic.zig @@ -125,17 +125,15 @@ pub fn forLenMismatch() noreturn { @trap(); } -pub fn memcpyLenMismatch() noreturn { - @branchHint(.cold); - @trap(); -} +/// Delete after next zig1.wasm update +pub const memcpyLenMismatch = copyLenMismatch; -pub fn memcpyAlias() noreturn { +pub fn copyLenMismatch() noreturn { @branchHint(.cold); @trap(); } -pub fn memmoveLenMismatch() noreturn { +pub fn memcpyAlias() noreturn { @branchHint(.cold); @trap(); } diff --git a/lib/std/debug/simple_panic.zig b/lib/std/debug/simple_panic.zig index 724061021f..568f7de495 100644 --- a/lib/std/debug/simple_panic.zig +++ b/lib/std/debug/simple_panic.zig @@ -120,18 +120,17 @@ pub fn forLenMismatch() noreturn { call("for loop over objects with non-equal lengths", null); } -pub fn memcpyLenMismatch() noreturn { - call("@memcpy arguments have non-equal lengths", null); +/// Delete after next zig1.wasm update +pub const memcpyLenMismatch = copyLenMismatch; + +pub fn copyLenMismatch() noreturn { + call("source and destination have non-equal lengths", null); } pub fn memcpyAlias() noreturn { call("@memcpy arguments alias", null); } -pub fn memmoveLenMismatch() noreturn { - call("@memmove arguments have non-equal lengths", null); -} - pub fn noreturnReturned() noreturn { call("'noreturn' function returned", null); } diff --git a/lib/std/zig/Zir.zig b/lib/std/zig/Zir.zig index d40322f51f..bc86ca6885 100644 --- a/lib/std/zig/Zir.zig +++ b/lib/std/zig/Zir.zig @@ -983,12 +983,12 @@ pub const Inst = struct { /// Implements the `@memcpy` builtin. /// Uses the `pl_node` union field with payload `Bin`. memcpy, - /// Implements the `@memset` builtin. - /// Uses the `pl_node` union field with payload `Bin`. - memset, /// Implements the `@memmove` builtin. /// Uses the `pl_node` union field with payload `Bin`. memmove, + /// Implements the `@memset` builtin. + /// Uses the `pl_node` union field with payload `Bin`. + memset, /// Implements the `@min` builtin for 2 args. /// Uses the `pl_node` union field with payload `Bin` min, diff --git a/src/Sema.zig b/src/Sema.zig index 06a4d5edf7..644410dc20 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1574,17 +1574,17 @@ fn analyzeBodyInner( continue; }, .memcpy => { - try sema.zirMemcpy(block, inst); + try sema.zirMemcpy(block, inst, .memcpy, true); i += 1; continue; }, - .memset => { - try sema.zirMemset(block, inst); + .memmove => { + try sema.zirMemcpy(block, inst, .memmove, false); i += 1; continue; }, - .memmove => { - try sema.zirMemmove(block, inst); + .memset => { + try sema.zirMemset(block, inst); i += 1; continue; }, @@ -25630,19 +25630,12 @@ fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !A return block.addBitCast(new_ty, non_slice_ptr); } -fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { - return sema.analyzeCopy(block, inst, .memcpy); -} - -fn zirMemmove(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { - return sema.analyzeCopy(block, inst, .memmove); -} - -fn analyzeCopy( +fn zirMemcpy( sema: *Sema, block: *Block, inst: Zir.Inst.Index, - op: enum { memcpy, memmove }, + air_tag: Air.Inst.Tag, + check_aliasing: bool, ) CompileError!void { const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; @@ -25659,12 +25652,12 @@ fn analyzeCopy( const zcu = pt.zcu; if (dest_ty.isConstPtr(zcu)) { - return sema.fail(block, dest_src, "cannot {s} to constant pointer", .{@tagName(op)}); + return sema.fail(block, dest_src, "cannot copy to constant pointer", .{}); } if (dest_len == .none and src_len == .none) { const msg = msg: { - const msg = try sema.errMsg(src, "unknown @{s} length", .{@tagName(op)}); + const msg = try sema.errMsg(src, "unknown copy length", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(dest_src, msg, "destination type '{}' provides no length", .{ dest_ty.fmt(pt), @@ -25710,7 +25703,7 @@ fn analyzeCopy( if (try sema.resolveDefinedValue(block, src_src, src_len)) |src_len_val| { if (!(try sema.valuesEqual(dest_len_val, src_len_val, Type.usize))) { const msg = msg: { - const msg = try sema.errMsg(src, "non-matching @{s} lengths", .{@tagName(op)}); + const msg = try sema.errMsg(src, "non-matching copy lengths", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(dest_src, msg, "length {} here", .{ dest_len_val.fmtValueSema(pt, sema), @@ -25730,11 +25723,7 @@ fn analyzeCopy( if (block.wantSafety()) { const ok = try block.addBinOp(.cmp_eq, dest_len, src_len); - const panic_id: Zcu.SimplePanicId = switch (op) { - .memcpy => .memcpy_len_mismatch, - .memmove => .memmove_len_mismatch, - }; - try sema.addSafetyCheck(block, src, ok, panic_id); + try sema.addSafetyCheck(block, src, ok, .copy_len_mismatch); } } else if (dest_len != .none) { if (try sema.resolveDefinedValue(block, dest_src, dest_len)) |dest_len_val| { @@ -25762,11 +25751,6 @@ fn analyzeCopy( return; } - const check_aliasing = switch (op) { - .memcpy => true, - .memmove => false, - }; - const runtime_src = rs: { const dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr) orelse break :rs dest_src; const src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr) orelse break :rs src_src; @@ -25898,10 +25882,7 @@ fn analyzeCopy( } _ = try block.addInst(.{ - .tag = switch (op) { - .memcpy => .memcpy, - .memmove => .memmove, - }, + .tag = air_tag, .data = .{ .bin_op = .{ .lhs = new_dest_ptr, .rhs = new_src_ptr, @@ -38153,9 +38134,8 @@ fn getExpectedBuiltinFnType(sema: *Sema, decl: Zcu.BuiltinDecl) CompileError!Typ .@"panic.shiftRhsTooBig", .@"panic.invalidEnumValue", .@"panic.forLenMismatch", - .@"panic.memcpyLenMismatch", + .@"panic.copyLenMismatch", .@"panic.memcpyAlias", - .@"panic.memmoveLenMismatch", .@"panic.noreturnReturned", => try pt.funcType(.{ .param_types = &.{}, diff --git a/src/Zcu.zig b/src/Zcu.zig index 6ff8c98cf8..3e2640fa1a 100644 --- a/src/Zcu.zig +++ b/src/Zcu.zig @@ -300,9 +300,8 @@ pub const BuiltinDecl = enum { @"panic.shiftRhsTooBig", @"panic.invalidEnumValue", @"panic.forLenMismatch", - @"panic.memcpyLenMismatch", + @"panic.copyLenMismatch", @"panic.memcpyAlias", - @"panic.memmoveLenMismatch", @"panic.noreturnReturned", VaList, @@ -378,9 +377,8 @@ pub const BuiltinDecl = enum { .@"panic.shiftRhsTooBig", .@"panic.invalidEnumValue", .@"panic.forLenMismatch", - .@"panic.memcpyLenMismatch", + .@"panic.copyLenMismatch", .@"panic.memcpyAlias", - .@"panic.memmoveLenMismatch", .@"panic.noreturnReturned", => .func, }; @@ -446,9 +444,8 @@ pub const SimplePanicId = enum { shift_rhs_too_big, invalid_enum_value, for_len_mismatch, - memcpy_len_mismatch, + copy_len_mismatch, memcpy_alias, - memmove_len_mismatch, noreturn_returned, pub fn toBuiltin(id: SimplePanicId) BuiltinDecl { @@ -471,9 +468,8 @@ pub const SimplePanicId = enum { .shift_rhs_too_big => .@"panic.shiftRhsTooBig", .invalid_enum_value => .@"panic.invalidEnumValue", .for_len_mismatch => .@"panic.forLenMismatch", - .memcpy_len_mismatch => .@"panic.memcpyLenMismatch", + .copy_len_mismatch => .@"panic.copyLenMismatch", .memcpy_alias => .@"panic.memcpyAlias", - .memmove_len_mismatch => .@"panic.memmoveLenMismatch", .noreturn_returned => .@"panic.noreturnReturned", // zig fmt: on }; diff --git a/src/codegen/c.zig b/src/codegen/c.zig index e7883b9def..e124355bc7 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -3348,8 +3348,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, .atomic_load => try airAtomicLoad(f, inst), .memset => try airMemset(f, inst, false), .memset_safe => try airMemset(f, inst, true), - .memcpy => try airMemcpy(f, inst), - .memmove => try airMemmove(f, inst), + .memcpy => try airMemcpy(f, inst, "memcpy("), + .memmove => try airMemcpy(f, inst, "memmove("), .set_union_tag => try airSetUnionTag(f, inst), .get_union_tag => try airGetUnionTag(f, inst), .clz => try airUnBuiltinCall(f, inst, air_datas[@intFromEnum(inst)].ty_op.operand, "clz", .bits), @@ -6976,15 +6976,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { return .none; } -fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { - return copyOp(f, inst, .memcpy); -} - -fn airMemmove(f: *Function, inst: Air.Inst.Index) !CValue { - return copyOp(f, inst, .memmove); -} - -fn copyOp(f: *Function, inst: Air.Inst.Index, op: enum { memcpy, memmove }) !CValue { +fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CValue { const pt = f.object.dg.pt; const zcu = pt.zcu; const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; @@ -6999,10 +6991,6 @@ fn copyOp(f: *Function, inst: Air.Inst.Index, op: enum { memcpy, memmove }) !CVa try writeArrayLen(f, writer, dest_ptr, dest_ty); try writer.writeAll(" != 0) "); } - const function_paren = switch (op) { - .memcpy => "memcpy(", - .memmove => "memmove(", - }; try writer.writeAll(function_paren); try writeSliceOrPtr(f, writer, dest_ptr, dest_ty); try writer.writeAll(", "); diff --git a/test/cases/compile_errors/bad_panic_call_signature.zig b/test/cases/compile_errors/bad_panic_call_signature.zig index 2165f178b5..1af0fdeb17 100644 --- a/test/cases/compile_errors/bad_panic_call_signature.zig +++ b/test/cases/compile_errors/bad_panic_call_signature.zig @@ -27,9 +27,10 @@ pub const panic = struct { pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig; pub const invalidEnumValue = simple_panic.invalidEnumValue; pub const forLenMismatch = simple_panic.forLenMismatch; - pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch; + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub const copyLenMismatch = simple_panic.copyLenMismatch; pub const memcpyAlias = simple_panic.memcpyAlias; - pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch; pub const noreturnReturned = simple_panic.noreturnReturned; }; diff --git a/test/cases/compile_errors/bad_panic_generic_signature.zig b/test/cases/compile_errors/bad_panic_generic_signature.zig index 413a6b79fd..9373551359 100644 --- a/test/cases/compile_errors/bad_panic_generic_signature.zig +++ b/test/cases/compile_errors/bad_panic_generic_signature.zig @@ -23,9 +23,10 @@ pub const panic = struct { pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig; pub const invalidEnumValue = simple_panic.invalidEnumValue; pub const forLenMismatch = simple_panic.forLenMismatch; - pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch; + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub const copyLenMismatch = simple_panic.copyLenMismatch; pub const memcpyAlias = simple_panic.memcpyAlias; - pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch; pub const noreturnReturned = simple_panic.noreturnReturned; }; diff --git a/test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig b/test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig index 3bc9065fa1..5b0fa8a041 100644 --- a/test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig +++ b/test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig @@ -66,29 +66,29 @@ pub export fn memset_array() void { // backend=stage2 // target=native // -// :5:5: error: unknown @memcpy length +// :5:5: error: unknown copy length // :5:18: note: destination type '[*]u8' provides no length // :5:24: note: source type '[*]const u8' provides no length // :10:13: error: type '*u8' is not an indexable pointer // :10:13: note: operand must be a slice, a many pointer or a pointer to an array // :15:13: error: type '*u8' is not an indexable pointer // :15:13: note: operand must be a slice, a many pointer or a pointer to an array -// :20:5: error: non-matching @memcpy lengths +// :20:5: error: non-matching copy lengths // :20:13: note: length 6 here // :20:20: note: length 5 here // :24:13: error: cannot memset constant pointer -// :29:13: error: cannot memcpy to constant pointer +// :29:13: error: cannot copy to constant pointer // :33:13: error: type '[5]u8' is not an indexable pointer // :33:13: note: operand must be a slice, a many pointer or a pointer to an array -// :39:5: error: unknown @memmove length +// :39:5: error: unknown copy length // :39:19: note: destination type '[*]u8' provides no length // :39:25: note: source type '[*]const u8' provides no length // :44:14: error: type '*u8' is not an indexable pointer // :44:14: note: operand must be a slice, a many pointer or a pointer to an array -// :49:5: error: non-matching @memmove lengths +// :49:5: error: non-matching copy lengths // :49:14: note: length 6 here // :49:21: note: length 5 here -// :54:14: error: cannot memmove to constant pointer +// :54:14: error: cannot copy to constant pointer // :58:14: error: type '[5]u8' is not an indexable pointer // :58:14: note: operand must be a slice, a many pointer or a pointer to an array // :62:13: error: type '[5]u8' is not an indexable pointer diff --git a/test/cases/safety/memcpy_len_mismatch.zig b/test/cases/safety/memcpy_len_mismatch.zig index cfa7f4fa77..0ef22b959c 100644 --- a/test/cases/safety/memcpy_len_mismatch.zig +++ b/test/cases/safety/memcpy_len_mismatch.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = stack_trace; - if (std.mem.eql(u8, message, "@memcpy arguments have non-equal lengths")) { + if (std.mem.eql(u8, message, "source and destination arguments have non-equal lengths")) { std.process.exit(0); } std.process.exit(1); diff --git a/test/cases/safety/memmove_len_mismatch.zig b/test/cases/safety/memmove_len_mismatch.zig index 89908a8c4e..16774cfe86 100644 --- a/test/cases/safety/memmove_len_mismatch.zig +++ b/test/cases/safety/memmove_len_mismatch.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = stack_trace; - if (std.mem.eql(u8, message, "@memmove arguments have non-equal lengths")) { + if (std.mem.eql(u8, message, "source and destination arguments have non-equal lengths")) { std.process.exit(0); } std.process.exit(1); diff --git a/test/incremental/change_panic_handler_explicit b/test/incremental/change_panic_handler_explicit index 5188208647..322773fd47 100644 --- a/test/incremental/change_panic_handler_explicit +++ b/test/incremental/change_panic_handler_explicit @@ -37,9 +37,10 @@ pub const panic = struct { pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; pub const invalidEnumValue = no_panic.invalidEnumValue; pub const forLenMismatch = no_panic.forLenMismatch; - pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub const copyLenMismatch = no_panic.copyLenMismatch; pub const memcpyAlias = no_panic.memcpyAlias; - pub const memmoveLenMismatch = no_panic.memmoveLenMismatch; pub const noreturnReturned = no_panic.noreturnReturned; }; fn myPanic(msg: []const u8, _: ?usize) noreturn { @@ -85,9 +86,10 @@ pub const panic = struct { pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; pub const invalidEnumValue = no_panic.invalidEnumValue; pub const forLenMismatch = no_panic.forLenMismatch; - pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub const copyLenMismatch = no_panic.copyLenMismatch; pub const memcpyAlias = no_panic.memcpyAlias; - pub const memmoveLenMismatch = no_panic.memmoveLenMismatch; pub const noreturnReturned = no_panic.noreturnReturned; }; fn myPanic(msg: []const u8, _: ?usize) noreturn { @@ -133,9 +135,10 @@ pub const panic = struct { pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; pub const invalidEnumValue = no_panic.invalidEnumValue; pub const forLenMismatch = no_panic.forLenMismatch; - pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + /// Delete after next zig1.wasm update + pub const memcpyLenMismatch = copyLenMismatch; + pub const copyLenMismatch = no_panic.copyLenMismatch; pub const memcpyAlias = no_panic.memcpyAlias; - pub const memmoveLenMismatch = no_panic.memmoveLenMismatch; pub const noreturnReturned = no_panic.noreturnReturned; }; fn myPanicNew(msg: []const u8, _: ?usize) noreturn { |
