From 15d415e10b81a66fa3b887fb2a0c20bbcd614d94 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 24 Nov 2019 21:12:01 -0500 Subject: [PATCH] make std.mem.toSlice use null terminated pointers and fix the fallout --- lib/std/buffer.zig | 9 ++----- lib/std/c.zig | 2 +- lib/std/fs.zig | 2 +- lib/std/mem.zig | 6 ++--- lib/std/net.zig | 4 +-- lib/std/os.zig | 31 +++++++++++----------- lib/std/os/linux/vdso.zig | 6 +++-- lib/std/special/start.zig | 12 ++++----- src-self-hosted/clang.zig | 6 ++--- src-self-hosted/compilation.zig | 6 ++--- src-self-hosted/llvm.zig | 44 +++++++++++++++---------------- src-self-hosted/stage1.zig | 4 +-- src-self-hosted/translate_c.zig | 2 +- src-self-hosted/util.zig | 6 ++--- src/analyze.cpp | 6 +++-- test/stage1/behavior/cast.zig | 2 +- test/stage1/behavior/pointers.zig | 3 ++- 17 files changed, 76 insertions(+), 75 deletions(-) diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig index 24bd23fa7..37414d64d 100644 --- a/lib/std/buffer.zig +++ b/lib/std/buffer.zig @@ -72,11 +72,11 @@ pub const Buffer = struct { self.list.deinit(); } - pub fn toSlice(self: Buffer) []u8 { + pub fn toSlice(self: Buffer) [:0]u8 { return self.list.toSlice()[0..self.len()]; } - pub fn toSliceConst(self: Buffer) []const u8 { + pub fn toSliceConst(self: Buffer) [:0]const u8 { return self.list.toSliceConst()[0..self.len()]; } @@ -131,11 +131,6 @@ pub const Buffer = struct { try self.resize(m.len); mem.copy(u8, self.list.toSlice(), m); } - - /// For passing to C functions. - pub fn ptr(self: Buffer) [*]u8 { - return self.list.items.ptr; - } }; test "simple Buffer" { diff --git a/lib/std/c.zig b/lib/std/c.zig index fac13efc6..33061b662 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -110,7 +110,7 @@ pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int; pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int; pub extern "c" fn rmdir(path: [*]const u8) c_int; -pub extern "c" fn getenv(name: [*]const u8) ?[*]u8; +pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8; pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 0b9c6559d..d5a793097 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -533,7 +533,7 @@ pub const Dir = struct { const next_index = self.index + linux_entry.reclen(); self.index = next_index; - const name = mem.toSlice(u8, @ptrCast([*]u8, &linux_entry.d_name)); + const name = mem.toSlice(u8, @ptrCast([*:0]u8, &linux_entry.d_name)); // skip . and .. entries if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { diff --git a/lib/std/mem.zig b/lib/std/mem.zig index f790dd683..412bf9b64 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -356,17 +356,17 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { return true; } -pub fn len(comptime T: type, ptr: [*]const T) usize { +pub fn len(comptime T: type, ptr: [*:0]const T) usize { var count: usize = 0; while (ptr[count] != 0) : (count += 1) {} return count; } -pub fn toSliceConst(comptime T: type, ptr: [*]const T) []const T { +pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T { return ptr[0..len(T, ptr)]; } -pub fn toSlice(comptime T: type, ptr: [*]T) []T { +pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T { return ptr[0..len(T, ptr)]; } diff --git a/lib/std/net.zig b/lib/std/net.zig index 5e194d73c..9a602c010 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -360,7 +360,7 @@ pub const Address = extern union { unreachable; } - const path_len = std.mem.len(u8, &self.un.path); + const path_len = std.mem.len(u8, @ptrCast([*:0]const u8, &self.un.path)); return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len); }, else => unreachable, @@ -1271,7 +1271,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8) var tmp: [256]u8 = undefined; // Returns len of compressed name. strlen to get canon name. _ = try os.dn_expand(packet, data, &tmp); - const canon_name = mem.toSliceConst(u8, &tmp); + const canon_name = mem.toSliceConst(u8, @ptrCast([*:0]const u8, &tmp)); if (isValidHostName(canon_name)) { try ctx.canon.replaceContents(canon_name); } diff --git a/lib/std/os.zig b/lib/std/os.zig index 28a88beb0..9035d8ecb 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -66,12 +66,12 @@ pub const system = if (builtin.link_libc) std.c else switch (builtin.os) { pub usingnamespace @import("os/bits.zig"); /// See also `getenv`. Populated by startup code before main(). -pub var environ: [][*]u8 = undefined; +pub var environ: [][*:0]u8 = undefined; /// Populated by startup code before main(). /// Not available on Windows. See `std.process.args` /// for obtaining the process arguments. -pub var argv: [][*]u8 = undefined; +pub var argv: [][*:0]u8 = undefined; /// To obtain errno, call this function with the return value of the /// system function call. For some systems this will obtain the value directly @@ -784,7 +784,7 @@ pub fn execveC(path: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]co /// matching the syscall API on all targets. This removes the need for an allocator. /// This function also uses the PATH environment variable to get the full path to the executable. /// If `file` is an absolute path, this is the same as `execveC`. -pub fn execvpeC(file: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) ExecveError { +pub fn execvpeC(file: [*:0]const u8, child_argv: [*]const ?[*:0]const u8, envp: [*]const ?[*:0]const u8) ExecveError { const file_slice = mem.toSliceConst(u8, file); if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveC(file, child_argv, envp); @@ -820,8 +820,8 @@ pub fn execvpe( argv_slice: []const []const u8, env_map: *const std.BufMap, ) (ExecveError || error{OutOfMemory}) { - const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1); - mem.set(?[*]u8, argv_buf, null); + const argv_buf = try allocator.alloc(?[*:0]u8, argv_slice.len + 1); + mem.set(?[*:0]u8, argv_buf, null); defer { for (argv_buf) |arg| { const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break; @@ -834,7 +834,8 @@ pub fn execvpe( @memcpy(arg_buf.ptr, arg.ptr, arg.len); arg_buf[arg.len] = 0; - argv_buf[i] = arg_buf.ptr; + // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731 + argv_buf[i] = @ptrCast([*:0]u8, arg_buf.ptr); } argv_buf[argv_slice.len] = null; @@ -844,10 +845,10 @@ pub fn execvpe( return execvpeC(argv_buf.ptr[0].?, argv_buf.ptr, envp_buf.ptr); } -pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 { +pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*:0]u8 { const envp_count = env_map.count(); - const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1); - mem.set(?[*]u8, envp_buf, null); + const envp_buf = try allocator.alloc(?[*:0]u8, envp_count + 1); + mem.set(?[*:0]u8, envp_buf, null); errdefer freeNullDelimitedEnvMap(allocator, envp_buf); { var it = env_map.iterator(); @@ -859,7 +860,8 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std. @memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len); env_buf[env_buf.len - 1] = 0; - envp_buf[i] = env_buf.ptr; + // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731 + envp_buf[i] = @ptrCast([*:0]u8, env_buf.ptr); } assert(i == envp_count); } @@ -867,7 +869,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std. return envp_buf; } -pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void { +pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void { for (envp_buf) |env| { const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; allocator.free(env_buf); @@ -896,8 +898,7 @@ pub fn getenv(key: []const u8) ?[]const u8 { /// Get an environment variable with a null-terminated name. /// See also `getenv`. -/// TODO https://github.com/ziglang/zig/issues/265 -pub fn getenvC(key: [*]const u8) ?[]const u8 { +pub fn getenvC(key: [*:0]const u8) ?[]const u8 { if (builtin.link_libc) { const value = system.getenv(key) orelse return null; return mem.toSliceConst(u8, value); @@ -922,7 +923,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); }; switch (err) { - 0 => return mem.toSlice(u8, out_buffer.ptr), + 0 => return mem.toSlice(u8, @ptrCast([*:0]u8, out_buffer.ptr)), EFAULT => unreachable, EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, @@ -2865,7 +2866,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { var uts: utsname = undefined; switch (errno(system.uname(&uts))) { 0 => { - const hostname = mem.toSlice(u8, &uts.nodename); + const hostname = mem.toSlice(u8, @ptrCast([*:0]u8, &uts.nodename)); mem.copy(u8, name_buffer, hostname); return name_buffer[0..hostname.len]; }, diff --git a/lib/std/os/linux/vdso.zig b/lib/std/os/linux/vdso.zig index d3e00b867..868eb26c6 100644 --- a/lib/std/os/linux/vdso.zig +++ b/lib/std/os/linux/vdso.zig @@ -65,7 +65,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == syms[i].st_shndx) continue; - if (!mem.eql(u8, name, mem.toSliceConst(u8, strings + syms[i].st_name))) continue; + const sym_name = @ptrCast([*:0]const u8, strings + syms[i].st_name); + if (!mem.eql(u8, name, mem.toSliceConst(u8, sym_name))) continue; if (maybe_versym) |versym| { if (!checkver(maybe_verdef.?, versym[i], vername, strings)) continue; @@ -87,5 +88,6 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); } const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); - return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); + const vda_name = @ptrCast([*:0]const u8, strings + aux.vda_name); + return mem.eql(u8, vername, mem.toSliceConst(u8, vda_name)); } diff --git a/lib/std/special/start.zig b/lib/std/special/start.zig index c56d50d7c..6d99618d2 100644 --- a/lib/std/special/start.zig +++ b/lib/std/special/start.zig @@ -123,12 +123,12 @@ fn posixCallMainAndExit() noreturn { @setAlignStack(16); } const argc = starting_stack_ptr[0]; - const argv = @ptrCast([*][*]u8, starting_stack_ptr + 1); + const argv = @ptrCast([*][*:0]u8, starting_stack_ptr + 1); - const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1); + const envp_optional = @ptrCast([*:null]?[*:0]u8, argv + argc + 1); var envp_count: usize = 0; while (envp_optional[envp_count]) |_| : (envp_count += 1) {} - const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; + const envp = @ptrCast([*][*:0]u8, envp_optional)[0..envp_count]; if (builtin.os == .linux) { // Find the beginning of the auxiliary vector @@ -168,7 +168,7 @@ fn posixCallMainAndExit() noreturn { std.os.exit(@inlineCall(callMainWithArgs, argc, argv, envp)); } -fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { +fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 { std.os.argv = argv[0..argc]; std.os.environ = envp; @@ -177,10 +177,10 @@ fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { return initEventLoopAndCallMain(); } -extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 { +extern fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) i32 { var env_count: usize = 0; while (c_envp[env_count] != null) : (env_count += 1) {} - const envp = @ptrCast([*][*]u8, c_envp)[0..env_count]; + const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count]; return @inlineCall(callMainWithArgs, @intCast(usize, c_argc), c_argv, envp); } diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index 8f3e53916..31907ff26 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -708,7 +708,7 @@ pub const ZigClangStringLiteral_StringKind = extern enum { }; pub extern fn ZigClangSourceManager_getSpellingLoc(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation; -pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*]const u8; +pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*:0]const u8; pub extern fn ZigClangSourceManager_getSpellingLineNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint; pub extern fn ZigClangSourceManager_getSpellingColumnNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint; pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8; @@ -746,7 +746,7 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass; pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType; pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool; -pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*]const u8; +pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*:0]const u8; pub extern fn ZigClangStmt_getBeginLoc(self: *const struct_ZigClangStmt) struct_ZigClangSourceLocation; pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) ZigClangStmtClass; pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool; @@ -904,7 +904,7 @@ pub extern fn ZigClangLoadFromCommandLine( ) ?*ZigClangASTUnit; pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind; -pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*]const u8; +pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*:0]const u8; pub const ZigClangCompoundStmt_const_body_iterator = [*c]const *struct_ZigClangStmt; diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 1d4ed757d..2f0de0c52 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -490,8 +490,8 @@ pub const Compilation = struct { // LLVM creates invalid binaries on Windows sometimes. // See https://github.com/ziglang/zig/issues/508 // As a workaround we do not use target native features on Windows. - var target_specific_cpu_args: ?[*]u8 = null; - var target_specific_cpu_features: ?[*]u8 = null; + var target_specific_cpu_args: ?[*:0]u8 = null; + var target_specific_cpu_features: ?[*:0]u8 = null; defer llvm.DisposeMessage(target_specific_cpu_args); defer llvm.DisposeMessage(target_specific_cpu_features); if (target == Target.Native and !target.isWindows()) { @@ -501,7 +501,7 @@ pub const Compilation = struct { comp.target_machine = llvm.CreateTargetMachine( comp.llvm_target, - comp.llvm_triple.ptr(), + comp.llvm_triple.toSliceConst(), target_specific_cpu_args orelse "", target_specific_cpu_features orelse "", opt_level, diff --git a/src-self-hosted/llvm.zig b/src-self-hosted/llvm.zig index c8eba2cd2..af42adb97 100644 --- a/src-self-hosted/llvm.zig +++ b/src-self-hosted/llvm.zig @@ -83,16 +83,16 @@ pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext; pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext; pub const AddGlobal = LLVMAddGlobal; -extern fn LLVMAddGlobal(M: *Module, Ty: *Type, Name: [*]const u8) ?*Value; +extern fn LLVMAddGlobal(M: *Module, Ty: *Type, Name: [*:0]const u8) ?*Value; pub const ConstStringInContext = LLVMConstStringInContext; -extern fn LLVMConstStringInContext(C: *Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) ?*Value; +extern fn LLVMConstStringInContext(C: *Context, Str: [*:0]const u8, Length: c_uint, DontNullTerminate: Bool) ?*Value; pub const ConstInt = LLVMConstInt; extern fn LLVMConstInt(IntTy: *Type, N: c_ulonglong, SignExtend: Bool) ?*Value; pub const BuildLoad = LLVMBuildLoad; -extern fn LLVMBuildLoad(arg0: *Builder, PointerVal: *Value, Name: [*]const u8) ?*Value; +extern fn LLVMBuildLoad(arg0: *Builder, PointerVal: *Value, Name: [*:0]const u8) ?*Value; pub const ConstNull = LLVMConstNull; extern fn LLVMConstNull(Ty: *Type) ?*Value; @@ -110,24 +110,24 @@ pub const CreateEnumAttribute = LLVMCreateEnumAttribute; extern fn LLVMCreateEnumAttribute(C: *Context, KindID: c_uint, Val: u64) ?*Attribute; pub const AddFunction = LLVMAddFunction; -extern fn LLVMAddFunction(M: *Module, Name: [*]const u8, FunctionTy: *Type) ?*Value; +extern fn LLVMAddFunction(M: *Module, Name: [*:0]const u8, FunctionTy: *Type) ?*Value; pub const CreateCompileUnit = ZigLLVMCreateCompileUnit; extern fn ZigLLVMCreateCompileUnit( dibuilder: *DIBuilder, lang: c_uint, difile: *DIFile, - producer: [*]const u8, + producer: [*:0]const u8, is_optimized: bool, - flags: [*]const u8, + flags: [*:0]const u8, runtime_version: c_uint, - split_name: [*]const u8, + split_name: [*:0]const u8, dwo_id: u64, emit_debug_info: bool, ) ?*DICompileUnit; pub const CreateFile = ZigLLVMCreateFile; -extern fn ZigLLVMCreateFile(dibuilder: *DIBuilder, filename: [*]const u8, directory: [*]const u8) ?*DIFile; +extern fn ZigLLVMCreateFile(dibuilder: *DIBuilder, filename: [*:0]const u8, directory: [*:0]const u8) ?*DIFile; pub const ArrayType = LLVMArrayType; extern fn LLVMArrayType(ElementType: *Type, ElementCount: c_uint) ?*Type; @@ -145,7 +145,7 @@ pub const IntTypeInContext = LLVMIntTypeInContext; extern fn LLVMIntTypeInContext(C: *Context, NumBits: c_uint) ?*Type; pub const ModuleCreateWithNameInContext = LLVMModuleCreateWithNameInContext; -extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*]const u8, C: *Context) ?*Module; +extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*:0]const u8, C: *Context) ?*Module; pub const VoidTypeInContext = LLVMVoidTypeInContext; extern fn LLVMVoidTypeInContext(C: *Context) ?*Type; @@ -157,7 +157,7 @@ pub const ContextDispose = LLVMContextDispose; extern fn LLVMContextDispose(C: *Context) void; pub const CopyStringRepOfTargetData = LLVMCopyStringRepOfTargetData; -extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8; +extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*:0]u8; pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout; extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData; @@ -165,9 +165,9 @@ extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData; pub const CreateTargetMachine = ZigLLVMCreateTargetMachine; extern fn ZigLLVMCreateTargetMachine( T: *Target, - Triple: [*]const u8, - CPU: [*]const u8, - Features: [*]const u8, + Triple: [*:0]const u8, + CPU: [*:0]const u8, + Features: [*:0]const u8, Level: CodeGenOptLevel, Reloc: RelocMode, CodeModel: CodeModel, @@ -175,10 +175,10 @@ extern fn ZigLLVMCreateTargetMachine( ) ?*TargetMachine; pub const GetHostCPUName = LLVMGetHostCPUName; -extern fn LLVMGetHostCPUName() ?[*]u8; +extern fn LLVMGetHostCPUName() ?[*:0]u8; pub const GetNativeFeatures = ZigLLVMGetNativeFeatures; -extern fn ZigLLVMGetNativeFeatures() ?[*]u8; +extern fn ZigLLVMGetNativeFeatures() ?[*:0]u8; pub const GetElementType = LLVMGetElementType; extern fn LLVMGetElementType(Ty: *Type) *Type; @@ -190,16 +190,16 @@ pub const BuildStore = LLVMBuildStore; extern fn LLVMBuildStore(arg0: *Builder, Val: *Value, Ptr: *Value) ?*Value; pub const BuildAlloca = LLVMBuildAlloca; -extern fn LLVMBuildAlloca(arg0: *Builder, Ty: *Type, Name: ?[*]const u8) ?*Value; +extern fn LLVMBuildAlloca(arg0: *Builder, Ty: *Type, Name: ?[*:0]const u8) ?*Value; pub const ConstInBoundsGEP = LLVMConstInBoundsGEP; pub extern fn LLVMConstInBoundsGEP(ConstantVal: *Value, ConstantIndices: [*]*Value, NumIndices: c_uint) ?*Value; pub const GetTargetFromTriple = LLVMGetTargetFromTriple; -extern fn LLVMGetTargetFromTriple(Triple: [*]const u8, T: **Target, ErrorMessage: ?*[*]u8) Bool; +extern fn LLVMGetTargetFromTriple(Triple: [*:0]const u8, T: **Target, ErrorMessage: ?*[*:0]u8) Bool; pub const VerifyModule = LLVMVerifyModule; -extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool; +extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*:0]u8) Bool; pub const GetInsertBlock = LLVMGetInsertBlock; extern fn LLVMGetInsertBlock(Builder: *Builder) *BasicBlock; @@ -216,7 +216,7 @@ pub const GetParam = LLVMGetParam; extern fn LLVMGetParam(Fn: *Value, Index: c_uint) *Value; pub const AppendBasicBlockInContext = LLVMAppendBasicBlockInContext; -extern fn LLVMAppendBasicBlockInContext(C: *Context, Fn: *Value, Name: [*]const u8) ?*BasicBlock; +extern fn LLVMAppendBasicBlockInContext(C: *Context, Fn: *Value, Name: [*:0]const u8) ?*BasicBlock; pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd; extern fn LLVMPositionBuilderAtEnd(Builder: *Builder, Block: *BasicBlock) void; @@ -278,14 +278,14 @@ pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile; extern fn ZigLLVMTargetMachineEmitToFile( targ_machine_ref: *TargetMachine, module_ref: *Module, - filename: [*]const u8, + filename: [*:0]const u8, output_type: EmitOutputType, - error_message: *[*]u8, + error_message: *[*:0]u8, is_debug: bool, is_small: bool, ) bool; pub const BuildCall = ZigLLVMBuildCall; -extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value; +extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*:0]const u8) ?*Value; pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage; diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index c746cb5be..7220c3d0d 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -144,7 +144,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error { // TODO: just use the actual self-hosted zig fmt. Until https://github.com/ziglang/zig/issues/2377, // we use a blocking implementation. -export fn stage2_fmt(argc: c_int, argv: [*]const [*]const u8) c_int { +export fn stage2_fmt(argc: c_int, argv: [*]const [*:0]const u8) c_int { if (std.debug.runtime_safety) { fmtMain(argc, argv) catch unreachable; } else { @@ -156,7 +156,7 @@ export fn stage2_fmt(argc: c_int, argv: [*]const [*]const u8) c_int { return 0; } -fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { +fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void { const allocator = std.heap.c_allocator; var args_list = std.ArrayList([]const u8).init(allocator); const argc_usize = @intCast(usize, argc); diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index ebc23e2ce..0470ab92d 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -113,7 +113,7 @@ const Context = struct { } /// Convert a null-terminated C string to a slice allocated in the arena - fn str(c: *Context, s: [*]const u8) ![]u8 { + fn str(c: *Context, s: [*:0]const u8) ![]u8 { return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s)); } diff --git a/src-self-hosted/util.zig b/src-self-hosted/util.zig index c1e2393d0..f1d892411 100644 --- a/src-self-hosted/util.zig +++ b/src-self-hosted/util.zig @@ -172,9 +172,9 @@ pub fn getDarwinArchString(self: Target) []const u8 { pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target { var result: *llvm.Target = undefined; - var err_msg: [*]u8 = undefined; - if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) { - std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg); + var err_msg: [*:0]u8 = undefined; + if (llvm.GetTargetFromTriple(triple.toSlice(), &result, &err_msg) != 0) { + std.debug.warn("triple: {s} error: {s}\n", triple.toSlice(), err_msg); return error.UnsupportedTarget; } return result; diff --git a/src/analyze.cpp b/src/analyze.cpp index cd00e4d30..7c9171d74 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -7792,7 +7792,8 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wa bool done = false; if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile || - ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero) + ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero || + ptr_type->data.pointer.sentinel != nullptr) { ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false, PtrLenUnknown, 0, 0, 0, false); @@ -7811,7 +7812,8 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wa ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index]->type_entry; assert(child_ptr_type->id == ZigTypeIdPointer); if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile || - child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero) + child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero || + child_ptr_type->data.pointer.sentinel != nullptr) { ZigType *grand_child_type = child_ptr_type->data.pointer.child_type; ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false, diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index 868b8d12e..fec4494ad 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -348,7 +348,7 @@ fn testCastPtrOfArrayToSliceAndPtr() void { test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{"window name"}; const x: [*]const ?[*]const u8 = &window_name; - expect(mem.eql(u8, std.mem.toSliceConst(u8, x[0].?), "window name")); + expect(mem.eql(u8, std.mem.toSliceConst(u8, @ptrCast([*:0]const u8, x[0].?)), "window name")); } test "@intCast comptime_int" { diff --git a/test/stage1/behavior/pointers.zig b/test/stage1/behavior/pointers.zig index 89c6b2987..58a46b539 100644 --- a/test/stage1/behavior/pointers.zig +++ b/test/stage1/behavior/pointers.zig @@ -207,7 +207,8 @@ test "null terminated pointer" { var array_with_zero = [_:0]u8{'h', 'e', 'l', 'l', 'o'}; var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero); var no_zero_ptr: [*]const u8 = zero_ptr; - expect(std.mem.eql(u8, std.mem.toSliceConst(u8, no_zero_ptr), "hello")); + var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr); + expect(std.mem.eql(u8, std.mem.toSliceConst(u8, zero_ptr_again), "hello")); } }; S.doTheTest();