From 9804cc8bc6fe83b2a0cd5b61b8d2fc5d458cb221 Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 15 Jan 2025 17:34:12 +0000 Subject: [PATCH] all: update to `std.builtin.Type.{Pointer,Array,StructField}` field renames --- lib/std/Build/Step/Options.zig | 4 +- lib/std/crypto/phc_encoding.zig | 2 +- lib/std/enums.zig | 2 +- lib/std/fmt.zig | 2 +- lib/std/io.zig | 2 +- lib/std/json/static.zig | 20 +++-- lib/std/json/stringify.zig | 2 +- lib/std/mem.zig | 75 ++++++++----------- lib/std/mem/Allocator.zig | 2 +- lib/std/meta.zig | 25 ++----- lib/std/meta/trailer_flags.zig | 2 +- lib/std/multi_array_list.zig | 2 +- lib/std/zig/c_translation.zig | 7 +- src/InternPool.zig | 6 +- src/Value.zig | 5 +- src/codegen/llvm/Builder.zig | 4 +- test/behavior/tuple.zig | 6 +- test/behavior/tuple_declarations.zig | 4 +- test/behavior/type.zig | 36 ++++----- test/behavior/type_info.zig | 32 ++++---- .../invalid_pointer_with_reify_type.zig | 2 +- .../compile_errors/non_scalar_sentinel.zig | 6 +- ...d_alignment_unavailable_for_reify_type.zig | 4 +- test/cases/compile_errors/reify_struct.zig | 12 ++- ...eify_type_with_invalid_field_alignment.zig | 4 +- .../reify_type_with_undefined.zig | 2 +- 26 files changed, 117 insertions(+), 153 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)), diff --git a/src/InternPool.zig b/src/InternPool.zig index 4974c54761..36ddfda857 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -1134,7 +1134,7 @@ const Local = struct { for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{ .name = elem_field.name, .type = *[len]elem_field.type, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }; @@ -1162,9 +1162,9 @@ const Local = struct { .address_space = .generic, .child = elem_field.type, .is_allowzero = false, - .sentinel = null, + .sentinel_ptr = null, } }), - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }; diff --git a/src/Value.zig b/src/Value.zig index 004d50fc93..828ad051c8 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -4641,10 +4641,7 @@ pub fn interpret(val: Value, comptime T: type, pt: Zcu.PerThread) error{ OutOfMe @field(result, field.name) = if (struct_obj.nameIndex(ip, field_name_ip)) |field_idx| f: { const field_val = try val.fieldValue(pt, field_idx); break :f try field_val.interpret(field.type, pt); - } else if (field.default_value) |ptr| f: { - const typed_ptr: *const field.type = @ptrCast(@alignCast(ptr)); - break :f typed_ptr.*; - } else return error.TypeMismatch; + } else (field.defaultValue() orelse return error.TypeMismatch); } return result; }, diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig index f5b05dfecd..b0869f3a8f 100644 --- a/src/codegen/llvm/Builder.zig +++ b/src/codegen/llvm/Builder.zig @@ -8463,7 +8463,7 @@ pub const Metadata = enum(u32) { field.* = .{ .name = name, .type = []const u8, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }; @@ -8474,7 +8474,7 @@ pub const Metadata = enum(u32) { field.* = .{ .name = name, .type = std.fmt.Formatter(format), - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }; diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index becfcce028..816b9e6d46 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -141,14 +141,14 @@ test "array-like initializer for tuple types" { .{ .name = "0", .type = i32, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(i32), }, .{ .name = "1", .type = u8, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(u8), }, @@ -330,7 +330,7 @@ test "zero sized struct in tuple handled correctly" { .fields = &.{.{ .name = "0", .type = struct {}, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 0, }}, diff --git a/test/behavior/tuple_declarations.zig b/test/behavior/tuple_declarations.zig index 907b114aeb..f437f41dec 100644 --- a/test/behavior/tuple_declarations.zig +++ b/test/behavior/tuple_declarations.zig @@ -20,13 +20,13 @@ test "tuple declaration type info" { try expectEqualStrings(info.fields[0].name, "0"); try expect(info.fields[0].type == u32); - try expect(@as(*const u32, @ptrCast(@alignCast(info.fields[0].default_value))).* == 1); + try expect(info.fields[0].defaultValue() == 1); try expect(info.fields[0].is_comptime); try expect(info.fields[0].alignment == @alignOf(u32)); try expectEqualStrings(info.fields[1].name, "1"); try expect(info.fields[1].type == []const u8); - try expect(info.fields[1].default_value == null); + try expect(info.fields[1].defaultValue() == null); try expect(!info.fields[1].is_comptime); try expect(info.fields[1].alignment == @alignOf([]const u8)); } diff --git a/test/behavior/type.zig b/test/behavior/type.zig index b9f7889e0e..13e2873e28 100644 --- a/test/behavior/type.zig +++ b/test/behavior/type.zig @@ -118,21 +118,21 @@ test "Type.Array" { .array = .{ .len = 123, .child = u8, - .sentinel = null, + .sentinel_ptr = null, }, })); try testing.expect([2]u32 == @Type(.{ .array = .{ .len = 2, .child = u32, - .sentinel = null, + .sentinel_ptr = null, }, })); try testing.expect([2:0]u32 == @Type(.{ .array = .{ .len = 2, .child = u32, - .sentinel = &@as(u32, 0), + .sentinel_ptr = &@as(u32, 0), }, })); try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool }); @@ -148,7 +148,7 @@ test "@Type create slice with null sentinel" { .alignment = 8, .address_space = .generic, .child = *i32, - .sentinel = null, + .sentinel_ptr = null, }, }); try testing.expect(Slice == []align(8) const *i32); @@ -266,10 +266,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.auto, infoA.layout); try testing.expectEqualSlices(u8, "x", infoA.fields[0].name); try testing.expectEqual(u8, infoA.fields[0].type); - try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value); + try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value_ptr); try testing.expectEqualSlices(u8, "y", infoA.fields[1].name); try testing.expectEqual(u32, infoA.fields[1].type); - try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value); + try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value_ptr); try testing.expectEqualSlices(Type.Declaration, &.{}, infoA.decls); try testing.expectEqual(@as(bool, false), infoA.is_tuple); @@ -284,10 +284,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.@"extern", infoB.layout); try testing.expectEqualSlices(u8, "x", infoB.fields[0].name); try testing.expectEqual(u8, infoB.fields[0].type); - try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value); + try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value_ptr); try testing.expectEqualSlices(u8, "y", infoB.fields[1].name); try testing.expectEqual(u32, infoB.fields[1].type); - try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoB.fields[1].default_value.?)).*); + try testing.expectEqual(@as(u32, 5), infoB.fields[1].defaultValue().?); try testing.expectEqual(@as(usize, 0), infoB.decls.len); try testing.expectEqual(@as(bool, false), infoB.is_tuple); @@ -296,10 +296,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.@"packed", infoC.layout); try testing.expectEqualSlices(u8, "x", infoC.fields[0].name); try testing.expectEqual(u8, infoC.fields[0].type); - try testing.expectEqual(@as(u8, 3), @as(*const u8, @ptrCast(infoC.fields[0].default_value.?)).*); + try testing.expectEqual(@as(u8, 3), infoC.fields[0].defaultValue().?); try testing.expectEqualSlices(u8, "y", infoC.fields[1].name); try testing.expectEqual(u32, infoC.fields[1].type); - try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoC.fields[1].default_value.?)).*); + try testing.expectEqual(@as(u32, 5), infoC.fields[1].defaultValue().?); try testing.expectEqual(@as(usize, 0), infoC.decls.len); try testing.expectEqual(@as(bool, false), infoC.is_tuple); @@ -309,10 +309,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.auto, infoD.layout); try testing.expectEqualSlices(u8, "x", infoD.fields[0].name); try testing.expectEqual(comptime_int, infoD.fields[0].type); - try testing.expectEqual(@as(comptime_int, 3), @as(*const comptime_int, @ptrCast(infoD.fields[0].default_value.?)).*); + try testing.expectEqual(@as(comptime_int, 3), infoD.fields[0].defaultValue().?); try testing.expectEqualSlices(u8, "y", infoD.fields[1].name); try testing.expectEqual(comptime_int, infoD.fields[1].type); - try testing.expectEqual(@as(comptime_int, 5), @as(*const comptime_int, @ptrCast(infoD.fields[1].default_value.?)).*); + try testing.expectEqual(@as(comptime_int, 5), infoD.fields[1].defaultValue().?); try testing.expectEqual(@as(usize, 0), infoD.decls.len); try testing.expectEqual(@as(bool, false), infoD.is_tuple); @@ -322,10 +322,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.auto, infoE.layout); try testing.expectEqualSlices(u8, "0", infoE.fields[0].name); try testing.expectEqual(comptime_int, infoE.fields[0].type); - try testing.expectEqual(@as(comptime_int, 1), @as(*const comptime_int, @ptrCast(infoE.fields[0].default_value.?)).*); + try testing.expectEqual(@as(comptime_int, 1), infoE.fields[0].defaultValue().?); try testing.expectEqualSlices(u8, "1", infoE.fields[1].name); try testing.expectEqual(comptime_int, infoE.fields[1].type); - try testing.expectEqual(@as(comptime_int, 2), @as(*const comptime_int, @ptrCast(infoE.fields[1].default_value.?)).*); + try testing.expectEqual(@as(comptime_int, 2), infoE.fields[1].defaultValue().?); try testing.expectEqual(@as(usize, 0), infoE.decls.len); try testing.expectEqual(@as(bool, true), infoE.is_tuple); @@ -582,7 +582,7 @@ test "reified struct field name from optional payload" { .fields = &.{.{ .name = name, .type = u8, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 1, }}, @@ -628,7 +628,7 @@ test "reified struct uses @alignOf" { .{ .name = "globals", .type = modules.mach.globals, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(modules.mach.globals), }, @@ -688,7 +688,7 @@ test "empty struct assigned to reified struct field" { .fields = &.{.{ .name = "components", .type = @TypeOf(modules.components), - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(@TypeOf(modules.components)), }}, @@ -738,7 +738,7 @@ test "struct field names sliced at comptime from larger string" { .alignment = 0, .name = name ++ "", .type = usize, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, }}; } diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index abd7e69160..2b7a66c763 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -84,7 +84,7 @@ fn testPointer() !void { try expect(u32_ptr_info.pointer.is_volatile == false); try expect(u32_ptr_info.pointer.alignment == @alignOf(u32)); try expect(u32_ptr_info.pointer.child == u32); - try expect(u32_ptr_info.pointer.sentinel == null); + try expect(u32_ptr_info.pointer.sentinel() == null); } test "type info: unknown length pointer type info" { @@ -98,7 +98,7 @@ fn testUnknownLenPtr() !void { try expect(u32_ptr_info.pointer.size == .many); try expect(u32_ptr_info.pointer.is_const == true); try expect(u32_ptr_info.pointer.is_volatile == true); - try expect(u32_ptr_info.pointer.sentinel == null); + try expect(u32_ptr_info.pointer.sentinel() == null); try expect(u32_ptr_info.pointer.alignment == @alignOf(f64)); try expect(u32_ptr_info.pointer.child == f64); } @@ -114,9 +114,9 @@ fn testNullTerminatedPtr() !void { try expect(ptr_info.pointer.size == .many); try expect(ptr_info.pointer.is_const == false); try expect(ptr_info.pointer.is_volatile == false); - try expect(@as(*const u8, @ptrCast(ptr_info.pointer.sentinel.?)).* == 0); + try expect(ptr_info.pointer.sentinel().? == 0); - try expect(@typeInfo([:0]u8).pointer.sentinel != null); + try expect(@typeInfo([:0]u8).pointer.sentinel() != null); } test "type info: slice type info" { @@ -145,14 +145,14 @@ fn testArray() !void { try expect(info == .array); try expect(info.array.len == 42); try expect(info.array.child == u8); - try expect(info.array.sentinel == null); + try expect(info.array.sentinel() == null); } { const info = @typeInfo([10:0]u8); try expect(info.array.len == 10); try expect(info.array.child == u8); - try expect(@as(*const u8, @ptrCast(info.array.sentinel.?)).* == @as(u8, 0)); + try expect(info.array.sentinel().? == @as(u8, 0)); try expect(@sizeOf([10:0]u8) == info.array.len + 1); } } @@ -292,8 +292,8 @@ fn testStruct() !void { try expect(unpacked_struct_info.@"struct".is_tuple == false); try expect(unpacked_struct_info.@"struct".backing_integer == null); try expect(unpacked_struct_info.@"struct".fields[0].alignment == @alignOf(u32)); - try expect(@as(*align(1) const u32, @ptrCast(unpacked_struct_info.@"struct".fields[0].default_value.?)).* == 4); - try expect(mem.eql(u8, "foobar", @as(*align(1) const *const [6:0]u8, @ptrCast(unpacked_struct_info.@"struct".fields[1].default_value.?)).*)); + try expect(unpacked_struct_info.@"struct".fields[0].defaultValue().? == 4); + try expect(mem.eql(u8, "foobar", unpacked_struct_info.@"struct".fields[1].defaultValue().?)); } const TestStruct = struct { @@ -315,8 +315,8 @@ fn testPackedStruct() !void { try expect(struct_info.@"struct".fields.len == 4); try expect(struct_info.@"struct".fields[0].alignment == 0); try expect(struct_info.@"struct".fields[2].type == f32); - try expect(struct_info.@"struct".fields[2].default_value == null); - try expect(@as(*align(1) const u32, @ptrCast(struct_info.@"struct".fields[3].default_value.?)).* == 4); + try expect(struct_info.@"struct".fields[2].defaultValue() == null); + try expect(struct_info.@"struct".fields[3].defaultValue().? == 4); try expect(struct_info.@"struct".fields[3].alignment == 0); try expect(struct_info.@"struct".decls.len == 1); } @@ -380,7 +380,7 @@ fn testFunction() !void { try expect(foo_ptr_fn_info.pointer.address_space == .generic); try expect(foo_ptr_fn_info.pointer.child == foo_fn_type); try expect(!foo_ptr_fn_info.pointer.is_allowzero); - try expect(foo_ptr_fn_info.pointer.sentinel == null); + try expect(foo_ptr_fn_info.pointer.sentinel() == null); // Avoid looking at `typeInfoFooAligned` on targets which don't support function alignment. switch (builtin.target.cpu.arch) { @@ -408,7 +408,7 @@ fn testFunction() !void { try expect(aligned_foo_ptr_fn_info.pointer.address_space == .generic); try expect(aligned_foo_ptr_fn_info.pointer.child == aligned_foo_fn_type); try expect(!aligned_foo_ptr_fn_info.pointer.is_allowzero); - try expect(aligned_foo_ptr_fn_info.pointer.sentinel == null); + try expect(aligned_foo_ptr_fn_info.pointer.sentinel() == null); } extern fn typeInfoFoo(a: usize, b: bool, ...) callconv(.c) usize; @@ -517,7 +517,7 @@ test "type info: TypeId -> Type impl cast" { test "sentinel of opaque pointer type" { const c_void_info = @typeInfo(*anyopaque); - try expect(c_void_info.pointer.sentinel == null); + try expect(c_void_info.pointer.sentinel_ptr == null); } test "@typeInfo does not force declarations into existence" { @@ -601,9 +601,9 @@ test "typeInfo resolves usingnamespace declarations" { try expectEqualStrings(decls[1].name, "f1"); } -test "value from struct @typeInfo default_value can be loaded at comptime" { +test "value from struct @typeInfo default_value_ptr can be loaded at comptime" { comptime { - const a = @typeInfo(@TypeOf(.{ .foo = @as(u8, 1) })).@"struct".fields[0].default_value; + const a = @typeInfo(@TypeOf(.{ .foo = @as(u8, 1) })).@"struct".fields[0].default_value_ptr; try expect(@as(*const u8, @ptrCast(a)).* == 1); } } @@ -646,7 +646,7 @@ test "@typeInfo decls ignore dependency loops" { test "type info of tuple of string literal default value" { const struct_field = @typeInfo(@TypeOf(.{"hi"})).@"struct".fields[0]; - const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*; + const value = struct_field.defaultValue().?; comptime std.debug.assert(value[0] == 'h'); } diff --git a/test/cases/compile_errors/invalid_pointer_with_reify_type.zig b/test/cases/compile_errors/invalid_pointer_with_reify_type.zig index 3238633b7e..c1f622c1a5 100644 --- a/test/cases/compile_errors/invalid_pointer_with_reify_type.zig +++ b/test/cases/compile_errors/invalid_pointer_with_reify_type.zig @@ -7,7 +7,7 @@ export fn entry() void { .address_space = .generic, .child = u8, .is_allowzero = false, - .sentinel = &@as(u8, 0), + .sentinel_ptr = &@as(u8, 0), } }); } diff --git a/test/cases/compile_errors/non_scalar_sentinel.zig b/test/cases/compile_errors/non_scalar_sentinel.zig index 8eb4c4232a..c2d8bced8d 100644 --- a/test/cases/compile_errors/non_scalar_sentinel.zig +++ b/test/cases/compile_errors/non_scalar_sentinel.zig @@ -12,7 +12,7 @@ comptime { } comptime { - _ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel = &sentinel } }); + _ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel_ptr = &sentinel } }); } comptime { _ = @Type(.{ .pointer = .{ @@ -23,7 +23,7 @@ comptime { .address_space = .generic, .child = S, .is_allowzero = false, - .sentinel = &sentinel, + .sentinel_ptr = &sentinel, } }); } comptime { @@ -35,7 +35,7 @@ comptime { .address_space = .generic, .child = S, .is_allowzero = false, - .sentinel = &sentinel, + .sentinel_ptr = &sentinel, } }); } diff --git a/test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig b/test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig index 0251c018fa..20fb548d83 100644 --- a/test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig +++ b/test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig @@ -1,11 +1,9 @@ export fn entry() void { _ = @Type(.{ .@"struct" = .{ .layout = .@"packed", .fields = &.{ - .{ .name = "one", .type = u4, .default_value = null, .is_comptime = false, .alignment = 2 }, + .{ .name = "one", .type = u4, .default_value_ptr = null, .is_comptime = false, .alignment = 2 }, }, .decls = &.{}, .is_tuple = false } }); } // error -// backend=stage2 -// target=native // // :2:9: error: alignment in a packed struct field must be set to 0 diff --git a/test/cases/compile_errors/reify_struct.zig b/test/cases/compile_errors/reify_struct.zig index fcea61b46f..12d445082c 100644 --- a/test/cases/compile_errors/reify_struct.zig +++ b/test/cases/compile_errors/reify_struct.zig @@ -4,7 +4,7 @@ comptime { .fields = &.{.{ .name = "foo", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 4, }}, @@ -18,7 +18,7 @@ comptime { .fields = &.{.{ .name = "3", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = false, .alignment = 4, }}, @@ -32,7 +32,7 @@ comptime { .fields = &.{.{ .name = "0", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = true, .alignment = 4, }}, @@ -46,7 +46,7 @@ comptime { .fields = &.{.{ .name = "0", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = true, .alignment = 4, }}, @@ -60,7 +60,7 @@ comptime { .fields = &.{.{ .name = "0", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = true, .alignment = 4, }}, @@ -70,8 +70,6 @@ comptime { } // error -// backend=stage2 -// target=native // // :2:5: error: tuple cannot have non-numeric field 'foo' // :16:5: error: tuple field name '3' does not match field index 0 diff --git a/test/cases/compile_errors/reify_type_with_invalid_field_alignment.zig b/test/cases/compile_errors/reify_type_with_invalid_field_alignment.zig index 84e5cc5c27..0fcb4ba7fc 100644 --- a/test/cases/compile_errors/reify_type_with_invalid_field_alignment.zig +++ b/test/cases/compile_errors/reify_type_with_invalid_field_alignment.zig @@ -17,7 +17,7 @@ comptime { .fields = &.{.{ .name = "0", .type = u32, - .default_value = null, + .default_value_ptr = null, .is_comptime = true, .alignment = 5, }}, @@ -36,7 +36,7 @@ comptime { .address_space = .generic, .child = u8, .is_allowzero = false, - .sentinel = null, + .sentinel_ptr = null, }, }); } diff --git a/test/cases/compile_errors/reify_type_with_undefined.zig b/test/cases/compile_errors/reify_type_with_undefined.zig index 1c34a2a84e..bfdeec6a81 100644 --- a/test/cases/compile_errors/reify_type_with_undefined.zig +++ b/test/cases/compile_errors/reify_type_with_undefined.zig @@ -1,5 +1,5 @@ comptime { - _ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel = undefined } }); + _ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel_ptr = undefined } }); } comptime { _ = @Type(.{