Skip to content

Commit f4a2493

Browse files
committed
stage1: avoid anytype fields for type info
prerequisite for ziglang#10705
1 parent 5cf9181 commit f4a2493

File tree

10 files changed

+246
-223
lines changed

10 files changed

+246
-223
lines changed

lib/std/builtin.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,10 @@ pub const TypeInfo = union(enum) {
226226
child: type,
227227
is_allowzero: bool,
228228

229-
/// This field is an optional type.
230229
/// The type of the sentinel is the element type of the pointer, which is
231230
/// the value of the `child` field in this struct. However there is no way
232-
/// to refer to that type here, so we use `anytype`.
233-
sentinel: anytype,
231+
/// to refer to that type here, so we use pointer to `anyopaque`.
232+
sentinel: ?*const anyopaque,
234233

235234
/// This data structure is used by the Zig language code generation and
236235
/// therefore must be kept in sync with the compiler implementation.
@@ -248,11 +247,10 @@ pub const TypeInfo = union(enum) {
248247
len: comptime_int,
249248
child: type,
250249

251-
/// This field is an optional type.
252250
/// The type of the sentinel is the element type of the array, which is
253251
/// the value of the `child` field in this struct. However there is no way
254-
/// to refer to that type here, so we use `anytype`.
255-
sentinel: anytype,
252+
/// to refer to that type here, so we use pointer to `anyopaque`.
253+
sentinel: ?*const anyopaque,
256254
};
257255

258256
/// This data structure is used by the Zig language code generation and
@@ -267,8 +265,9 @@ pub const TypeInfo = union(enum) {
267265
/// therefore must be kept in sync with the compiler implementation.
268266
pub const StructField = struct {
269267
name: []const u8,
268+
/// TODO rename to `type`
270269
field_type: type,
271-
default_value: anytype,
270+
default_value: ?*const anyopaque,
272271
is_comptime: bool,
273272
alignment: comptime_int,
274273
};
@@ -369,7 +368,7 @@ pub const TypeInfo = union(enum) {
369368
/// This data structure is used by the Zig language code generation and
370369
/// therefore must be kept in sync with the compiler implementation.
371370
pub const Frame = struct {
372-
function: anytype,
371+
function: *const anyopaque,
373372
};
374373

375374
/// This data structure is used by the Zig language code generation and

lib/std/enums.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
1616
fields = fields ++ &[_]StructField{.{
1717
.name = field.name,
1818
.field_type = Data,
19-
.default_value = field_default,
19+
.default_value = if (field_default) |d| &d else null,
2020
.is_comptime = false,
2121
.alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
2222
}};

lib/std/mem.zig

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ pub fn zeroes(comptime T: type) T {
296296
}
297297
},
298298
.Array => |info| {
299-
if (info.sentinel) |sentinel| {
299+
if (info.sentinel) |sentinel_ptr| {
300+
const sentinel = @ptrCast(*const info.child, sentinel_ptr).*;
300301
return [_:sentinel]info.child{zeroes(info.child)} ** info.len;
301302
}
302303
return [_]info.child{zeroes(info.child)} ** info.len;
@@ -453,7 +454,8 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
453454
@field(value, field.name) = @field(init, field.name);
454455
},
455456
}
456-
} else if (field.default_value) |default_value| {
457+
} else if (field.default_value) |default_value_ptr| {
458+
const default_value = @ptrCast(*const field.field_type, default_value_ptr).*;
457459
@field(value, field.name) = default_value;
458460
}
459461
}
@@ -599,7 +601,7 @@ pub fn Span(comptime T: type) type {
599601
else => @compileError("invalid type given to std.mem.Span"),
600602
},
601603
.C => {
602-
new_ptr_info.sentinel = 0;
604+
new_ptr_info.sentinel = &@as(ptr_info.child, 0);
603605
new_ptr_info.is_allowzero = false;
604606
},
605607
.Many, .Slice => {},
@@ -651,7 +653,9 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
651653
}
652654
const Result = Span(@TypeOf(ptr));
653655
const l = len(ptr);
654-
if (@typeInfo(Result).Pointer.sentinel) |s| {
656+
const ptr_info = @typeInfo(Result).Pointer;
657+
if (ptr_info.sentinel) |s_ptr| {
658+
const s = @ptrCast(*const ptr_info.child, s_ptr).*;
655659
return ptr[0..l :s];
656660
} else {
657661
return ptr[0..l];
@@ -684,9 +688,10 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
684688
// The return type must only be sentinel terminated if we are guaranteed
685689
// to find the value searched for, which is only the case if it matches
686690
// the sentinel of the type passed.
687-
if (array_info.sentinel) |sentinel| {
691+
if (array_info.sentinel) |sentinel_ptr| {
692+
const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*;
688693
if (end == sentinel) {
689-
new_ptr_info.sentinel = end;
694+
new_ptr_info.sentinel = &end;
690695
} else {
691696
new_ptr_info.sentinel = null;
692697
}
@@ -698,16 +703,17 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
698703
// The return type must only be sentinel terminated if we are guaranteed
699704
// to find the value searched for, which is only the case if it matches
700705
// the sentinel of the type passed.
701-
if (ptr_info.sentinel) |sentinel| {
706+
if (ptr_info.sentinel) |sentinel_ptr| {
707+
const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
702708
if (end == sentinel) {
703-
new_ptr_info.sentinel = end;
709+
new_ptr_info.sentinel = &end;
704710
} else {
705711
new_ptr_info.sentinel = null;
706712
}
707713
}
708714
},
709715
.C => {
710-
new_ptr_info.sentinel = end;
716+
new_ptr_info.sentinel = &end;
711717
// C pointers are always allowzero, but we don't want the return type to be.
712718
assert(new_ptr_info.is_allowzero);
713719
new_ptr_info.is_allowzero = false;
@@ -734,7 +740,9 @@ pub fn sliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) SliceTo(@Typ
734740
}
735741
const Result = SliceTo(@TypeOf(ptr), end);
736742
const length = lenSliceTo(ptr, end);
737-
if (@typeInfo(Result).Pointer.sentinel) |s| {
743+
const ptr_info = @typeInfo(Result).Pointer;
744+
if (ptr_info.sentinel) |s_ptr| {
745+
const s = @ptrCast(*const ptr_info.child, s_ptr).*;
738746
return ptr[0..length :s];
739747
} else {
740748
return ptr[0..length];
@@ -786,7 +794,8 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
786794
.Pointer => |ptr_info| switch (ptr_info.size) {
787795
.One => switch (@typeInfo(ptr_info.child)) {
788796
.Array => |array_info| {
789-
if (array_info.sentinel) |sentinel| {
797+
if (array_info.sentinel) |sentinel_ptr| {
798+
const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*;
790799
if (sentinel == end) {
791800
return indexOfSentinel(array_info.child, end, ptr);
792801
}
@@ -795,7 +804,8 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
795804
},
796805
else => {},
797806
},
798-
.Many => if (ptr_info.sentinel) |sentinel| {
807+
.Many => if (ptr_info.sentinel) |sentinel_ptr| {
808+
const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
799809
// We may be looking for something other than the sentinel,
800810
// but iterating past the sentinel would be a bug so we need
801811
// to check for both.
@@ -808,7 +818,8 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
808818
return indexOfSentinel(ptr_info.child, end, ptr);
809819
},
810820
.Slice => {
811-
if (ptr_info.sentinel) |sentinel| {
821+
if (ptr_info.sentinel) |sentinel_ptr| {
822+
const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
812823
if (sentinel == end) {
813824
return indexOfSentinel(ptr_info.child, sentinel, ptr);
814825
}
@@ -867,10 +878,12 @@ pub fn len(value: anytype) usize {
867878
.Array => value.len,
868879
else => @compileError("invalid type given to std.mem.len"),
869880
},
870-
.Many => if (info.sentinel) |sentinel|
871-
indexOfSentinel(info.child, sentinel, value)
872-
else
873-
@compileError("length of pointer with no sentinel"),
881+
.Many => {
882+
const sentinel_ptr = info.sentinel orelse
883+
@compileError("length of pointer with no sentinel");
884+
const sentinel = @ptrCast(*const info.child, sentinel_ptr).*;
885+
return indexOfSentinel(info.child, sentinel, value);
886+
},
874887
.C => {
875888
assert(value != null);
876889
return indexOfSentinel(info.child, 0, value);
@@ -2572,7 +2585,11 @@ test "alignPointer" {
25722585
try S.checkAlign([*]u32, math.maxInt(usize) - 3, 8, 0);
25732586
}
25742587

2575-
fn CopyPtrAttrs(comptime source: type, comptime size: std.builtin.TypeInfo.Pointer.Size, comptime child: type) type {
2588+
fn CopyPtrAttrs(
2589+
comptime source: type,
2590+
comptime size: std.builtin.TypeInfo.Pointer.Size,
2591+
comptime child: type,
2592+
) type {
25762593
const info = @typeInfo(source).Pointer;
25772594
return @Type(.{
25782595
.Pointer = .{

lib/std/meta.zig

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,21 @@ test "std.meta.Elem" {
190190
/// Types which cannot possibly have a sentinel will be a compile error.
191191
pub fn sentinel(comptime T: type) ?Elem(T) {
192192
switch (@typeInfo(T)) {
193-
.Array => |info| return info.sentinel,
193+
.Array => |info| {
194+
const sentinel_ptr = info.sentinel orelse return null;
195+
return @ptrCast(*const info.child, sentinel_ptr).*;
196+
},
194197
.Pointer => |info| {
195198
switch (info.size) {
196-
.Many, .Slice => return info.sentinel,
199+
.Many, .Slice => {
200+
const sentinel_ptr = info.sentinel orelse return null;
201+
return @ptrCast(*const info.child, sentinel_ptr).*;
202+
},
197203
.One => switch (@typeInfo(info.child)) {
198-
.Array => |array_info| return array_info.sentinel,
204+
.Array => |array_info| {
205+
const sentinel_ptr = array_info.sentinel orelse return null;
206+
return @ptrCast(*const array_info.child, sentinel_ptr).*;
207+
},
199208
else => {},
200209
},
201210
else => {},
@@ -239,7 +248,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
239248
.Array = .{
240249
.len = array_info.len,
241250
.child = array_info.child,
242-
.sentinel = sentinel_val,
251+
.sentinel = &sentinel_val,
243252
},
244253
}),
245254
.is_allowzero = info.is_allowzero,
@@ -257,7 +266,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
257266
.address_space = info.address_space,
258267
.child = info.child,
259268
.is_allowzero = info.is_allowzero,
260-
.sentinel = sentinel_val,
269+
.sentinel = &sentinel_val,
261270
},
262271
}),
263272
else => {},
@@ -275,7 +284,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
275284
.address_space = ptr_info.address_space,
276285
.child = ptr_info.child,
277286
.is_allowzero = ptr_info.is_allowzero,
278-
.sentinel = sentinel_val,
287+
.sentinel = &sentinel_val,
279288
},
280289
}),
281290
},

src/stage1/all_types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ struct CodeGen {
21082108
ZigType *entry_global_error_set;
21092109
ZigType *entry_enum_literal;
21102110
ZigType *entry_any_frame;
2111+
ZigType *entry_opt_ptr_const_anyopaque;
21112112
} builtin_types;
21122113

21132114
struct Intern {

src/stage1/codegen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9451,6 +9451,12 @@ static void define_builtin_types(CodeGen *g) {
94519451
g->primitive_type_table.put(&g->builtin_types.entry_anyopaque->name, g->builtin_types.entry_anyopaque);
94529452
}
94539453

9454+
{
9455+
ZigType *ptr_const_anyopaque = get_pointer_to_type(g,
9456+
g->builtin_types.entry_anyopaque, true);
9457+
g->builtin_types.entry_opt_ptr_const_anyopaque = get_optional_type(g, ptr_const_anyopaque);
9458+
}
9459+
94549460
{
94559461
ZigType *entry = new_type_table_entry(ZigTypeIdErrorSet);
94569462
buf_init_from_str(&entry->name, "anyerror");

0 commit comments

Comments
 (0)