Skip to content

Commit 9804cc8

Browse files
committed
all: update to std.builtin.Type.{Pointer,Array,StructField} field renames
1 parent 89a9cab commit 9804cc8

26 files changed

+117
-153
lines changed

lib/std/Build/Step/Options.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
787786
fn 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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,37 +950,34 @@ 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
}
968958
return indexOfScalar(array_info.child, ptr, end) orelse array_info.len;
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 => {
985974
assert(ptr != null);
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub fn reallocAdvanced(
307307
pub 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

0 commit comments

Comments
 (0)