diff --git a/build.zig b/build.zig index 099c1a954155d533118b0649b5060ccb6299bf98..3c4f56b4ed159167fbf052eeccfea210ecdfcc05 100644 --- a/build.zig +++ b/build.zig @@ -307,7 +307,7 @@ pub fn build(b: *std.Build) !void { }, } }; - const version = try b.allocator.dupeZ(u8, version_slice); + const version = try b.allocator.dupeSentinel(u8, version_slice, 0); exe_options.addOption([:0]const u8, "version", version); if (enable_llvm) { diff --git a/lib/compiler/aro/aro/CodeGen.zig b/lib/compiler/aro/aro/CodeGen.zig index 74dc454ffb623835659db25989848c29aafc2808..fdbcb9909e0d17fb3c08ded8ff4283a754d096f2 100644 --- a/lib/compiler/aro/aro/CodeGen.zig +++ b/lib/compiler/aro/aro/CodeGen.zig @@ -894,7 +894,7 @@ fn genLval(c: *CodeGen, node_index: Node.Index) Error!Ir.Ref { } } - const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice); + const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0); const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len); try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr }); return ref; @@ -1112,7 +1112,7 @@ fn genCall(c: *CodeGen, call: Node.Call) Error!Ir.Ref { } } - const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice); + const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0); const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len); try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr }); break :blk ref; diff --git a/lib/std/Build/Watch/FsEvents.zig b/lib/std/Build/Watch/FsEvents.zig index dd22a5881df3cc888d9cc44a9e9f546f42ce3169..0a56ce182255176222ef4b9a7a7bbb22abc9350d 100644 --- a/lib/std/Build/Watch/FsEvents.zig +++ b/lib/std/Build/Watch/FsEvents.zig @@ -195,7 +195,7 @@ pub fn setPaths(fse: *FsEvents, gpa: Allocator, steps: []const *std.Build.Step) } else { fse.watch_roots = try gpa.realloc(fse.watch_roots, need_dirs.count()); for (fse.watch_roots, need_dirs.keys()) |*out, in| { - out.* = try paths_arena.dupeZ(u8, in); + out.* = try paths_arena.dupeSentinel(u8, in, 0); } } if (enable_debug_logs) { diff --git a/lib/std/Io/Dir.zig b/lib/std/Io/Dir.zig index 95388eac789f9a16d523bd28e10a16aaa591c18c..15785cfd8a69a04cf7682110dc84754424a7b306 100644 --- a/lib/std/Io/Dir.zig +++ b/lib/std/Io/Dir.zig @@ -944,7 +944,7 @@ pub const RealPathFileAllocError = RealPathFileError || Allocator.Error; pub fn realPathFileAlloc(dir: Dir, io: Io, sub_path: []const u8, allocator: Allocator) RealPathFileAllocError![:0]u8 { var buffer: [max_path_bytes]u8 = undefined; const n = try realPathFile(dir, io, sub_path, &buffer); - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } /// Same as `realPathFile` except `absolute_path` is asserted to be an absolute @@ -974,7 +974,7 @@ pub fn realPathFileAbsolute(io: Io, absolute_path: []const u8, out_buffer: []u8) pub fn realPathFileAbsoluteAlloc(io: Io, absolute_path: []const u8, allocator: Allocator) RealPathFileAllocError![:0]u8 { var buffer: [max_path_bytes]u8 = undefined; const n = try realPathFileAbsolute(io, absolute_path, &buffer); - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } pub const DeleteFileError = error{ diff --git a/lib/std/Io/Dispatch.zig b/lib/std/Io/Dispatch.zig index d24d8855a3acd6b8a44792feec9a4180cb6b8c73..8c1ba1c80559390bb39eb06dd1dccb054b80b9bf 100644 --- a/lib/std/Io/Dispatch.zig +++ b/lib/std/Io/Dispatch.zig @@ -4086,7 +4086,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -4222,7 +4222,7 @@ fn spawn(ev: *Evented, options: process.SpawnOptions) process.SpawnError!Spawned // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = if (prog_pipe[1] == -1) -1 else prog_fileno; diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index bc7fd2d2c8e6ac271c33099c18fb94fc0634615b..ff40b77139ee67442dc90fa821e06a50f0ed1dda 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -13851,7 +13851,7 @@ fn netLookupFallible( const name_c = name_buffer[0..name.len :0]; var port_buffer: [8]u8 = undefined; - const port_c = std.fmt.bufPrintZ(&port_buffer, "{d}", .{options.port}) catch unreachable; + const port_c = std.fmt.bufPrintSentinel(&port_buffer, "{d}", .{options.port}, 0) catch unreachable; const hints: posix.addrinfo = .{ .flags = .{ .CANONNAME = options.canonical_name_buffer != null, .NUMERICSERV = true }, @@ -14954,7 +14954,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -15061,7 +15061,7 @@ fn spawnPosix(t: *Threaded, options: process.SpawnOptions) process.SpawnError!Sp // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const prog_fileno = 3; comptime assert(@max(posix.STDIN_FILENO, posix.STDOUT_FILENO, posix.STDERR_FILENO) + 1 == prog_fileno); diff --git a/lib/std/Io/Uring.zig b/lib/std/Io/Uring.zig index 01821938e78b09df4cca44d10e7904bdead67825..accbf942963559c2a882fffa1639fe6895ff1f3a 100644 --- a/lib/std/Io/Uring.zig +++ b/lib/std/Io/Uring.zig @@ -4226,7 +4226,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -4369,7 +4369,7 @@ fn spawn(ev: *Evented, options: process.SpawnOptions) process.SpawnError!Spawned // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = if (prog_pipe[1] == -1) -1 else prog_fileno; diff --git a/lib/std/debug.zig b/lib/std/debug.zig index ff336e976beb8d0928b685f19f6bfe92360dcf09..a6f6df08bc6abacdda268f3b183a787316500c07 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -520,7 +520,7 @@ pub fn defaultPanic(msg: []const u8, first_trace_addr: ?usize) noreturn { if (uefi.system_table.boot_services) |bs| { // ExitData buffer must be allocated using boot_services.allocatePool (spec: page 220) - const exit_data = uefi.raw_pool_allocator.dupeZ(u16, exit_msg) catch @trap(); + const exit_data = uefi.raw_pool_allocator.dupeSentinel(u16, exit_msg, 0) catch @trap(); bs.exit(uefi.handle, .aborted, exit_data) catch {}; } @trap(); diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 1fccc065ded47eae0a743b9d43d98106e6314a40..b6b58ed747a691ca7d89b1f3b3b5ed3714932cfc 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -602,11 +602,6 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro return w.buffered(); } -/// Deprecated in favor of `bufPrintSentinel` -pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 { - return try bufPrintSentinel(buf, fmt, args, 0); -} - pub fn bufPrintSentinel( buf: []u8, comptime fmt: []const u8, diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 686ae840823e0ce4c12da76d4f8c8c5cb466b4c3..580804e1029bb4d4704ec1c56c928e89331956d2 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -135,7 +135,7 @@ const TestContext = struct { const allocator = self.arena.allocator(); const transformed_path = try self.transform_fn(allocator, self.io, self.dir, relative_path); if (native_os == .windows) { - const transformed_sep_path = try allocator.dupeZ(u8, transformed_path); + const transformed_sep_path = try allocator.dupeSentinel(u8, transformed_path, 0); std.mem.replaceScalar(u8, transformed_sep_path, switch (self.path_sep) { '/' => '\\', '\\' => '/', @@ -153,7 +153,7 @@ const TestContext = struct { pub fn toCanonicalPathSep(self: *TestContext, path: [:0]const u8) ![:0]const u8 { if (native_os == .windows) { const allocator = self.arena.allocator(); - const transformed_sep_path = try allocator.dupeZ(u8, path); + const transformed_sep_path = try allocator.dupeSentinel(u8, path, 0); std.mem.replaceScalar(u8, transformed_sep_path, '/', '\\'); return transformed_sep_path; } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 125f1688d3f1b41beb14e3259717555473beeb8d..f0b664f12e77ae032ada79b362c51decc3479f69 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -4964,7 +4964,7 @@ test isAligned { } test "freeing empty string with null-terminated sentinel" { - const empty_string = try testing.allocator.dupeZ(u8, ""); + const empty_string = try testing.allocator.dupeSentinel(u8, "", 0); testing.allocator.free(empty_string); } diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 00298ae71ee144513b3fec1481d0ffb52594b664..ec3426146cc0b93f1ea9b7e4a364cc61ab6d641f 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -456,12 +456,6 @@ pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) Error![]T { return new_buf; } -/// Deprecated in favor of `dupeSentinel` -/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. -pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T { - return allocator.dupeSentinel(T, m, 0); -} - /// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. pub fn dupeSentinel( allocator: Allocator, diff --git a/lib/std/os/plan9.zig b/lib/std/os/plan9.zig index 36bf83b21c02f42bf1f24f439a355eea9df78a13..48e82c3e11405f5482b2b7b85a715ee7329faf08 100644 --- a/lib/std/os/plan9.zig +++ b/lib/std/os/plan9.zig @@ -299,7 +299,7 @@ pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, _: mode_t) usize { const dir_path = std.mem.span(@as([*:0]u8, @ptrCast(&dir_path_buf))); const total_path = std.fs.path.join(alloc, &.{ dir_path, std.mem.span(path) }) catch unreachable; // the allocation shouldn't fail because it should not exceed max_path_bytes fba.reset(); - const total_path_z = alloc.dupeZ(u8, total_path) catch unreachable; // should not exceed max_path_bytes + 1 + const total_path_z = alloc.dupeSentinel(u8, total_path, 0) catch unreachable; // should not exceed max_path_bytes + 1 return open(total_path_z.ptr, flags); } diff --git a/lib/std/process.zig b/lib/std/process.zig index e70505ba8b0522bb4a16f66d2b640713668bc38e..cfb57838392b92af6bfbfdb2c2fce4b9342eb646 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -86,7 +86,7 @@ pub fn currentPathAlloc(io: Io, allocator: Allocator) CurrentPathAllocError![:0] error.NameTooLong => unreachable, else => |e| return e, }; - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } test currentPathAlloc { @@ -735,7 +735,7 @@ pub fn executablePathAlloc(io: Io, allocator: Allocator) ExecutablePathAllocErro error.NameTooLong => unreachable, else => |e| return e, }; - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } pub const ExecutablePathError = ExecutablePathBaseError || error{NameTooLong}; diff --git a/lib/std/process/Environ.zig b/lib/std/process/Environ.zig index 57d5ea02711885f583f60c65fd26adfb3712f11b..3f467a8db936c6ee0720e1921f46cebc08f0111f 100644 --- a/lib/std/process/Environ.zig +++ b/lib/std/process/Environ.zig @@ -754,7 +754,7 @@ pub fn createPosixBlock( }, .nothing => {}, }; - envp[envp_len] = try gpa.dupeZ(u8, mem.span(entry)); + envp[envp_len] = try gpa.dupeSentinel(u8, mem.span(entry), 0); envp_len += 1; } diff --git a/lib/std/zig/LibCInstallation.zig b/lib/std/zig/LibCInstallation.zig index 0cf9dcfb5454a855edbab1ae34d97f87051eb2ae..9d7995a69af007b2bf8dd58cb58290ee25618082 100644 --- a/lib/std/zig/LibCInstallation.zig +++ b/lib/std/zig/LibCInstallation.zig @@ -70,7 +70,7 @@ pub fn parse(allocator: Allocator, io: Io, libc_file: []const u8, target: *const if (value.len == 0) { @field(self, field.name) = null; } else { - found_keys[i].allocated = try allocator.dupeZ(u8, value); + found_keys[i].allocated = try allocator.dupeSentinel(u8, value, 0); @field(self, field.name) = found_keys[i].allocated; } break; diff --git a/src/Compilation.zig b/src/Compilation.zig index 4901eebe95151e896b15416edefe3297e4377ca2..3a70e05f5dde53b1f58911096b233b1c05c9c2b0 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1869,7 +1869,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic, const comp: *Compilation = comp: { // We put the `Compilation` itself in the arena. Freeing the arena will free the module. // It's initialized later after we prepare the initialization options. - const root_name = try arena.dupeZ(u8, options.root_name); + const root_name = try arena.dupeSentinel(u8, options.root_name, 0); // The "any" values provided by resolved config only account for // explicitly-provided settings. We now make them additionally account diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 73790b5ea56a5dad5c2218f6675c26897be3d436..967ed386587144e08fd1c0995b986ecebe3a36a6 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -918,7 +918,7 @@ pub const Object = struct { } const target_triple_sentinel = - try o.gpa.dupeZ(u8, o.builder.target_triple.slice(&o.builder).?); + try o.gpa.dupeSentinel(u8, o.builder.target_triple.slice(&o.builder).?, 0); defer o.gpa.free(target_triple_sentinel); const emit_asm_msg = options.asm_path orelse "(none)"; diff --git a/src/link/Lld.zig b/src/link/Lld.zig index eba1ee6a8dcec4289f8bb5579b6e7ad7e4cea79b..5eb1e4e4467318ba972c59809ce28f00b66a3e79 100644 --- a/src/link/Lld.zig +++ b/src/link/Lld.zig @@ -287,7 +287,7 @@ fn linkAsArchive(lld: *Lld, arena: Allocator) !void { const comp = base.comp; const directory = base.emit.root_dir; // Just an alias to make it shorter to type. const full_out_path = try directory.join(arena, &[_][]const u8{base.emit.sub_path}); - const full_out_path_z = try arena.dupeZ(u8, full_out_path); + const full_out_path_z = try arena.dupeSentinel(u8, full_out_path, 0); const opt_zcu = comp.zcu; const zcu_obj_path: ?Cache.Path = if (opt_zcu != null) p: { @@ -326,7 +326,7 @@ fn linkAsArchive(lld: *Lld, arena: Allocator) !void { object_files.appendAssumeCapacity(try key.status.success.object_path.toStringZ(arena)); } for (comp.win32_resource_table.keys()) |key| { - object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.res_path)); + object_files.appendAssumeCapacity(try arena.dupeSentinel(u8, key.status.success.res_path, 0)); } if (zcu_obj_path) |p| object_files.appendAssumeCapacity(try p.toStringZ(arena)); if (compiler_rt_path) |p| object_files.appendAssumeCapacity(try p.toStringZ(arena)); diff --git a/src/main.zig b/src/main.zig index 8a8b5f7a5ab5759408bdd3a2b301427c464779c2..875cddf533ee2efbd5caede36584720e206f6d69 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5969,7 +5969,7 @@ extern "c" fn ZigLlvmAr_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int; fn argsCopyZ(alloc: Allocator, args: []const []const u8) ![:null]?[*:0]u8 { var argv = try alloc.allocSentinel(?[*:0]u8, args.len, null); for (args, 0..) |arg, i| { - argv[i] = try alloc.dupeZ(u8, arg); // TODO If there was an argsAllocZ we could avoid this allocation. + argv[i] = try alloc.dupeSentinel(u8, arg, 0); // TODO If there was an argsAllocZ we could avoid this allocation. } return argv; } @@ -6599,7 +6599,7 @@ fn cmdDumpLlvmInts( if (!build_options.have_llvm) fatal("compiler does not use LLVM; cannot dump LLVM integer sizes", .{}); - const triple = try arena.dupeZ(u8, args[0]); + const triple = try arena.dupeSentinel(u8, args[0], 0); const llvm = @import("codegen/llvm/bindings.zig"); diff --git a/tools/docgen.zig b/tools/docgen.zig index 33ab5ca3cba7fd2a27f887b29daf468aad32c5f7..ab9419d55ab98eff79e07c01a831f6ec07168728 100644 --- a/tools/docgen.zig +++ b/tools/docgen.zig @@ -679,7 +679,7 @@ fn tokenizeAndPrintRaw( raw_src: []const u8, ) !void { const src_non_terminated = mem.trim(u8, raw_src, " \r\n"); - const src = try allocator.dupeZ(u8, src_non_terminated); + const src = try allocator.dupeSentinel(u8, src_non_terminated, 0); try out.writeAll(""); var tokenizer = std.zig.Tokenizer.init(src); diff --git a/tools/doctest.zig b/tools/doctest.zig index fde277118ef44b3f3407f788f3dee56561f72592..90c7108d297acaf6db6da02eb29b87af2ac151a9 100644 --- a/tools/doctest.zig +++ b/tools/doctest.zig @@ -607,7 +607,7 @@ fn printSourceBlock(arena: Allocator, out: *Writer, source_bytes: []const u8, na fn tokenizeAndPrint(arena: Allocator, out: *Writer, raw_src: []const u8) !void { const src_non_terminated = mem.trim(u8, raw_src, " \r\n"); - const src = try arena.dupeZ(u8, src_non_terminated); + const src = try arena.dupeSentinel(u8, src_non_terminated, 0); try out.writeAll(""); var tokenizer = std.zig.Tokenizer.init(src);