diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-01-15 17:34:12 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-01-16 12:49:58 +0000 |
| commit | 9804cc8bc6fe83b2a0cd5b61b8d2fc5d458cb221 (patch) | |
| tree | d2f7a675b37c8f94db9b1015589a3b37805e2ac6 /lib/std | |
| parent | 89a9cabafd745034871ea014b06bd3bad0505f4a (diff) | |
| download | zig-9804cc8bc6fe83b2a0cd5b61b8d2fc5d458cb221.tar.gz zig-9804cc8bc6fe83b2a0cd5b61b8d2fc5d458cb221.zip | |
all: update to `std.builtin.Type.{Pointer,Array,StructField}` field renames
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Build/Step/Options.zig | 4 | ||||
| -rw-r--r-- | lib/std/crypto/phc_encoding.zig | 2 | ||||
| -rw-r--r-- | lib/std/enums.zig | 2 | ||||
| -rw-r--r-- | lib/std/fmt.zig | 2 | ||||
| -rw-r--r-- | lib/std/io.zig | 2 | ||||
| -rw-r--r-- | lib/std/json/static.zig | 20 | ||||
| -rw-r--r-- | lib/std/json/stringify.zig | 2 | ||||
| -rw-r--r-- | lib/std/mem.zig | 75 | ||||
| -rw-r--r-- | lib/std/mem/Allocator.zig | 2 | ||||
| -rw-r--r-- | lib/std/meta.zig | 25 | ||||
| -rw-r--r-- | lib/std/meta/trailer_flags.zig | 2 | ||||
| -rw-r--r-- | lib/std/multi_array_list.zig | 2 | ||||
| -rw-r--r-- | lib/std/zig/c_translation.zig | 7 |
13 files changed, 59 insertions, 88 deletions
diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 5a7332d9d8..dd09c0b5c0 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -318,9 +318,7 @@ fn printStruct(options: *Options, out: anytype, comptime T: type, comptime val: try out.print(" {p_}: {s}", .{ std.zig.fmtId(field.name), type_name }); } - if (field.default_value != null) { - const default_value = @as(*field.type, @ptrCast(@alignCast(@constCast(field.default_value.?)))).*; - + if (field.defaultValue()) |default_value| { try out.writeAll(" = "); switch (@typeInfo(@TypeOf(default_value))) { .@"enum" => try out.print(".{s},\n", .{@tagName(default_value)}), diff --git a/lib/std/crypto/phc_encoding.zig b/lib/std/crypto/phc_encoding.zig index ba48a9954f..ee814861b3 100644 --- a/lib/std/crypto/phc_encoding.zig +++ b/lib/std/crypto/phc_encoding.zig @@ -164,7 +164,7 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult // with default values var expected_fields: usize = 0; inline for (comptime meta.fields(HashResult)) |p| { - if (@typeInfo(p.type) != .optional and p.default_value == null) { + if (@typeInfo(p.type) != .optional and p.default_value_ptr == null) { expected_fields += 1; } } diff --git a/lib/std/enums.zig b/lib/std/enums.zig index aebfe2a18a..cb928ac023 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -19,7 +19,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def struct_field.* = .{ .name = enum_field.name ++ "", .type = Data, - .default_value = if (field_default) |d| @as(?*const anyopaque, @ptrCast(&d)) else null, + .default_value_ptr = if (field_default) |d| @as(?*const anyopaque, @ptrCast(&d)) else null, .is_comptime = false, .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0, }; diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index a044bbe608..2316b32d5d 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -633,7 +633,7 @@ pub fn formatType( .many, .c => { if (actual_fmt.len == 0) @compileError("cannot format pointer without a specifier (i.e. {s} or {*})"); - if (ptr_info.sentinel) |_| { + if (ptr_info.sentinel() != null) { return formatType(mem.span(value), actual_fmt, options, writer, max_depth); } if (actual_fmt[0] == 's' and ptr_info.child == u8) { diff --git a/lib/std/io.zig b/lib/std/io.zig index 7336d85cd1..bd9b0042b9 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -805,7 +805,7 @@ pub fn PollFiles(comptime StreamEnum: type) type { struct_field.* = .{ .name = enum_field.name ++ "", .type = fs.File, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(fs.File), }; diff --git a/lib/std/json/static.zig b/lib/std/json/static.zig index d2972be7b9..9cdb6226f2 100644 --- a/lib/std/json/static.zig +++ b/lib/std/json/static.zig @@ -476,9 +476,8 @@ pub fn innerParse( arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options)); } - if (ptrInfo.sentinel) |some| { - const sentinel_value = @as(*align(1) const ptrInfo.child, @ptrCast(some)).*; - return try arraylist.toOwnedSliceSentinel(sentinel_value); + if (ptrInfo.sentinel()) |s| { + return try arraylist.toOwnedSliceSentinel(s); } return try arraylist.toOwnedSlice(); @@ -487,11 +486,11 @@ pub fn innerParse( if (ptrInfo.child != u8) return error.UnexpectedToken; // Dynamic length string. - if (ptrInfo.sentinel) |sentinel_ptr| { + if (ptrInfo.sentinel()) |s| { // Use our own array list so we can append the sentinel. var value_list = ArrayList(u8).init(allocator); _ = try source.allocNextIntoArrayList(&value_list, .alloc_always); - return try value_list.toOwnedSliceSentinel(@as(*const u8, @ptrCast(sentinel_ptr)).*); + return try value_list.toOwnedSliceSentinel(s); } if (ptrInfo.is_const) { switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) { @@ -714,8 +713,8 @@ pub fn innerParseFromValue( .slice => { switch (source) { .array => |array| { - const r = if (ptrInfo.sentinel) |sentinel_ptr| - try allocator.allocSentinel(ptrInfo.child, array.items.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*) + const r = if (ptrInfo.sentinel()) |sentinel| + try allocator.allocSentinel(ptrInfo.child, array.items.len, sentinel) else try allocator.alloc(ptrInfo.child, array.items.len); @@ -729,8 +728,8 @@ pub fn innerParseFromValue( if (ptrInfo.child != u8) return error.UnexpectedToken; // Dynamic length string. - const r = if (ptrInfo.sentinel) |sentinel_ptr| - try allocator.allocSentinel(ptrInfo.child, s.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*) + const r = if (ptrInfo.sentinel()) |sentinel| + try allocator.allocSentinel(ptrInfo.child, s.len, sentinel) else try allocator.alloc(ptrInfo.child, s.len); @memcpy(r[0..], s); @@ -787,8 +786,7 @@ fn sliceToEnum(comptime T: type, slice: []const u8) !T { fn fillDefaultStructValues(comptime T: type, r: *T, fields_seen: *[@typeInfo(T).@"struct".fields.len]bool) !void { inline for (@typeInfo(T).@"struct".fields, 0..) |field, i| { if (!fields_seen[i]) { - if (field.default_value) |default_ptr| { - const default = @as(*align(1) const field.type, @ptrCast(default_ptr)).*; + if (field.defaultValue()) |default| { @field(r, field.name) = default; } else { return error.MissingField; diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig index 9761196151..db2ba85318 100644 --- a/lib/std/json/stringify.zig +++ b/lib/std/json/stringify.zig @@ -642,7 +642,7 @@ pub fn WriteStream( }, }, .many, .slice => { - if (ptr_info.size == .many and ptr_info.sentinel == null) + if (ptr_info.size == .many and ptr_info.sentinel() == null) @compileError("unable to stringify type '" ++ @typeName(T) ++ "' without sentinel"); const slice = if (ptr_info.size == .many) std.mem.span(value) else value; diff --git a/lib/std/mem.zig b/lib/std/mem.zig index d9469ab738..28288620f6 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -263,8 +263,8 @@ pub fn zeroes(comptime T: type) T { .pointer => |ptr_info| { switch (ptr_info.size) { .slice => { - if (ptr_info.sentinel) |sentinel| { - if (ptr_info.child == u8 and @as(*const u8, @ptrCast(sentinel)).* == 0) { + if (ptr_info.sentinel()) |sentinel| { + if (ptr_info.child == u8 and sentinel == 0) { return ""; // A special case for the most common use-case: null-terminated strings. } @compileError("Can't set a sentinel slice to zero. This would require allocating memory."); @@ -282,11 +282,7 @@ pub fn zeroes(comptime T: type) T { } }, .array => |info| { - if (info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*; - return [_:sentinel]info.child{zeroes(info.child)} ** info.len; - } - return [_]info.child{zeroes(info.child)} ** info.len; + return @splat(zeroes(info.child)); }, .vector => |info| { return @splat(zeroes(info.child)); @@ -456,9 +452,8 @@ pub fn zeroInit(comptime T: type, init: anytype) T { @field(value, field.name) = @field(init, field.name); }, } - } else if (field.default_value) |default_value_ptr| { - const default_value = @as(*align(1) const field.type, @ptrCast(default_value_ptr)).*; - @field(value, field.name) = default_value; + } else if (field.defaultValue()) |val| { + @field(value, field.name) = val; } else { switch (@typeInfo(field.type)) { .@"struct" => { @@ -782,10 +777,10 @@ fn Span(comptime T: type) type { var new_ptr_info = ptr_info; switch (ptr_info.size) { .c => { - new_ptr_info.sentinel = &@as(ptr_info.child, 0); + new_ptr_info.sentinel_ptr = &@as(ptr_info.child, 0); new_ptr_info.is_allowzero = false; }, - .many => if (ptr_info.sentinel == null) @compileError("invalid type given to std.mem.span: " ++ @typeName(T)), + .many => if (ptr_info.sentinel() == null) @compileError("invalid type given to std.mem.span: " ++ @typeName(T)), .one, .slice => @compileError("invalid type given to std.mem.span: " ++ @typeName(T)), } new_ptr_info.size = .slice; @@ -822,8 +817,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) { const Result = Span(@TypeOf(ptr)); const l = len(ptr); const ptr_info = @typeInfo(Result).pointer; - if (ptr_info.sentinel) |s_ptr| { - const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*; + if (ptr_info.sentinel()) |s| { return ptr[0..l :s]; } else { return ptr[0..l]; @@ -853,12 +847,11 @@ fn SliceTo(comptime T: type, comptime end: std.meta.Elem(T)) type { // The return type must only be sentinel terminated if we are guaranteed // to find the value searched for, which is only the case if it matches // the sentinel of the type passed. - if (array_info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*; - if (end == sentinel) { - new_ptr_info.sentinel = &end; + if (array_info.sentinel()) |s| { + if (end == s) { + new_ptr_info.sentinel_ptr = &end; } else { - new_ptr_info.sentinel = null; + new_ptr_info.sentinel_ptr = null; } } }, @@ -868,17 +861,16 @@ fn SliceTo(comptime T: type, comptime end: std.meta.Elem(T)) type { // The return type must only be sentinel terminated if we are guaranteed // to find the value searched for, which is only the case if it matches // the sentinel of the type passed. - if (ptr_info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*; - if (end == sentinel) { - new_ptr_info.sentinel = &end; + if (ptr_info.sentinel()) |s| { + if (end == s) { + new_ptr_info.sentinel_ptr = &end; } else { - new_ptr_info.sentinel = null; + new_ptr_info.sentinel_ptr = null; } } }, .c => { - new_ptr_info.sentinel = &end; + new_ptr_info.sentinel_ptr = &end; // C pointers are always allowzero, but we don't want the return type to be. assert(new_ptr_info.is_allowzero); new_ptr_info.is_allowzero = false; @@ -906,8 +898,7 @@ pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo( const Result = SliceTo(@TypeOf(ptr), end); const length = lenSliceTo(ptr, end); const ptr_info = @typeInfo(Result).pointer; - if (ptr_info.sentinel) |s_ptr| { - const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*; + if (ptr_info.sentinel()) |s| { return ptr[0..length :s]; } else { return ptr[0..length]; @@ -959,9 +950,8 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize { .pointer => |ptr_info| switch (ptr_info.size) { .one => switch (@typeInfo(ptr_info.child)) { .array => |array_info| { - if (array_info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*; - if (sentinel == end) { + if (array_info.sentinel()) |s| { + if (s == end) { return indexOfSentinel(array_info.child, end, ptr); } } @@ -969,16 +959,15 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize { }, else => {}, }, - .many => if (ptr_info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*; - if (sentinel == end) { + .many => if (ptr_info.sentinel()) |s| { + if (s == end) { return indexOfSentinel(ptr_info.child, end, ptr); } // We're looking for something other than the sentinel, // but iterating past the sentinel would be a bug so we need // to check for both. var i: usize = 0; - while (ptr[i] != end and ptr[i] != sentinel) i += 1; + while (ptr[i] != end and ptr[i] != s) i += 1; return i; }, .c => { @@ -986,10 +975,9 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize { return indexOfSentinel(ptr_info.child, end, ptr); }, .slice => { - if (ptr_info.sentinel) |sentinel_ptr| { - const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*; - if (sentinel == end) { - return indexOfSentinel(ptr_info.child, sentinel, ptr); + if (ptr_info.sentinel()) |s| { + if (s == end) { + return indexOfSentinel(ptr_info.child, s, ptr); } } return indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len; @@ -1040,9 +1028,8 @@ pub fn len(value: anytype) usize { switch (@typeInfo(@TypeOf(value))) { .pointer => |info| switch (info.size) { .many => { - const sentinel_ptr = info.sentinel orelse + const sentinel = info.sentinel() orelse @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value))); - const sentinel = @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*; return indexOfSentinel(info.child, sentinel, value); }, .c => { @@ -3587,7 +3574,7 @@ fn ReverseIterator(comptime T: type) type { var new_ptr_info = ptr_info; new_ptr_info.size = .many; new_ptr_info.child = array_info.child; - new_ptr_info.sentinel = array_info.sentinel; + new_ptr_info.sentinel_ptr = array_info.sentinel_ptr; break :blk @Type(.{ .pointer = new_ptr_info }); }, else => {}, @@ -3608,7 +3595,7 @@ fn ReverseIterator(comptime T: type) type { var ptr = @typeInfo(Pointer).pointer; ptr.size = .one; ptr.child = Element; - ptr.sentinel = null; + ptr.sentinel_ptr = null; break :ptr ptr; } }); return struct { @@ -3979,7 +3966,7 @@ fn CopyPtrAttrs( .alignment = info.alignment, .address_space = info.address_space, .child = child, - .sentinel = null, + .sentinel_ptr = null, }, }); } @@ -4547,7 +4534,7 @@ fn AlignedSlice(comptime AttributeSource: type, comptime new_alignment: usize) t .alignment = new_alignment, .address_space = info.address_space, .child = info.child, - .sentinel = null, + .sentinel_ptr = null, }, }); } diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 5f403be162..c6b0369127 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -307,7 +307,7 @@ pub fn reallocAdvanced( pub fn free(self: Allocator, memory: anytype) void { const Slice = @typeInfo(@TypeOf(memory)).pointer; const bytes = mem.sliceAsBytes(memory); - const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0; + const bytes_len = bytes.len + if (Slice.sentinel() != null) @sizeOf(Slice.child) else 0; if (bytes_len == 0) return; const non_const_ptr = @constCast(bytes.ptr); // TODO: https://github.com/ziglang/zig/issues/4298 diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 5d3c9f34be..08c8589682 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -132,21 +132,12 @@ test Elem { /// Result is always comptime-known. pub inline fn sentinel(comptime T: type) ?Elem(T) { switch (@typeInfo(T)) { - .array => |info| { - const sentinel_ptr = info.sentinel orelse return null; - return @as(*const info.child, @ptrCast(sentinel_ptr)).*; - }, + .array => |info| return info.sentinel(), .pointer => |info| { switch (info.size) { - .many, .slice => { - const sentinel_ptr = info.sentinel orelse return null; - return @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*; - }, + .many, .slice => return info.sentinel(), .one => switch (@typeInfo(info.child)) { - .array => |array_info| { - const sentinel_ptr = array_info.sentinel orelse return null; - return @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*; - }, + .array => |array_info| return array_info.sentinel(), else => {}, }, else => {}, @@ -190,11 +181,11 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .array = .{ .len = array_info.len, .child = array_info.child, - .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)), + .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)), }, }), .is_allowzero = info.is_allowzero, - .sentinel = info.sentinel, + .sentinel_ptr = info.sentinel_ptr, }, }), else => {}, @@ -208,7 +199,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .address_space = info.address_space, .child = info.child, .is_allowzero = info.is_allowzero, - .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)), + .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)), }, }), else => {}, @@ -226,7 +217,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .address_space = ptr_info.address_space, .child = ptr_info.child, .is_allowzero = ptr_info.is_allowzero, - .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)), + .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)), }, }), }, @@ -1018,7 +1009,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type { tuple_fields[i] = .{ .name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable, .type = T, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }; diff --git a/lib/std/meta/trailer_flags.zig b/lib/std/meta/trailer_flags.zig index e00d32c789..186dd2be14 100644 --- a/lib/std/meta/trailer_flags.zig +++ b/lib/std/meta/trailer_flags.zig @@ -25,7 +25,7 @@ pub fn TrailerFlags(comptime Fields: type) type { fields[i] = Type.StructField{ .name = struct_field.name, .type = ?struct_field.type, - .default_value = &@as(?struct_field.type, null), + .default_value_ptr = &@as(?struct_field.type, null), .is_comptime = false, .alignment = @alignOf(?struct_field.type), }; diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index f928617211..f8d32a42d2 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -571,7 +571,7 @@ pub fn MultiArrayList(comptime T: type) type { for (&entry_fields, sizes.fields) |*entry_field, i| entry_field.* = .{ .name = fields[i].name ++ "_ptr", .type = *fields[i].type, - .default_value = null, + .default_value_ptr = null, .is_comptime = fields[i].is_comptime, .alignment = fields[i].alignment, }; diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig index 5d16882845..92a20ac62d 100644 --- a/lib/std/zig/c_translation.zig +++ b/lib/std/zig/c_translation.zig @@ -180,10 +180,7 @@ pub fn sizeof(target: anytype) usize { // specially handled here. if (ptr.size == .one and ptr.is_const and @typeInfo(ptr.child) == .array) { const array_info = @typeInfo(ptr.child).array; - if ((array_info.child == u8 or array_info.child == u16) and - array_info.sentinel != null and - @as(*align(1) const array_info.child, @ptrCast(array_info.sentinel.?)).* == 0) - { + if ((array_info.child == u8 or array_info.child == u16) and array_info.sentinel() == 0) { // length of the string plus one for the null terminator. return (array_info.len + 1) * @sizeOf(array_info.child); } @@ -348,7 +345,7 @@ pub fn FlexibleArrayType(comptime SelfType: type, comptime ElementType: type) ty .address_space = .generic, .child = ElementType, .is_allowzero = true, - .sentinel = null, + .sentinel_ptr = null, } }); }, else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)), |
