authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-15 17:34:12+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-16 12:49:58+00:00
log9804cc8bc6fe83b2a0cd5b61b8d2fc5d458cb221
treed2f7a675b37c8f94db9b1015589a3b37805e2ac6
parent89a9cabafd745034871ea014b06bd3bad0505f4a
signaturelock-open Commit is signed but in an unrecognized format.

all: update to `std.builtin.Type.{Pointer,Array,StructField}` field renames


26 files changed, 117 insertions(+), 153 deletions(-)

lib/std/Build/Step/Options.zig+1-3
......@@ -318,9 +318,7 @@ fn printStruct(options: *Options, out: anytype, comptime T: type, comptime val:
318318 try out.print(" {p_}: {s}", .{ std.zig.fmtId(field.name), type_name });
319319 }
320320
321 if (field.default_value != null) {
322 const default_value = @as(*field.type, @ptrCast(@alignCast(@constCast(field.default_value.?)))).*;
323
321 if (field.defaultValue()) |default_value| {
324322 try out.writeAll(" = ");
325323 switch (@typeInfo(@TypeOf(default_value))) {
326324 .@"enum" => try out.print(".{s},\n", .{@tagName(default_value)}),
lib/std/crypto/phc_encoding.zig+1-1
......@@ -164,7 +164,7 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult
164164 // with default values
165165 var expected_fields: usize = 0;
166166 inline for (comptime meta.fields(HashResult)) |p| {
167 if (@typeInfo(p.type) != .optional and p.default_value == null) {
167 if (@typeInfo(p.type) != .optional and p.default_value_ptr == null) {
168168 expected_fields += 1;
169169 }
170170 }
lib/std/enums.zig+1-1
......@@ -19,7 +19,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
1919 struct_field.* = .{
2020 .name = enum_field.name ++ "",
2121 .type = Data,
22 .default_value = if (field_default) |d| @as(?*const anyopaque, @ptrCast(&d)) else null,
22 .default_value_ptr = if (field_default) |d| @as(?*const anyopaque, @ptrCast(&d)) else null,
2323 .is_comptime = false,
2424 .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
2525 };
lib/std/fmt.zig+1-1
......@@ -633,7 +633,7 @@ pub fn formatType(
633633 .many, .c => {
634634 if (actual_fmt.len == 0)
635635 @compileError("cannot format pointer without a specifier (i.e. {s} or {*})");
636 if (ptr_info.sentinel) |_| {
636 if (ptr_info.sentinel() != null) {
637637 return formatType(mem.span(value), actual_fmt, options, writer, max_depth);
638638 }
639639 if (actual_fmt[0] == 's' and ptr_info.child == u8) {
lib/std/io.zig+1-1
......@@ -805,7 +805,7 @@ pub fn PollFiles(comptime StreamEnum: type) type {
805805 struct_field.* = .{
806806 .name = enum_field.name ++ "",
807807 .type = fs.File,
808 .default_value = null,
808 .default_value_ptr = null,
809809 .is_comptime = false,
810810 .alignment = @alignOf(fs.File),
811811 };
lib/std/json/static.zig+9-11
......@@ -476,9 +476,8 @@ pub fn innerParse(
476476 arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));
477477 }
478478
479 if (ptrInfo.sentinel) |some| {
480 const sentinel_value = @as(*align(1) const ptrInfo.child, @ptrCast(some)).*;
481 return try arraylist.toOwnedSliceSentinel(sentinel_value);
479 if (ptrInfo.sentinel()) |s| {
480 return try arraylist.toOwnedSliceSentinel(s);
482481 }
483482
484483 return try arraylist.toOwnedSlice();
......@@ -487,11 +486,11 @@ pub fn innerParse(
487486 if (ptrInfo.child != u8) return error.UnexpectedToken;
488487
489488 // Dynamic length string.
490 if (ptrInfo.sentinel) |sentinel_ptr| {
489 if (ptrInfo.sentinel()) |s| {
491490 // Use our own array list so we can append the sentinel.
492491 var value_list = ArrayList(u8).init(allocator);
493492 _ = try source.allocNextIntoArrayList(&value_list, .alloc_always);
494 return try value_list.toOwnedSliceSentinel(@as(*const u8, @ptrCast(sentinel_ptr)).*);
493 return try value_list.toOwnedSliceSentinel(s);
495494 }
496495 if (ptrInfo.is_const) {
497496 switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) {
......@@ -714,8 +713,8 @@ pub fn innerParseFromValue(
714713 .slice => {
715714 switch (source) {
716715 .array => |array| {
717 const r = if (ptrInfo.sentinel) |sentinel_ptr|
718 try allocator.allocSentinel(ptrInfo.child, array.items.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
716 const r = if (ptrInfo.sentinel()) |sentinel|
717 try allocator.allocSentinel(ptrInfo.child, array.items.len, sentinel)
719718 else
720719 try allocator.alloc(ptrInfo.child, array.items.len);
721720
......@@ -729,8 +728,8 @@ pub fn innerParseFromValue(
729728 if (ptrInfo.child != u8) return error.UnexpectedToken;
730729 // Dynamic length string.
731730
732 const r = if (ptrInfo.sentinel) |sentinel_ptr|
733 try allocator.allocSentinel(ptrInfo.child, s.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
731 const r = if (ptrInfo.sentinel()) |sentinel|
732 try allocator.allocSentinel(ptrInfo.child, s.len, sentinel)
734733 else
735734 try allocator.alloc(ptrInfo.child, s.len);
736735 @memcpy(r[0..], s);
......@@ -787,8 +786,7 @@ fn sliceToEnum(comptime T: type, slice: []const u8) !T {
787786fn fillDefaultStructValues(comptime T: type, r: *T, fields_seen: *[@typeInfo(T).@"struct".fields.len]bool) !void {
788787 inline for (@typeInfo(T).@"struct".fields, 0..) |field, i| {
789788 if (!fields_seen[i]) {
790 if (field.default_value) |default_ptr| {
791 const default = @as(*align(1) const field.type, @ptrCast(default_ptr)).*;
789 if (field.defaultValue()) |default| {
792790 @field(r, field.name) = default;
793791 } else {
794792 return error.MissingField;
lib/std/json/stringify.zig+1-1
......@@ -642,7 +642,7 @@ pub fn WriteStream(
642642 },
643643 },
644644 .many, .slice => {
645 if (ptr_info.size == .many and ptr_info.sentinel == null)
645 if (ptr_info.size == .many and ptr_info.sentinel() == null)
646646 @compileError("unable to stringify type '" ++ @typeName(T) ++ "' without sentinel");
647647 const slice = if (ptr_info.size == .many) std.mem.span(value) else value;
648648
lib/std/mem.zig+31-44
......@@ -263,8 +263,8 @@ pub fn zeroes(comptime T: type) T {
263263 .pointer => |ptr_info| {
264264 switch (ptr_info.size) {
265265 .slice => {
266 if (ptr_info.sentinel) |sentinel| {
267 if (ptr_info.child == u8 and @as(*const u8, @ptrCast(sentinel)).* == 0) {
266 if (ptr_info.sentinel()) |sentinel| {
267 if (ptr_info.child == u8 and sentinel == 0) {
268268 return ""; // A special case for the most common use-case: null-terminated strings.
269269 }
270270 @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 {
282282 }
283283 },
284284 .array => |info| {
285 if (info.sentinel) |sentinel_ptr| {
286 const sentinel = @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*;
287 return [_:sentinel]info.child{zeroes(info.child)} ** info.len;
288 }
289 return [_]info.child{zeroes(info.child)} ** info.len;
285 return @splat(zeroes(info.child));
290286 },
291287 .vector => |info| {
292288 return @splat(zeroes(info.child));
......@@ -456,9 +452,8 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
456452 @field(value, field.name) = @field(init, field.name);
457453 },
458454 }
459 } else if (field.default_value) |default_value_ptr| {
460 const default_value = @as(*align(1) const field.type, @ptrCast(default_value_ptr)).*;
461 @field(value, field.name) = default_value;
455 } else if (field.defaultValue()) |val| {
456 @field(value, field.name) = val;
462457 } else {
463458 switch (@typeInfo(field.type)) {
464459 .@"struct" => {
......@@ -782,10 +777,10 @@ fn Span(comptime T: type) type {
782777 var new_ptr_info = ptr_info;
783778 switch (ptr_info.size) {
784779 .c => {
785 new_ptr_info.sentinel = &@as(ptr_info.child, 0);
780 new_ptr_info.sentinel_ptr = &@as(ptr_info.child, 0);
786781 new_ptr_info.is_allowzero = false;
787782 },
788 .many => if (ptr_info.sentinel == null) @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
783 .many => if (ptr_info.sentinel() == null) @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
789784 .one, .slice => @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
790785 }
791786 new_ptr_info.size = .slice;
......@@ -822,8 +817,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
822817 const Result = Span(@TypeOf(ptr));
823818 const l = len(ptr);
824819 const ptr_info = @typeInfo(Result).pointer;
825 if (ptr_info.sentinel) |s_ptr| {
826 const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*;
820 if (ptr_info.sentinel()) |s| {
827821 return ptr[0..l :s];
828822 } else {
829823 return ptr[0..l];
......@@ -853,12 +847,11 @@ fn SliceTo(comptime T: type, comptime end: std.meta.Elem(T)) type {
853847 // The return type must only be sentinel terminated if we are guaranteed
854848 // to find the value searched for, which is only the case if it matches
855849 // the sentinel of the type passed.
856 if (array_info.sentinel) |sentinel_ptr| {
857 const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
858 if (end == sentinel) {
859 new_ptr_info.sentinel = &end;
850 if (array_info.sentinel()) |s| {
851 if (end == s) {
852 new_ptr_info.sentinel_ptr = &end;
860853 } else {
861 new_ptr_info.sentinel = null;
854 new_ptr_info.sentinel_ptr = null;
862855 }
863856 }
864857 },
......@@ -868,17 +861,16 @@ fn SliceTo(comptime T: type, comptime end: std.meta.Elem(T)) type {
868861 // The return type must only be sentinel terminated if we are guaranteed
869862 // to find the value searched for, which is only the case if it matches
870863 // the sentinel of the type passed.
871 if (ptr_info.sentinel) |sentinel_ptr| {
872 const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
873 if (end == sentinel) {
874 new_ptr_info.sentinel = &end;
864 if (ptr_info.sentinel()) |s| {
865 if (end == s) {
866 new_ptr_info.sentinel_ptr = &end;
875867 } else {
876 new_ptr_info.sentinel = null;
868 new_ptr_info.sentinel_ptr = null;
877869 }
878870 }
879871 },
880872 .c => {
881 new_ptr_info.sentinel = &end;
873 new_ptr_info.sentinel_ptr = &end;
882874 // C pointers are always allowzero, but we don't want the return type to be.
883875 assert(new_ptr_info.is_allowzero);
884876 new_ptr_info.is_allowzero = false;
......@@ -906,8 +898,7 @@ pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(
906898 const Result = SliceTo(@TypeOf(ptr), end);
907899 const length = lenSliceTo(ptr, end);
908900 const ptr_info = @typeInfo(Result).pointer;
909 if (ptr_info.sentinel) |s_ptr| {
910 const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*;
901 if (ptr_info.sentinel()) |s| {
911902 return ptr[0..length :s];
912903 } else {
913904 return ptr[0..length];
......@@ -959,9 +950,8 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
959950 .pointer => |ptr_info| switch (ptr_info.size) {
960951 .one => switch (@typeInfo(ptr_info.child)) {
961952 .array => |array_info| {
962 if (array_info.sentinel) |sentinel_ptr| {
963 const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
964 if (sentinel == end) {
953 if (array_info.sentinel()) |s| {
954 if (s == end) {
965955 return indexOfSentinel(array_info.child, end, ptr);
966956 }
967957 }
......@@ -969,16 +959,15 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
969959 },
970960 else => {},
971961 },
972 .many => if (ptr_info.sentinel) |sentinel_ptr| {
973 const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
974 if (sentinel == end) {
962 .many => if (ptr_info.sentinel()) |s| {
963 if (s == end) {
975964 return indexOfSentinel(ptr_info.child, end, ptr);
976965 }
977966 // We're looking for something other than the sentinel,
978967 // but iterating past the sentinel would be a bug so we need
979968 // to check for both.
980969 var i: usize = 0;
981 while (ptr[i] != end and ptr[i] != sentinel) i += 1;
970 while (ptr[i] != end and ptr[i] != s) i += 1;
982971 return i;
983972 },
984973 .c => {
......@@ -986,10 +975,9 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
986975 return indexOfSentinel(ptr_info.child, end, ptr);
987976 },
988977 .slice => {
989 if (ptr_info.sentinel) |sentinel_ptr| {
990 const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
991 if (sentinel == end) {
992 return indexOfSentinel(ptr_info.child, sentinel, ptr);
978 if (ptr_info.sentinel()) |s| {
979 if (s == end) {
980 return indexOfSentinel(ptr_info.child, s, ptr);
993981 }
994982 }
995983 return indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len;
......@@ -1040,9 +1028,8 @@ pub fn len(value: anytype) usize {
10401028 switch (@typeInfo(@TypeOf(value))) {
10411029 .pointer => |info| switch (info.size) {
10421030 .many => {
1043 const sentinel_ptr = info.sentinel orelse
1031 const sentinel = info.sentinel() orelse
10441032 @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value)));
1045 const sentinel = @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*;
10461033 return indexOfSentinel(info.child, sentinel, value);
10471034 },
10481035 .c => {
......@@ -3587,7 +3574,7 @@ fn ReverseIterator(comptime T: type) type {
35873574 var new_ptr_info = ptr_info;
35883575 new_ptr_info.size = .many;
35893576 new_ptr_info.child = array_info.child;
3590 new_ptr_info.sentinel = array_info.sentinel;
3577 new_ptr_info.sentinel_ptr = array_info.sentinel_ptr;
35913578 break :blk @Type(.{ .pointer = new_ptr_info });
35923579 },
35933580 else => {},
......@@ -3608,7 +3595,7 @@ fn ReverseIterator(comptime T: type) type {
36083595 var ptr = @typeInfo(Pointer).pointer;
36093596 ptr.size = .one;
36103597 ptr.child = Element;
3611 ptr.sentinel = null;
3598 ptr.sentinel_ptr = null;
36123599 break :ptr ptr;
36133600 } });
36143601 return struct {
......@@ -3979,7 +3966,7 @@ fn CopyPtrAttrs(
39793966 .alignment = info.alignment,
39803967 .address_space = info.address_space,
39813968 .child = child,
3982 .sentinel = null,
3969 .sentinel_ptr = null,
39833970 },
39843971 });
39853972}
......@@ -4547,7 +4534,7 @@ fn AlignedSlice(comptime AttributeSource: type, comptime new_alignment: usize) t
45474534 .alignment = new_alignment,
45484535 .address_space = info.address_space,
45494536 .child = info.child,
4550 .sentinel = null,
4537 .sentinel_ptr = null,
45514538 },
45524539 });
45534540}
lib/std/mem/Allocator.zig+1-1
......@@ -307,7 +307,7 @@ pub fn reallocAdvanced(
307307pub fn free(self: Allocator, memory: anytype) void {
308308 const Slice = @typeInfo(@TypeOf(memory)).pointer;
309309 const bytes = mem.sliceAsBytes(memory);
310 const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0;
310 const bytes_len = bytes.len + if (Slice.sentinel() != null) @sizeOf(Slice.child) else 0;
311311 if (bytes_len == 0) return;
312312 const non_const_ptr = @constCast(bytes.ptr);
313313 // TODO: https://github.com/ziglang/zig/issues/4298
lib/std/meta.zig+8-17
......@@ -132,21 +132,12 @@ test Elem {
132132/// Result is always comptime-known.
133133pub inline fn sentinel(comptime T: type) ?Elem(T) {
134134 switch (@typeInfo(T)) {
135 .array => |info| {
136 const sentinel_ptr = info.sentinel orelse return null;
137 return @as(*const info.child, @ptrCast(sentinel_ptr)).*;
138 },
135 .array => |info| return info.sentinel(),
139136 .pointer => |info| {
140137 switch (info.size) {
141 .many, .slice => {
142 const sentinel_ptr = info.sentinel orelse return null;
143 return @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*;
144 },
138 .many, .slice => return info.sentinel(),
145139 .one => switch (@typeInfo(info.child)) {
146 .array => |array_info| {
147 const sentinel_ptr = array_info.sentinel orelse return null;
148 return @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
149 },
140 .array => |array_info| return array_info.sentinel(),
150141 else => {},
151142 },
152143 else => {},
......@@ -190,11 +181,11 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
190181 .array = .{
191182 .len = array_info.len,
192183 .child = array_info.child,
193 .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
184 .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
194185 },
195186 }),
196187 .is_allowzero = info.is_allowzero,
197 .sentinel = info.sentinel,
188 .sentinel_ptr = info.sentinel_ptr,
198189 },
199190 }),
200191 else => {},
......@@ -208,7 +199,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
208199 .address_space = info.address_space,
209200 .child = info.child,
210201 .is_allowzero = info.is_allowzero,
211 .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
202 .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
212203 },
213204 }),
214205 else => {},
......@@ -226,7 +217,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
226217 .address_space = ptr_info.address_space,
227218 .child = ptr_info.child,
228219 .is_allowzero = ptr_info.is_allowzero,
229 .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
220 .sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
230221 },
231222 }),
232223 },
......@@ -1018,7 +1009,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
10181009 tuple_fields[i] = .{
10191010 .name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,
10201011 .type = T,
1021 .default_value = null,
1012 .default_value_ptr = null,
10221013 .is_comptime = false,
10231014 .alignment = 0,
10241015 };
lib/std/meta/trailer_flags.zig+1-1
......@@ -25,7 +25,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
2525 fields[i] = Type.StructField{
2626 .name = struct_field.name,
2727 .type = ?struct_field.type,
28 .default_value = &@as(?struct_field.type, null),
28 .default_value_ptr = &@as(?struct_field.type, null),
2929 .is_comptime = false,
3030 .alignment = @alignOf(?struct_field.type),
3131 };
lib/std/multi_array_list.zig+1-1
......@@ -571,7 +571,7 @@ pub fn MultiArrayList(comptime T: type) type {
571571 for (&entry_fields, sizes.fields) |*entry_field, i| entry_field.* = .{
572572 .name = fields[i].name ++ "_ptr",
573573 .type = *fields[i].type,
574 .default_value = null,
574 .default_value_ptr = null,
575575 .is_comptime = fields[i].is_comptime,
576576 .alignment = fields[i].alignment,
577577 };
lib/std/zig/c_translation.zig+2-5
......@@ -180,10 +180,7 @@ pub fn sizeof(target: anytype) usize {
180180 // specially handled here.
181181 if (ptr.size == .one and ptr.is_const and @typeInfo(ptr.child) == .array) {
182182 const array_info = @typeInfo(ptr.child).array;
183 if ((array_info.child == u8 or array_info.child == u16) and
184 array_info.sentinel != null and
185 @as(*align(1) const array_info.child, @ptrCast(array_info.sentinel.?)).* == 0)
186 {
183 if ((array_info.child == u8 or array_info.child == u16) and array_info.sentinel() == 0) {
187184 // length of the string plus one for the null terminator.
188185 return (array_info.len + 1) * @sizeOf(array_info.child);
189186 }
......@@ -348,7 +345,7 @@ pub fn FlexibleArrayType(comptime SelfType: type, comptime ElementType: type) ty
348345 .address_space = .generic,
349346 .child = ElementType,
350347 .is_allowzero = true,
351 .sentinel = null,
348 .sentinel_ptr = null,
352349 } });
353350 },
354351 else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)),
src/InternPool.zig+3-3
......@@ -1134,7 +1134,7 @@ const Local = struct {
11341134 for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{
11351135 .name = elem_field.name,
11361136 .type = *[len]elem_field.type,
1137 .default_value = null,
1137 .default_value_ptr = null,
11381138 .is_comptime = false,
11391139 .alignment = 0,
11401140 };
......@@ -1162,9 +1162,9 @@ const Local = struct {
11621162 .address_space = .generic,
11631163 .child = elem_field.type,
11641164 .is_allowzero = false,
1165 .sentinel = null,
1165 .sentinel_ptr = null,
11661166 } }),
1167 .default_value = null,
1167 .default_value_ptr = null,
11681168 .is_comptime = false,
11691169 .alignment = 0,
11701170 };
src/Value.zig+1-4
......@@ -4641,10 +4641,7 @@ pub fn interpret(val: Value, comptime T: type, pt: Zcu.PerThread) error{ OutOfMe
46414641 @field(result, field.name) = if (struct_obj.nameIndex(ip, field_name_ip)) |field_idx| f: {
46424642 const field_val = try val.fieldValue(pt, field_idx);
46434643 break :f try field_val.interpret(field.type, pt);
4644 } else if (field.default_value) |ptr| f: {
4645 const typed_ptr: *const field.type = @ptrCast(@alignCast(ptr));
4646 break :f typed_ptr.*;
4647 } else return error.TypeMismatch;
4644 } else (field.defaultValue() orelse return error.TypeMismatch);
46484645 }
46494646 return result;
46504647 },
src/codegen/llvm/Builder.zig+2-2
......@@ -8463,7 +8463,7 @@ pub const Metadata = enum(u32) {
84638463 field.* = .{
84648464 .name = name,
84658465 .type = []const u8,
8466 .default_value = null,
8466 .default_value_ptr = null,
84678467 .is_comptime = false,
84688468 .alignment = 0,
84698469 };
......@@ -8474,7 +8474,7 @@ pub const Metadata = enum(u32) {
84748474 field.* = .{
84758475 .name = name,
84768476 .type = std.fmt.Formatter(format),
8477 .default_value = null,
8477 .default_value_ptr = null,
84788478 .is_comptime = false,
84798479 .alignment = 0,
84808480 };
test/behavior/tuple.zig+3-3
......@@ -141,14 +141,14 @@ test "array-like initializer for tuple types" {
141141 .{
142142 .name = "0",
143143 .type = i32,
144 .default_value = null,
144 .default_value_ptr = null,
145145 .is_comptime = false,
146146 .alignment = @alignOf(i32),
147147 },
148148 .{
149149 .name = "1",
150150 .type = u8,
151 .default_value = null,
151 .default_value_ptr = null,
152152 .is_comptime = false,
153153 .alignment = @alignOf(u8),
154154 },
......@@ -330,7 +330,7 @@ test "zero sized struct in tuple handled correctly" {
330330 .fields = &.{.{
331331 .name = "0",
332332 .type = struct {},
333 .default_value = null,
333 .default_value_ptr = null,
334334 .is_comptime = false,
335335 .alignment = 0,
336336 }},
test/behavior/tuple_declarations.zig+2-2
......@@ -20,13 +20,13 @@ test "tuple declaration type info" {
2020
2121 try expectEqualStrings(info.fields[0].name, "0");
2222 try expect(info.fields[0].type == u32);
23 try expect(@as(*const u32, @ptrCast(@alignCast(info.fields[0].default_value))).* == 1);
23 try expect(info.fields[0].defaultValue() == 1);
2424 try expect(info.fields[0].is_comptime);
2525 try expect(info.fields[0].alignment == @alignOf(u32));
2626
2727 try expectEqualStrings(info.fields[1].name, "1");
2828 try expect(info.fields[1].type == []const u8);
29 try expect(info.fields[1].default_value == null);
29 try expect(info.fields[1].defaultValue() == null);
3030 try expect(!info.fields[1].is_comptime);
3131 try expect(info.fields[1].alignment == @alignOf([]const u8));
3232 }
test/behavior/type.zig+18-18
......@@ -118,21 +118,21 @@ test "Type.Array" {
118118 .array = .{
119119 .len = 123,
120120 .child = u8,
121 .sentinel = null,
121 .sentinel_ptr = null,
122122 },
123123 }));
124124 try testing.expect([2]u32 == @Type(.{
125125 .array = .{
126126 .len = 2,
127127 .child = u32,
128 .sentinel = null,
128 .sentinel_ptr = null,
129129 },
130130 }));
131131 try testing.expect([2:0]u32 == @Type(.{
132132 .array = .{
133133 .len = 2,
134134 .child = u32,
135 .sentinel = &@as(u32, 0),
135 .sentinel_ptr = &@as(u32, 0),
136136 },
137137 }));
138138 try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
......@@ -148,7 +148,7 @@ test "@Type create slice with null sentinel" {
148148 .alignment = 8,
149149 .address_space = .generic,
150150 .child = *i32,
151 .sentinel = null,
151 .sentinel_ptr = null,
152152 },
153153 });
154154 try testing.expect(Slice == []align(8) const *i32);
......@@ -266,10 +266,10 @@ test "Type.Struct" {
266266 try testing.expectEqual(Type.ContainerLayout.auto, infoA.layout);
267267 try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
268268 try testing.expectEqual(u8, infoA.fields[0].type);
269 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value);
269 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value_ptr);
270270 try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
271271 try testing.expectEqual(u32, infoA.fields[1].type);
272 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value);
272 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value_ptr);
273273 try testing.expectEqualSlices(Type.Declaration, &.{}, infoA.decls);
274274 try testing.expectEqual(@as(bool, false), infoA.is_tuple);
275275
......@@ -284,10 +284,10 @@ test "Type.Struct" {
284284 try testing.expectEqual(Type.ContainerLayout.@"extern", infoB.layout);
285285 try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
286286 try testing.expectEqual(u8, infoB.fields[0].type);
287 try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value);
287 try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value_ptr);
288288 try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
289289 try testing.expectEqual(u32, infoB.fields[1].type);
290 try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoB.fields[1].default_value.?)).*);
290 try testing.expectEqual(@as(u32, 5), infoB.fields[1].defaultValue().?);
291291 try testing.expectEqual(@as(usize, 0), infoB.decls.len);
292292 try testing.expectEqual(@as(bool, false), infoB.is_tuple);
293293
......@@ -296,10 +296,10 @@ test "Type.Struct" {
296296 try testing.expectEqual(Type.ContainerLayout.@"packed", infoC.layout);
297297 try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
298298 try testing.expectEqual(u8, infoC.fields[0].type);
299 try testing.expectEqual(@as(u8, 3), @as(*const u8, @ptrCast(infoC.fields[0].default_value.?)).*);
299 try testing.expectEqual(@as(u8, 3), infoC.fields[0].defaultValue().?);
300300 try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
301301 try testing.expectEqual(u32, infoC.fields[1].type);
302 try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoC.fields[1].default_value.?)).*);
302 try testing.expectEqual(@as(u32, 5), infoC.fields[1].defaultValue().?);
303303 try testing.expectEqual(@as(usize, 0), infoC.decls.len);
304304 try testing.expectEqual(@as(bool, false), infoC.is_tuple);
305305
......@@ -309,10 +309,10 @@ test "Type.Struct" {
309309 try testing.expectEqual(Type.ContainerLayout.auto, infoD.layout);
310310 try testing.expectEqualSlices(u8, "x", infoD.fields[0].name);
311311 try testing.expectEqual(comptime_int, infoD.fields[0].type);
312 try testing.expectEqual(@as(comptime_int, 3), @as(*const comptime_int, @ptrCast(infoD.fields[0].default_value.?)).*);
312 try testing.expectEqual(@as(comptime_int, 3), infoD.fields[0].defaultValue().?);
313313 try testing.expectEqualSlices(u8, "y", infoD.fields[1].name);
314314 try testing.expectEqual(comptime_int, infoD.fields[1].type);
315 try testing.expectEqual(@as(comptime_int, 5), @as(*const comptime_int, @ptrCast(infoD.fields[1].default_value.?)).*);
315 try testing.expectEqual(@as(comptime_int, 5), infoD.fields[1].defaultValue().?);
316316 try testing.expectEqual(@as(usize, 0), infoD.decls.len);
317317 try testing.expectEqual(@as(bool, false), infoD.is_tuple);
318318
......@@ -322,10 +322,10 @@ test "Type.Struct" {
322322 try testing.expectEqual(Type.ContainerLayout.auto, infoE.layout);
323323 try testing.expectEqualSlices(u8, "0", infoE.fields[0].name);
324324 try testing.expectEqual(comptime_int, infoE.fields[0].type);
325 try testing.expectEqual(@as(comptime_int, 1), @as(*const comptime_int, @ptrCast(infoE.fields[0].default_value.?)).*);
325 try testing.expectEqual(@as(comptime_int, 1), infoE.fields[0].defaultValue().?);
326326 try testing.expectEqualSlices(u8, "1", infoE.fields[1].name);
327327 try testing.expectEqual(comptime_int, infoE.fields[1].type);
328 try testing.expectEqual(@as(comptime_int, 2), @as(*const comptime_int, @ptrCast(infoE.fields[1].default_value.?)).*);
328 try testing.expectEqual(@as(comptime_int, 2), infoE.fields[1].defaultValue().?);
329329 try testing.expectEqual(@as(usize, 0), infoE.decls.len);
330330 try testing.expectEqual(@as(bool, true), infoE.is_tuple);
331331
......@@ -582,7 +582,7 @@ test "reified struct field name from optional payload" {
582582 .fields = &.{.{
583583 .name = name,
584584 .type = u8,
585 .default_value = null,
585 .default_value_ptr = null,
586586 .is_comptime = false,
587587 .alignment = 1,
588588 }},
......@@ -628,7 +628,7 @@ test "reified struct uses @alignOf" {
628628 .{
629629 .name = "globals",
630630 .type = modules.mach.globals,
631 .default_value = null,
631 .default_value_ptr = null,
632632 .is_comptime = false,
633633 .alignment = @alignOf(modules.mach.globals),
634634 },
......@@ -688,7 +688,7 @@ test "empty struct assigned to reified struct field" {
688688 .fields = &.{.{
689689 .name = "components",
690690 .type = @TypeOf(modules.components),
691 .default_value = null,
691 .default_value_ptr = null,
692692 .is_comptime = false,
693693 .alignment = @alignOf(@TypeOf(modules.components)),
694694 }},
......@@ -738,7 +738,7 @@ test "struct field names sliced at comptime from larger string" {
738738 .alignment = 0,
739739 .name = name ++ "",
740740 .type = usize,
741 .default_value = null,
741 .default_value_ptr = null,
742742 .is_comptime = false,
743743 }};
744744 }
test/behavior/type_info.zig+16-16
......@@ -84,7 +84,7 @@ fn testPointer() !void {
8484 try expect(u32_ptr_info.pointer.is_volatile == false);
8585 try expect(u32_ptr_info.pointer.alignment == @alignOf(u32));
8686 try expect(u32_ptr_info.pointer.child == u32);
87 try expect(u32_ptr_info.pointer.sentinel == null);
87 try expect(u32_ptr_info.pointer.sentinel() == null);
8888}
8989
9090test "type info: unknown length pointer type info" {
......@@ -98,7 +98,7 @@ fn testUnknownLenPtr() !void {
9898 try expect(u32_ptr_info.pointer.size == .many);
9999 try expect(u32_ptr_info.pointer.is_const == true);
100100 try expect(u32_ptr_info.pointer.is_volatile == true);
101 try expect(u32_ptr_info.pointer.sentinel == null);
101 try expect(u32_ptr_info.pointer.sentinel() == null);
102102 try expect(u32_ptr_info.pointer.alignment == @alignOf(f64));
103103 try expect(u32_ptr_info.pointer.child == f64);
104104}
......@@ -114,9 +114,9 @@ fn testNullTerminatedPtr() !void {
114114 try expect(ptr_info.pointer.size == .many);
115115 try expect(ptr_info.pointer.is_const == false);
116116 try expect(ptr_info.pointer.is_volatile == false);
117 try expect(@as(*const u8, @ptrCast(ptr_info.pointer.sentinel.?)).* == 0);
117 try expect(ptr_info.pointer.sentinel().? == 0);
118118
119 try expect(@typeInfo([:0]u8).pointer.sentinel != null);
119 try expect(@typeInfo([:0]u8).pointer.sentinel() != null);
120120}
121121
122122test "type info: slice type info" {
......@@ -145,14 +145,14 @@ fn testArray() !void {
145145 try expect(info == .array);
146146 try expect(info.array.len == 42);
147147 try expect(info.array.child == u8);
148 try expect(info.array.sentinel == null);
148 try expect(info.array.sentinel() == null);
149149 }
150150
151151 {
152152 const info = @typeInfo([10:0]u8);
153153 try expect(info.array.len == 10);
154154 try expect(info.array.child == u8);
155 try expect(@as(*const u8, @ptrCast(info.array.sentinel.?)).* == @as(u8, 0));
155 try expect(info.array.sentinel().? == @as(u8, 0));
156156 try expect(@sizeOf([10:0]u8) == info.array.len + 1);
157157 }
158158}
......@@ -292,8 +292,8 @@ fn testStruct() !void {
292292 try expect(unpacked_struct_info.@"struct".is_tuple == false);
293293 try expect(unpacked_struct_info.@"struct".backing_integer == null);
294294 try expect(unpacked_struct_info.@"struct".fields[0].alignment == @alignOf(u32));
295 try expect(@as(*align(1) const u32, @ptrCast(unpacked_struct_info.@"struct".fields[0].default_value.?)).* == 4);
296 try expect(mem.eql(u8, "foobar", @as(*align(1) const *const [6:0]u8, @ptrCast(unpacked_struct_info.@"struct".fields[1].default_value.?)).*));
295 try expect(unpacked_struct_info.@"struct".fields[0].defaultValue().? == 4);
296 try expect(mem.eql(u8, "foobar", unpacked_struct_info.@"struct".fields[1].defaultValue().?));
297297}
298298
299299const TestStruct = struct {
......@@ -315,8 +315,8 @@ fn testPackedStruct() !void {
315315 try expect(struct_info.@"struct".fields.len == 4);
316316 try expect(struct_info.@"struct".fields[0].alignment == 0);
317317 try expect(struct_info.@"struct".fields[2].type == f32);
318 try expect(struct_info.@"struct".fields[2].default_value == null);
319 try expect(@as(*align(1) const u32, @ptrCast(struct_info.@"struct".fields[3].default_value.?)).* == 4);
318 try expect(struct_info.@"struct".fields[2].defaultValue() == null);
319 try expect(struct_info.@"struct".fields[3].defaultValue().? == 4);
320320 try expect(struct_info.@"struct".fields[3].alignment == 0);
321321 try expect(struct_info.@"struct".decls.len == 1);
322322}
......@@ -380,7 +380,7 @@ fn testFunction() !void {
380380 try expect(foo_ptr_fn_info.pointer.address_space == .generic);
381381 try expect(foo_ptr_fn_info.pointer.child == foo_fn_type);
382382 try expect(!foo_ptr_fn_info.pointer.is_allowzero);
383 try expect(foo_ptr_fn_info.pointer.sentinel == null);
383 try expect(foo_ptr_fn_info.pointer.sentinel() == null);
384384
385385 // Avoid looking at `typeInfoFooAligned` on targets which don't support function alignment.
386386 switch (builtin.target.cpu.arch) {
......@@ -408,7 +408,7 @@ fn testFunction() !void {
408408 try expect(aligned_foo_ptr_fn_info.pointer.address_space == .generic);
409409 try expect(aligned_foo_ptr_fn_info.pointer.child == aligned_foo_fn_type);
410410 try expect(!aligned_foo_ptr_fn_info.pointer.is_allowzero);
411 try expect(aligned_foo_ptr_fn_info.pointer.sentinel == null);
411 try expect(aligned_foo_ptr_fn_info.pointer.sentinel() == null);
412412}
413413
414414extern fn typeInfoFoo(a: usize, b: bool, ...) callconv(.c) usize;
......@@ -517,7 +517,7 @@ test "type info: TypeId -> Type impl cast" {
517517
518518test "sentinel of opaque pointer type" {
519519 const c_void_info = @typeInfo(*anyopaque);
520 try expect(c_void_info.pointer.sentinel == null);
520 try expect(c_void_info.pointer.sentinel_ptr == null);
521521}
522522
523523test "@typeInfo does not force declarations into existence" {
......@@ -601,9 +601,9 @@ test "typeInfo resolves usingnamespace declarations" {
601601 try expectEqualStrings(decls[1].name, "f1");
602602}
603603
604test "value from struct @typeInfo default_value can be loaded at comptime" {
604test "value from struct @typeInfo default_value_ptr can be loaded at comptime" {
605605 comptime {
606 const a = @typeInfo(@TypeOf(.{ .foo = @as(u8, 1) })).@"struct".fields[0].default_value;
606 const a = @typeInfo(@TypeOf(.{ .foo = @as(u8, 1) })).@"struct".fields[0].default_value_ptr;
607607 try expect(@as(*const u8, @ptrCast(a)).* == 1);
608608 }
609609}
......@@ -646,7 +646,7 @@ test "@typeInfo decls ignore dependency loops" {
646646
647647test "type info of tuple of string literal default value" {
648648 const struct_field = @typeInfo(@TypeOf(.{"hi"})).@"struct".fields[0];
649 const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*;
649 const value = struct_field.defaultValue().?;
650650 comptime std.debug.assert(value[0] == 'h');
651651}
652652
test/cases/compile_errors/invalid_pointer_with_reify_type.zig+1-1
......@@ -7,7 +7,7 @@ export fn entry() void {
77 .address_space = .generic,
88 .child = u8,
99 .is_allowzero = false,
10 .sentinel = &@as(u8, 0),
10 .sentinel_ptr = &@as(u8, 0),
1111 } });
1212}
1313
test/cases/compile_errors/non_scalar_sentinel.zig+3-3
......@@ -12,7 +12,7 @@ comptime {
1212}
1313
1414comptime {
15 _ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel = &sentinel } });
15 _ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel_ptr = &sentinel } });
1616}
1717comptime {
1818 _ = @Type(.{ .pointer = .{
......@@ -23,7 +23,7 @@ comptime {
2323 .address_space = .generic,
2424 .child = S,
2525 .is_allowzero = false,
26 .sentinel = &sentinel,
26 .sentinel_ptr = &sentinel,
2727 } });
2828}
2929comptime {
......@@ -35,7 +35,7 @@ comptime {
3535 .address_space = .generic,
3636 .child = S,
3737 .is_allowzero = false,
38 .sentinel = &sentinel,
38 .sentinel_ptr = &sentinel,
3939 } });
4040}
4141
test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig+1-3
......@@ -1,11 +1,9 @@
11export fn entry() void {
22 _ = @Type(.{ .@"struct" = .{ .layout = .@"packed", .fields = &.{
3 .{ .name = "one", .type = u4, .default_value = null, .is_comptime = false, .alignment = 2 },
3 .{ .name = "one", .type = u4, .default_value_ptr = null, .is_comptime = false, .alignment = 2 },
44 }, .decls = &.{}, .is_tuple = false } });
55}
66
77// error
8// backend=stage2
9// target=native
108//
119// :2:9: error: alignment in a packed struct field must be set to 0
test/cases/compile_errors/reify_struct.zig+5-7
......@@ -4,7 +4,7 @@ comptime {
44 .fields = &.{.{
55 .name = "foo",
66 .type = u32,
7 .default_value = null,
7 .default_value_ptr = null,
88 .is_comptime = false,
99 .alignment = 4,
1010 }},
......@@ -18,7 +18,7 @@ comptime {
1818 .fields = &.{.{
1919 .name = "3",
2020 .type = u32,
21 .default_value = null,
21 .default_value_ptr = null,
2222 .is_comptime = false,
2323 .alignment = 4,
2424 }},
......@@ -32,7 +32,7 @@ comptime {
3232 .fields = &.{.{
3333 .name = "0",
3434 .type = u32,
35 .default_value = null,
35 .default_value_ptr = null,
3636 .is_comptime = true,
3737 .alignment = 4,
3838 }},
......@@ -46,7 +46,7 @@ comptime {
4646 .fields = &.{.{
4747 .name = "0",
4848 .type = u32,
49 .default_value = null,
49 .default_value_ptr = null,
5050 .is_comptime = true,
5151 .alignment = 4,
5252 }},
......@@ -60,7 +60,7 @@ comptime {
6060 .fields = &.{.{
6161 .name = "0",
6262 .type = u32,
63 .default_value = null,
63 .default_value_ptr = null,
6464 .is_comptime = true,
6565 .alignment = 4,
6666 }},
......@@ -70,8 +70,6 @@ comptime {
7070}
7171
7272// error
73// backend=stage2
74// target=native
7573//
7674// :2:5: error: tuple cannot have non-numeric field 'foo'
7775// :16:5: error: tuple field name '3' does not match field index 0
test/cases/compile_errors/reify_type_with_invalid_field_alignment.zig+2-2
......@@ -17,7 +17,7 @@ comptime {
1717 .fields = &.{.{
1818 .name = "0",
1919 .type = u32,
20 .default_value = null,
20 .default_value_ptr = null,
2121 .is_comptime = true,
2222 .alignment = 5,
2323 }},
......@@ -36,7 +36,7 @@ comptime {
3636 .address_space = .generic,
3737 .child = u8,
3838 .is_allowzero = false,
39 .sentinel = null,
39 .sentinel_ptr = null,
4040 },
4141 });
4242}
test/cases/compile_errors/reify_type_with_undefined.zig+1-1
......@@ -1,5 +1,5 @@
11comptime {
2 _ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel = undefined } });
2 _ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel_ptr = undefined } });
33}
44comptime {
55 _ = @Type(.{