mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
all: update to std.builtin.Type.{Pointer,Array,StructField} field renames
This commit is contained in:
parent
89a9cabafd
commit
9804cc8bc6
@ -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 });
|
try out.print(" {p_}: {s}", .{ std.zig.fmtId(field.name), type_name });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.default_value != null) {
|
if (field.defaultValue()) |default_value| {
|
||||||
const default_value = @as(*field.type, @ptrCast(@alignCast(@constCast(field.default_value.?)))).*;
|
|
||||||
|
|
||||||
try out.writeAll(" = ");
|
try out.writeAll(" = ");
|
||||||
switch (@typeInfo(@TypeOf(default_value))) {
|
switch (@typeInfo(@TypeOf(default_value))) {
|
||||||
.@"enum" => try out.print(".{s},\n", .{@tagName(default_value)}),
|
.@"enum" => try out.print(".{s},\n", .{@tagName(default_value)}),
|
||||||
|
|||||||
@ -164,7 +164,7 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult
|
|||||||
// with default values
|
// with default values
|
||||||
var expected_fields: usize = 0;
|
var expected_fields: usize = 0;
|
||||||
inline for (comptime meta.fields(HashResult)) |p| {
|
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;
|
expected_fields += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
|
|||||||
struct_field.* = .{
|
struct_field.* = .{
|
||||||
.name = enum_field.name ++ "",
|
.name = enum_field.name ++ "",
|
||||||
.type = Data,
|
.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,
|
.is_comptime = false,
|
||||||
.alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
|
.alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -633,7 +633,7 @@ pub fn formatType(
|
|||||||
.many, .c => {
|
.many, .c => {
|
||||||
if (actual_fmt.len == 0)
|
if (actual_fmt.len == 0)
|
||||||
@compileError("cannot format pointer without a specifier (i.e. {s} or {*})");
|
@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);
|
return formatType(mem.span(value), actual_fmt, options, writer, max_depth);
|
||||||
}
|
}
|
||||||
if (actual_fmt[0] == 's' and ptr_info.child == u8) {
|
if (actual_fmt[0] == 's' and ptr_info.child == u8) {
|
||||||
|
|||||||
@ -805,7 +805,7 @@ pub fn PollFiles(comptime StreamEnum: type) type {
|
|||||||
struct_field.* = .{
|
struct_field.* = .{
|
||||||
.name = enum_field.name ++ "",
|
.name = enum_field.name ++ "",
|
||||||
.type = fs.File,
|
.type = fs.File,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(fs.File),
|
.alignment = @alignOf(fs.File),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -476,9 +476,8 @@ pub fn innerParse(
|
|||||||
arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));
|
arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ptrInfo.sentinel) |some| {
|
if (ptrInfo.sentinel()) |s| {
|
||||||
const sentinel_value = @as(*align(1) const ptrInfo.child, @ptrCast(some)).*;
|
return try arraylist.toOwnedSliceSentinel(s);
|
||||||
return try arraylist.toOwnedSliceSentinel(sentinel_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return try arraylist.toOwnedSlice();
|
return try arraylist.toOwnedSlice();
|
||||||
@ -487,11 +486,11 @@ pub fn innerParse(
|
|||||||
if (ptrInfo.child != u8) return error.UnexpectedToken;
|
if (ptrInfo.child != u8) return error.UnexpectedToken;
|
||||||
|
|
||||||
// Dynamic length string.
|
// Dynamic length string.
|
||||||
if (ptrInfo.sentinel) |sentinel_ptr| {
|
if (ptrInfo.sentinel()) |s| {
|
||||||
// Use our own array list so we can append the sentinel.
|
// Use our own array list so we can append the sentinel.
|
||||||
var value_list = ArrayList(u8).init(allocator);
|
var value_list = ArrayList(u8).init(allocator);
|
||||||
_ = try source.allocNextIntoArrayList(&value_list, .alloc_always);
|
_ = 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) {
|
if (ptrInfo.is_const) {
|
||||||
switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) {
|
switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) {
|
||||||
@ -714,8 +713,8 @@ pub fn innerParseFromValue(
|
|||||||
.slice => {
|
.slice => {
|
||||||
switch (source) {
|
switch (source) {
|
||||||
.array => |array| {
|
.array => |array| {
|
||||||
const r = if (ptrInfo.sentinel) |sentinel_ptr|
|
const r = if (ptrInfo.sentinel()) |sentinel|
|
||||||
try allocator.allocSentinel(ptrInfo.child, array.items.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
|
try allocator.allocSentinel(ptrInfo.child, array.items.len, sentinel)
|
||||||
else
|
else
|
||||||
try allocator.alloc(ptrInfo.child, array.items.len);
|
try allocator.alloc(ptrInfo.child, array.items.len);
|
||||||
|
|
||||||
@ -729,8 +728,8 @@ pub fn innerParseFromValue(
|
|||||||
if (ptrInfo.child != u8) return error.UnexpectedToken;
|
if (ptrInfo.child != u8) return error.UnexpectedToken;
|
||||||
// Dynamic length string.
|
// Dynamic length string.
|
||||||
|
|
||||||
const r = if (ptrInfo.sentinel) |sentinel_ptr|
|
const r = if (ptrInfo.sentinel()) |sentinel|
|
||||||
try allocator.allocSentinel(ptrInfo.child, s.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
|
try allocator.allocSentinel(ptrInfo.child, s.len, sentinel)
|
||||||
else
|
else
|
||||||
try allocator.alloc(ptrInfo.child, s.len);
|
try allocator.alloc(ptrInfo.child, s.len);
|
||||||
@memcpy(r[0..], s);
|
@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 {
|
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| {
|
inline for (@typeInfo(T).@"struct".fields, 0..) |field, i| {
|
||||||
if (!fields_seen[i]) {
|
if (!fields_seen[i]) {
|
||||||
if (field.default_value) |default_ptr| {
|
if (field.defaultValue()) |default| {
|
||||||
const default = @as(*align(1) const field.type, @ptrCast(default_ptr)).*;
|
|
||||||
@field(r, field.name) = default;
|
@field(r, field.name) = default;
|
||||||
} else {
|
} else {
|
||||||
return error.MissingField;
|
return error.MissingField;
|
||||||
|
|||||||
@ -642,7 +642,7 @@ pub fn WriteStream(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
.many, .slice => {
|
.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");
|
@compileError("unable to stringify type '" ++ @typeName(T) ++ "' without sentinel");
|
||||||
const slice = if (ptr_info.size == .many) std.mem.span(value) else value;
|
const slice = if (ptr_info.size == .many) std.mem.span(value) else value;
|
||||||
|
|
||||||
|
|||||||
@ -263,8 +263,8 @@ pub fn zeroes(comptime T: type) T {
|
|||||||
.pointer => |ptr_info| {
|
.pointer => |ptr_info| {
|
||||||
switch (ptr_info.size) {
|
switch (ptr_info.size) {
|
||||||
.slice => {
|
.slice => {
|
||||||
if (ptr_info.sentinel) |sentinel| {
|
if (ptr_info.sentinel()) |sentinel| {
|
||||||
if (ptr_info.child == u8 and @as(*const u8, @ptrCast(sentinel)).* == 0) {
|
if (ptr_info.child == u8 and sentinel == 0) {
|
||||||
return ""; // A special case for the most common use-case: null-terminated strings.
|
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.");
|
@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| {
|
.array => |info| {
|
||||||
if (info.sentinel) |sentinel_ptr| {
|
return @splat(zeroes(info.child));
|
||||||
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;
|
|
||||||
},
|
},
|
||||||
.vector => |info| {
|
.vector => |info| {
|
||||||
return @splat(zeroes(info.child));
|
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);
|
@field(value, field.name) = @field(init, field.name);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else if (field.default_value) |default_value_ptr| {
|
} else if (field.defaultValue()) |val| {
|
||||||
const default_value = @as(*align(1) const field.type, @ptrCast(default_value_ptr)).*;
|
@field(value, field.name) = val;
|
||||||
@field(value, field.name) = default_value;
|
|
||||||
} else {
|
} else {
|
||||||
switch (@typeInfo(field.type)) {
|
switch (@typeInfo(field.type)) {
|
||||||
.@"struct" => {
|
.@"struct" => {
|
||||||
@ -782,10 +777,10 @@ fn Span(comptime T: type) type {
|
|||||||
var new_ptr_info = ptr_info;
|
var new_ptr_info = ptr_info;
|
||||||
switch (ptr_info.size) {
|
switch (ptr_info.size) {
|
||||||
.c => {
|
.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;
|
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)),
|
.one, .slice => @compileError("invalid type given to std.mem.span: " ++ @typeName(T)),
|
||||||
}
|
}
|
||||||
new_ptr_info.size = .slice;
|
new_ptr_info.size = .slice;
|
||||||
@ -822,8 +817,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
|
|||||||
const Result = Span(@TypeOf(ptr));
|
const Result = Span(@TypeOf(ptr));
|
||||||
const l = len(ptr);
|
const l = len(ptr);
|
||||||
const ptr_info = @typeInfo(Result).pointer;
|
const ptr_info = @typeInfo(Result).pointer;
|
||||||
if (ptr_info.sentinel) |s_ptr| {
|
if (ptr_info.sentinel()) |s| {
|
||||||
const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*;
|
|
||||||
return ptr[0..l :s];
|
return ptr[0..l :s];
|
||||||
} else {
|
} else {
|
||||||
return ptr[0..l];
|
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
|
// 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
|
// to find the value searched for, which is only the case if it matches
|
||||||
// the sentinel of the type passed.
|
// the sentinel of the type passed.
|
||||||
if (array_info.sentinel) |sentinel_ptr| {
|
if (array_info.sentinel()) |s| {
|
||||||
const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
|
if (end == s) {
|
||||||
if (end == sentinel) {
|
new_ptr_info.sentinel_ptr = &end;
|
||||||
new_ptr_info.sentinel = &end;
|
|
||||||
} else {
|
} 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
|
// 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
|
// to find the value searched for, which is only the case if it matches
|
||||||
// the sentinel of the type passed.
|
// the sentinel of the type passed.
|
||||||
if (ptr_info.sentinel) |sentinel_ptr| {
|
if (ptr_info.sentinel()) |s| {
|
||||||
const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
|
if (end == s) {
|
||||||
if (end == sentinel) {
|
new_ptr_info.sentinel_ptr = &end;
|
||||||
new_ptr_info.sentinel = &end;
|
|
||||||
} else {
|
} else {
|
||||||
new_ptr_info.sentinel = null;
|
new_ptr_info.sentinel_ptr = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.c => {
|
.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.
|
// C pointers are always allowzero, but we don't want the return type to be.
|
||||||
assert(new_ptr_info.is_allowzero);
|
assert(new_ptr_info.is_allowzero);
|
||||||
new_ptr_info.is_allowzero = false;
|
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 Result = SliceTo(@TypeOf(ptr), end);
|
||||||
const length = lenSliceTo(ptr, end);
|
const length = lenSliceTo(ptr, end);
|
||||||
const ptr_info = @typeInfo(Result).pointer;
|
const ptr_info = @typeInfo(Result).pointer;
|
||||||
if (ptr_info.sentinel) |s_ptr| {
|
if (ptr_info.sentinel()) |s| {
|
||||||
const s = @as(*align(1) const ptr_info.child, @ptrCast(s_ptr)).*;
|
|
||||||
return ptr[0..length :s];
|
return ptr[0..length :s];
|
||||||
} else {
|
} else {
|
||||||
return ptr[0..length];
|
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) {
|
.pointer => |ptr_info| switch (ptr_info.size) {
|
||||||
.one => switch (@typeInfo(ptr_info.child)) {
|
.one => switch (@typeInfo(ptr_info.child)) {
|
||||||
.array => |array_info| {
|
.array => |array_info| {
|
||||||
if (array_info.sentinel) |sentinel_ptr| {
|
if (array_info.sentinel()) |s| {
|
||||||
const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
|
if (s == end) {
|
||||||
if (sentinel == end) {
|
|
||||||
return indexOfSentinel(array_info.child, end, ptr);
|
return indexOfSentinel(array_info.child, end, ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -969,16 +959,15 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
|
|||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
},
|
},
|
||||||
.many => if (ptr_info.sentinel) |sentinel_ptr| {
|
.many => if (ptr_info.sentinel()) |s| {
|
||||||
const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
|
if (s == end) {
|
||||||
if (sentinel == end) {
|
|
||||||
return indexOfSentinel(ptr_info.child, end, ptr);
|
return indexOfSentinel(ptr_info.child, end, ptr);
|
||||||
}
|
}
|
||||||
// We're looking for something other than the sentinel,
|
// We're looking for something other than the sentinel,
|
||||||
// but iterating past the sentinel would be a bug so we need
|
// but iterating past the sentinel would be a bug so we need
|
||||||
// to check for both.
|
// to check for both.
|
||||||
var i: usize = 0;
|
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;
|
return i;
|
||||||
},
|
},
|
||||||
.c => {
|
.c => {
|
||||||
@ -986,10 +975,9 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
|
|||||||
return indexOfSentinel(ptr_info.child, end, ptr);
|
return indexOfSentinel(ptr_info.child, end, ptr);
|
||||||
},
|
},
|
||||||
.slice => {
|
.slice => {
|
||||||
if (ptr_info.sentinel) |sentinel_ptr| {
|
if (ptr_info.sentinel()) |s| {
|
||||||
const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
|
if (s == end) {
|
||||||
if (sentinel == end) {
|
return indexOfSentinel(ptr_info.child, s, ptr);
|
||||||
return indexOfSentinel(ptr_info.child, sentinel, ptr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len;
|
return indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len;
|
||||||
@ -1040,9 +1028,8 @@ pub fn len(value: anytype) usize {
|
|||||||
switch (@typeInfo(@TypeOf(value))) {
|
switch (@typeInfo(@TypeOf(value))) {
|
||||||
.pointer => |info| switch (info.size) {
|
.pointer => |info| switch (info.size) {
|
||||||
.many => {
|
.many => {
|
||||||
const sentinel_ptr = info.sentinel orelse
|
const sentinel = info.sentinel() orelse
|
||||||
@compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value)));
|
@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);
|
return indexOfSentinel(info.child, sentinel, value);
|
||||||
},
|
},
|
||||||
.c => {
|
.c => {
|
||||||
@ -3587,7 +3574,7 @@ fn ReverseIterator(comptime T: type) type {
|
|||||||
var new_ptr_info = ptr_info;
|
var new_ptr_info = ptr_info;
|
||||||
new_ptr_info.size = .many;
|
new_ptr_info.size = .many;
|
||||||
new_ptr_info.child = array_info.child;
|
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 });
|
break :blk @Type(.{ .pointer = new_ptr_info });
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
@ -3608,7 +3595,7 @@ fn ReverseIterator(comptime T: type) type {
|
|||||||
var ptr = @typeInfo(Pointer).pointer;
|
var ptr = @typeInfo(Pointer).pointer;
|
||||||
ptr.size = .one;
|
ptr.size = .one;
|
||||||
ptr.child = Element;
|
ptr.child = Element;
|
||||||
ptr.sentinel = null;
|
ptr.sentinel_ptr = null;
|
||||||
break :ptr ptr;
|
break :ptr ptr;
|
||||||
} });
|
} });
|
||||||
return struct {
|
return struct {
|
||||||
@ -3979,7 +3966,7 @@ fn CopyPtrAttrs(
|
|||||||
.alignment = info.alignment,
|
.alignment = info.alignment,
|
||||||
.address_space = info.address_space,
|
.address_space = info.address_space,
|
||||||
.child = child,
|
.child = child,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -4547,7 +4534,7 @@ fn AlignedSlice(comptime AttributeSource: type, comptime new_alignment: usize) t
|
|||||||
.alignment = new_alignment,
|
.alignment = new_alignment,
|
||||||
.address_space = info.address_space,
|
.address_space = info.address_space,
|
||||||
.child = info.child,
|
.child = info.child,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -307,7 +307,7 @@ pub fn reallocAdvanced(
|
|||||||
pub fn free(self: Allocator, memory: anytype) void {
|
pub fn free(self: Allocator, memory: anytype) void {
|
||||||
const Slice = @typeInfo(@TypeOf(memory)).pointer;
|
const Slice = @typeInfo(@TypeOf(memory)).pointer;
|
||||||
const bytes = mem.sliceAsBytes(memory);
|
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;
|
if (bytes_len == 0) return;
|
||||||
const non_const_ptr = @constCast(bytes.ptr);
|
const non_const_ptr = @constCast(bytes.ptr);
|
||||||
// TODO: https://github.com/ziglang/zig/issues/4298
|
// TODO: https://github.com/ziglang/zig/issues/4298
|
||||||
|
|||||||
@ -132,21 +132,12 @@ test Elem {
|
|||||||
/// Result is always comptime-known.
|
/// Result is always comptime-known.
|
||||||
pub inline fn sentinel(comptime T: type) ?Elem(T) {
|
pub inline fn sentinel(comptime T: type) ?Elem(T) {
|
||||||
switch (@typeInfo(T)) {
|
switch (@typeInfo(T)) {
|
||||||
.array => |info| {
|
.array => |info| return info.sentinel(),
|
||||||
const sentinel_ptr = info.sentinel orelse return null;
|
|
||||||
return @as(*const info.child, @ptrCast(sentinel_ptr)).*;
|
|
||||||
},
|
|
||||||
.pointer => |info| {
|
.pointer => |info| {
|
||||||
switch (info.size) {
|
switch (info.size) {
|
||||||
.many, .slice => {
|
.many, .slice => return info.sentinel(),
|
||||||
const sentinel_ptr = info.sentinel orelse return null;
|
|
||||||
return @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*;
|
|
||||||
},
|
|
||||||
.one => switch (@typeInfo(info.child)) {
|
.one => switch (@typeInfo(info.child)) {
|
||||||
.array => |array_info| {
|
.array => |array_info| return array_info.sentinel(),
|
||||||
const sentinel_ptr = array_info.sentinel orelse return null;
|
|
||||||
return @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
|
|
||||||
},
|
|
||||||
else => {},
|
else => {},
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
@ -190,11 +181,11 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
|
|||||||
.array = .{
|
.array = .{
|
||||||
.len = array_info.len,
|
.len = array_info.len,
|
||||||
.child = array_info.child,
|
.child = array_info.child,
|
||||||
.sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
|
.sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
.is_allowzero = info.is_allowzero,
|
.is_allowzero = info.is_allowzero,
|
||||||
.sentinel = info.sentinel,
|
.sentinel_ptr = info.sentinel_ptr,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
else => {},
|
else => {},
|
||||||
@ -208,7 +199,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
|
|||||||
.address_space = info.address_space,
|
.address_space = info.address_space,
|
||||||
.child = info.child,
|
.child = info.child,
|
||||||
.is_allowzero = info.is_allowzero,
|
.is_allowzero = info.is_allowzero,
|
||||||
.sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
|
.sentinel_ptr = @as(?*const anyopaque, @ptrCast(&sentinel_val)),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
else => {},
|
else => {},
|
||||||
@ -226,7 +217,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
|
|||||||
.address_space = ptr_info.address_space,
|
.address_space = ptr_info.address_space,
|
||||||
.child = ptr_info.child,
|
.child = ptr_info.child,
|
||||||
.is_allowzero = ptr_info.is_allowzero,
|
.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] = .{
|
tuple_fields[i] = .{
|
||||||
.name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,
|
.name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,
|
||||||
.type = T,
|
.type = T,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -25,7 +25,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
|
|||||||
fields[i] = Type.StructField{
|
fields[i] = Type.StructField{
|
||||||
.name = struct_field.name,
|
.name = struct_field.name,
|
||||||
.type = ?struct_field.type,
|
.type = ?struct_field.type,
|
||||||
.default_value = &@as(?struct_field.type, null),
|
.default_value_ptr = &@as(?struct_field.type, null),
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(?struct_field.type),
|
.alignment = @alignOf(?struct_field.type),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -571,7 +571,7 @@ pub fn MultiArrayList(comptime T: type) type {
|
|||||||
for (&entry_fields, sizes.fields) |*entry_field, i| entry_field.* = .{
|
for (&entry_fields, sizes.fields) |*entry_field, i| entry_field.* = .{
|
||||||
.name = fields[i].name ++ "_ptr",
|
.name = fields[i].name ++ "_ptr",
|
||||||
.type = *fields[i].type,
|
.type = *fields[i].type,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = fields[i].is_comptime,
|
.is_comptime = fields[i].is_comptime,
|
||||||
.alignment = fields[i].alignment,
|
.alignment = fields[i].alignment,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -180,10 +180,7 @@ pub fn sizeof(target: anytype) usize {
|
|||||||
// specially handled here.
|
// specially handled here.
|
||||||
if (ptr.size == .one and ptr.is_const and @typeInfo(ptr.child) == .array) {
|
if (ptr.size == .one and ptr.is_const and @typeInfo(ptr.child) == .array) {
|
||||||
const array_info = @typeInfo(ptr.child).array;
|
const array_info = @typeInfo(ptr.child).array;
|
||||||
if ((array_info.child == u8 or array_info.child == u16) and
|
if ((array_info.child == u8 or array_info.child == u16) and array_info.sentinel() == 0) {
|
||||||
array_info.sentinel != null and
|
|
||||||
@as(*align(1) const array_info.child, @ptrCast(array_info.sentinel.?)).* == 0)
|
|
||||||
{
|
|
||||||
// length of the string plus one for the null terminator.
|
// length of the string plus one for the null terminator.
|
||||||
return (array_info.len + 1) * @sizeOf(array_info.child);
|
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,
|
.address_space = .generic,
|
||||||
.child = ElementType,
|
.child = ElementType,
|
||||||
.is_allowzero = true,
|
.is_allowzero = true,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
} });
|
} });
|
||||||
},
|
},
|
||||||
else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)),
|
else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)),
|
||||||
|
|||||||
@ -1134,7 +1134,7 @@ const Local = struct {
|
|||||||
for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{
|
for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{
|
||||||
.name = elem_field.name,
|
.name = elem_field.name,
|
||||||
.type = *[len]elem_field.type,
|
.type = *[len]elem_field.type,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
};
|
};
|
||||||
@ -1162,9 +1162,9 @@ const Local = struct {
|
|||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = elem_field.type,
|
.child = elem_field.type,
|
||||||
.is_allowzero = false,
|
.is_allowzero = false,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
} }),
|
} }),
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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: {
|
@field(result, field.name) = if (struct_obj.nameIndex(ip, field_name_ip)) |field_idx| f: {
|
||||||
const field_val = try val.fieldValue(pt, field_idx);
|
const field_val = try val.fieldValue(pt, field_idx);
|
||||||
break :f try field_val.interpret(field.type, pt);
|
break :f try field_val.interpret(field.type, pt);
|
||||||
} else if (field.default_value) |ptr| f: {
|
} else (field.defaultValue() orelse return error.TypeMismatch);
|
||||||
const typed_ptr: *const field.type = @ptrCast(@alignCast(ptr));
|
|
||||||
break :f typed_ptr.*;
|
|
||||||
} else return error.TypeMismatch;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|||||||
@ -8463,7 +8463,7 @@ pub const Metadata = enum(u32) {
|
|||||||
field.* = .{
|
field.* = .{
|
||||||
.name = name,
|
.name = name,
|
||||||
.type = []const u8,
|
.type = []const u8,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
};
|
};
|
||||||
@ -8474,7 +8474,7 @@ pub const Metadata = enum(u32) {
|
|||||||
field.* = .{
|
field.* = .{
|
||||||
.name = name,
|
.name = name,
|
||||||
.type = std.fmt.Formatter(format),
|
.type = std.fmt.Formatter(format),
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -141,14 +141,14 @@ test "array-like initializer for tuple types" {
|
|||||||
.{
|
.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = i32,
|
.type = i32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(i32),
|
.alignment = @alignOf(i32),
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.name = "1",
|
.name = "1",
|
||||||
.type = u8,
|
.type = u8,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(u8),
|
.alignment = @alignOf(u8),
|
||||||
},
|
},
|
||||||
@ -330,7 +330,7 @@ test "zero sized struct in tuple handled correctly" {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = struct {},
|
.type = struct {},
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
}},
|
}},
|
||||||
|
|||||||
@ -20,13 +20,13 @@ test "tuple declaration type info" {
|
|||||||
|
|
||||||
try expectEqualStrings(info.fields[0].name, "0");
|
try expectEqualStrings(info.fields[0].name, "0");
|
||||||
try expect(info.fields[0].type == u32);
|
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].is_comptime);
|
||||||
try expect(info.fields[0].alignment == @alignOf(u32));
|
try expect(info.fields[0].alignment == @alignOf(u32));
|
||||||
|
|
||||||
try expectEqualStrings(info.fields[1].name, "1");
|
try expectEqualStrings(info.fields[1].name, "1");
|
||||||
try expect(info.fields[1].type == []const u8);
|
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].is_comptime);
|
||||||
try expect(info.fields[1].alignment == @alignOf([]const u8));
|
try expect(info.fields[1].alignment == @alignOf([]const u8));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -118,21 +118,21 @@ test "Type.Array" {
|
|||||||
.array = .{
|
.array = .{
|
||||||
.len = 123,
|
.len = 123,
|
||||||
.child = u8,
|
.child = u8,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
try testing.expect([2]u32 == @Type(.{
|
try testing.expect([2]u32 == @Type(.{
|
||||||
.array = .{
|
.array = .{
|
||||||
.len = 2,
|
.len = 2,
|
||||||
.child = u32,
|
.child = u32,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
try testing.expect([2:0]u32 == @Type(.{
|
try testing.expect([2:0]u32 == @Type(.{
|
||||||
.array = .{
|
.array = .{
|
||||||
.len = 2,
|
.len = 2,
|
||||||
.child = u32,
|
.child = u32,
|
||||||
.sentinel = &@as(u32, 0),
|
.sentinel_ptr = &@as(u32, 0),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
|
try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
|
||||||
@ -148,7 +148,7 @@ test "@Type create slice with null sentinel" {
|
|||||||
.alignment = 8,
|
.alignment = 8,
|
||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = *i32,
|
.child = *i32,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
try testing.expect(Slice == []align(8) const *i32);
|
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.expectEqual(Type.ContainerLayout.auto, infoA.layout);
|
||||||
try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
|
try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
|
||||||
try testing.expectEqual(u8, infoA.fields[0].type);
|
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.expectEqualSlices(u8, "y", infoA.fields[1].name);
|
||||||
try testing.expectEqual(u32, infoA.fields[1].type);
|
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.expectEqualSlices(Type.Declaration, &.{}, infoA.decls);
|
||||||
try testing.expectEqual(@as(bool, false), infoA.is_tuple);
|
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.expectEqual(Type.ContainerLayout.@"extern", infoB.layout);
|
||||||
try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
|
try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
|
||||||
try testing.expectEqual(u8, infoB.fields[0].type);
|
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.expectEqualSlices(u8, "y", infoB.fields[1].name);
|
||||||
try testing.expectEqual(u32, infoB.fields[1].type);
|
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(usize, 0), infoB.decls.len);
|
||||||
try testing.expectEqual(@as(bool, false), infoB.is_tuple);
|
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.expectEqual(Type.ContainerLayout.@"packed", infoC.layout);
|
||||||
try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
|
try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
|
||||||
try testing.expectEqual(u8, infoC.fields[0].type);
|
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.expectEqualSlices(u8, "y", infoC.fields[1].name);
|
||||||
try testing.expectEqual(u32, infoC.fields[1].type);
|
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(usize, 0), infoC.decls.len);
|
||||||
try testing.expectEqual(@as(bool, false), infoC.is_tuple);
|
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.expectEqual(Type.ContainerLayout.auto, infoD.layout);
|
||||||
try testing.expectEqualSlices(u8, "x", infoD.fields[0].name);
|
try testing.expectEqualSlices(u8, "x", infoD.fields[0].name);
|
||||||
try testing.expectEqual(comptime_int, infoD.fields[0].type);
|
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.expectEqualSlices(u8, "y", infoD.fields[1].name);
|
||||||
try testing.expectEqual(comptime_int, infoD.fields[1].type);
|
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(usize, 0), infoD.decls.len);
|
||||||
try testing.expectEqual(@as(bool, false), infoD.is_tuple);
|
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.expectEqual(Type.ContainerLayout.auto, infoE.layout);
|
||||||
try testing.expectEqualSlices(u8, "0", infoE.fields[0].name);
|
try testing.expectEqualSlices(u8, "0", infoE.fields[0].name);
|
||||||
try testing.expectEqual(comptime_int, infoE.fields[0].type);
|
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.expectEqualSlices(u8, "1", infoE.fields[1].name);
|
||||||
try testing.expectEqual(comptime_int, infoE.fields[1].type);
|
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(usize, 0), infoE.decls.len);
|
||||||
try testing.expectEqual(@as(bool, true), infoE.is_tuple);
|
try testing.expectEqual(@as(bool, true), infoE.is_tuple);
|
||||||
|
|
||||||
@ -582,7 +582,7 @@ test "reified struct field name from optional payload" {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = name,
|
.name = name,
|
||||||
.type = u8,
|
.type = u8,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 1,
|
.alignment = 1,
|
||||||
}},
|
}},
|
||||||
@ -628,7 +628,7 @@ test "reified struct uses @alignOf" {
|
|||||||
.{
|
.{
|
||||||
.name = "globals",
|
.name = "globals",
|
||||||
.type = modules.mach.globals,
|
.type = modules.mach.globals,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(modules.mach.globals),
|
.alignment = @alignOf(modules.mach.globals),
|
||||||
},
|
},
|
||||||
@ -688,7 +688,7 @@ test "empty struct assigned to reified struct field" {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "components",
|
.name = "components",
|
||||||
.type = @TypeOf(modules.components),
|
.type = @TypeOf(modules.components),
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = @alignOf(@TypeOf(modules.components)),
|
.alignment = @alignOf(@TypeOf(modules.components)),
|
||||||
}},
|
}},
|
||||||
@ -738,7 +738,7 @@ test "struct field names sliced at comptime from larger string" {
|
|||||||
.alignment = 0,
|
.alignment = 0,
|
||||||
.name = name ++ "",
|
.name = name ++ "",
|
||||||
.type = usize,
|
.type = usize,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -84,7 +84,7 @@ fn testPointer() !void {
|
|||||||
try expect(u32_ptr_info.pointer.is_volatile == false);
|
try expect(u32_ptr_info.pointer.is_volatile == false);
|
||||||
try expect(u32_ptr_info.pointer.alignment == @alignOf(u32));
|
try expect(u32_ptr_info.pointer.alignment == @alignOf(u32));
|
||||||
try expect(u32_ptr_info.pointer.child == 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" {
|
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.size == .many);
|
||||||
try expect(u32_ptr_info.pointer.is_const == true);
|
try expect(u32_ptr_info.pointer.is_const == true);
|
||||||
try expect(u32_ptr_info.pointer.is_volatile == 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.alignment == @alignOf(f64));
|
||||||
try expect(u32_ptr_info.pointer.child == 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.size == .many);
|
||||||
try expect(ptr_info.pointer.is_const == false);
|
try expect(ptr_info.pointer.is_const == false);
|
||||||
try expect(ptr_info.pointer.is_volatile == 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" {
|
test "type info: slice type info" {
|
||||||
@ -145,14 +145,14 @@ fn testArray() !void {
|
|||||||
try expect(info == .array);
|
try expect(info == .array);
|
||||||
try expect(info.array.len == 42);
|
try expect(info.array.len == 42);
|
||||||
try expect(info.array.child == u8);
|
try expect(info.array.child == u8);
|
||||||
try expect(info.array.sentinel == null);
|
try expect(info.array.sentinel() == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const info = @typeInfo([10:0]u8);
|
const info = @typeInfo([10:0]u8);
|
||||||
try expect(info.array.len == 10);
|
try expect(info.array.len == 10);
|
||||||
try expect(info.array.child == u8);
|
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);
|
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".is_tuple == false);
|
||||||
try expect(unpacked_struct_info.@"struct".backing_integer == null);
|
try expect(unpacked_struct_info.@"struct".backing_integer == null);
|
||||||
try expect(unpacked_struct_info.@"struct".fields[0].alignment == @alignOf(u32));
|
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(unpacked_struct_info.@"struct".fields[0].defaultValue().? == 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(mem.eql(u8, "foobar", unpacked_struct_info.@"struct".fields[1].defaultValue().?));
|
||||||
}
|
}
|
||||||
|
|
||||||
const TestStruct = struct {
|
const TestStruct = struct {
|
||||||
@ -315,8 +315,8 @@ fn testPackedStruct() !void {
|
|||||||
try expect(struct_info.@"struct".fields.len == 4);
|
try expect(struct_info.@"struct".fields.len == 4);
|
||||||
try expect(struct_info.@"struct".fields[0].alignment == 0);
|
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].type == f32);
|
||||||
try expect(struct_info.@"struct".fields[2].default_value == null);
|
try expect(struct_info.@"struct".fields[2].defaultValue() == null);
|
||||||
try expect(@as(*align(1) const u32, @ptrCast(struct_info.@"struct".fields[3].default_value.?)).* == 4);
|
try expect(struct_info.@"struct".fields[3].defaultValue().? == 4);
|
||||||
try expect(struct_info.@"struct".fields[3].alignment == 0);
|
try expect(struct_info.@"struct".fields[3].alignment == 0);
|
||||||
try expect(struct_info.@"struct".decls.len == 1);
|
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.address_space == .generic);
|
||||||
try expect(foo_ptr_fn_info.pointer.child == foo_fn_type);
|
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.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.
|
// Avoid looking at `typeInfoFooAligned` on targets which don't support function alignment.
|
||||||
switch (builtin.target.cpu.arch) {
|
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.address_space == .generic);
|
||||||
try expect(aligned_foo_ptr_fn_info.pointer.child == aligned_foo_fn_type);
|
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.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;
|
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" {
|
test "sentinel of opaque pointer type" {
|
||||||
const c_void_info = @typeInfo(*anyopaque);
|
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" {
|
test "@typeInfo does not force declarations into existence" {
|
||||||
@ -601,9 +601,9 @@ test "typeInfo resolves usingnamespace declarations" {
|
|||||||
try expectEqualStrings(decls[1].name, "f1");
|
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 {
|
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);
|
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" {
|
test "type info of tuple of string literal default value" {
|
||||||
const struct_field = @typeInfo(@TypeOf(.{"hi"})).@"struct".fields[0];
|
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');
|
comptime std.debug.assert(value[0] == 'h');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ export fn entry() void {
|
|||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = u8,
|
.child = u8,
|
||||||
.is_allowzero = false,
|
.is_allowzero = false,
|
||||||
.sentinel = &@as(u8, 0),
|
.sentinel_ptr = &@as(u8, 0),
|
||||||
} });
|
} });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ comptime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
comptime {
|
comptime {
|
||||||
_ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel = &sentinel } });
|
_ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel_ptr = &sentinel } });
|
||||||
}
|
}
|
||||||
comptime {
|
comptime {
|
||||||
_ = @Type(.{ .pointer = .{
|
_ = @Type(.{ .pointer = .{
|
||||||
@ -23,7 +23,7 @@ comptime {
|
|||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = S,
|
.child = S,
|
||||||
.is_allowzero = false,
|
.is_allowzero = false,
|
||||||
.sentinel = &sentinel,
|
.sentinel_ptr = &sentinel,
|
||||||
} });
|
} });
|
||||||
}
|
}
|
||||||
comptime {
|
comptime {
|
||||||
@ -35,7 +35,7 @@ comptime {
|
|||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = S,
|
.child = S,
|
||||||
.is_allowzero = false,
|
.is_allowzero = false,
|
||||||
.sentinel = &sentinel,
|
.sentinel_ptr = &sentinel,
|
||||||
} });
|
} });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,9 @@
|
|||||||
export fn entry() void {
|
export fn entry() void {
|
||||||
_ = @Type(.{ .@"struct" = .{ .layout = .@"packed", .fields = &.{
|
_ = @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 } });
|
}, .decls = &.{}, .is_tuple = false } });
|
||||||
}
|
}
|
||||||
|
|
||||||
// error
|
// error
|
||||||
// backend=stage2
|
|
||||||
// target=native
|
|
||||||
//
|
//
|
||||||
// :2:9: error: alignment in a packed struct field must be set to 0
|
// :2:9: error: alignment in a packed struct field must be set to 0
|
||||||
|
|||||||
@ -4,7 +4,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "foo",
|
.name = "foo",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 4,
|
.alignment = 4,
|
||||||
}},
|
}},
|
||||||
@ -18,7 +18,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "3",
|
.name = "3",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 4,
|
.alignment = 4,
|
||||||
}},
|
}},
|
||||||
@ -32,7 +32,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = true,
|
.is_comptime = true,
|
||||||
.alignment = 4,
|
.alignment = 4,
|
||||||
}},
|
}},
|
||||||
@ -46,7 +46,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = true,
|
.is_comptime = true,
|
||||||
.alignment = 4,
|
.alignment = 4,
|
||||||
}},
|
}},
|
||||||
@ -60,7 +60,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = true,
|
.is_comptime = true,
|
||||||
.alignment = 4,
|
.alignment = 4,
|
||||||
}},
|
}},
|
||||||
@ -70,8 +70,6 @@ comptime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// error
|
// error
|
||||||
// backend=stage2
|
|
||||||
// target=native
|
|
||||||
//
|
//
|
||||||
// :2:5: error: tuple cannot have non-numeric field 'foo'
|
// :2:5: error: tuple cannot have non-numeric field 'foo'
|
||||||
// :16:5: error: tuple field name '3' does not match field index 0
|
// :16:5: error: tuple field name '3' does not match field index 0
|
||||||
|
|||||||
@ -17,7 +17,7 @@ comptime {
|
|||||||
.fields = &.{.{
|
.fields = &.{.{
|
||||||
.name = "0",
|
.name = "0",
|
||||||
.type = u32,
|
.type = u32,
|
||||||
.default_value = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = true,
|
.is_comptime = true,
|
||||||
.alignment = 5,
|
.alignment = 5,
|
||||||
}},
|
}},
|
||||||
@ -36,7 +36,7 @@ comptime {
|
|||||||
.address_space = .generic,
|
.address_space = .generic,
|
||||||
.child = u8,
|
.child = u8,
|
||||||
.is_allowzero = false,
|
.is_allowzero = false,
|
||||||
.sentinel = null,
|
.sentinel_ptr = null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
comptime {
|
comptime {
|
||||||
_ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel = undefined } });
|
_ = @Type(.{ .array = .{ .len = 0, .child = u8, .sentinel_ptr = undefined } });
|
||||||
}
|
}
|
||||||
comptime {
|
comptime {
|
||||||
_ = @Type(.{
|
_ = @Type(.{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user