From f4a249325e8e3741a6294462ae37a79cb9089c56 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Feb 2022 18:10:19 -0700 Subject: stage1: avoid anytype fields for type info prerequisite for #10705 --- lib/std/builtin.zig | 15 +++++++-------- lib/std/enums.zig | 2 +- lib/std/mem.zig | 53 +++++++++++++++++++++++++++++++++++------------------ lib/std/meta.zig | 21 +++++++++++++++------ 4 files changed, 58 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 7c37fa7616..9e7bfc99ba 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -226,11 +226,10 @@ pub const TypeInfo = union(enum) { child: type, is_allowzero: bool, - /// This field is an optional type. /// The type of the sentinel is the element type of the pointer, which is /// the value of the `child` field in this struct. However there is no way - /// to refer to that type here, so we use `anytype`. - sentinel: anytype, + /// to refer to that type here, so we use pointer to `anyopaque`. + sentinel: ?*const anyopaque, /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. @@ -248,11 +247,10 @@ pub const TypeInfo = union(enum) { len: comptime_int, child: type, - /// This field is an optional type. /// The type of the sentinel is the element type of the array, which is /// the value of the `child` field in this struct. However there is no way - /// to refer to that type here, so we use `anytype`. - sentinel: anytype, + /// to refer to that type here, so we use pointer to `anyopaque`. + sentinel: ?*const anyopaque, }; /// This data structure is used by the Zig language code generation and @@ -267,8 +265,9 @@ pub const TypeInfo = union(enum) { /// therefore must be kept in sync with the compiler implementation. pub const StructField = struct { name: []const u8, + /// TODO rename to `type` field_type: type, - default_value: anytype, + default_value: ?*const anyopaque, is_comptime: bool, alignment: comptime_int, }; @@ -369,7 +368,7 @@ pub const TypeInfo = union(enum) { /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. pub const Frame = struct { - function: anytype, + function: *const anyopaque, }; /// This data structure is used by the Zig language code generation and diff --git a/lib/std/enums.zig b/lib/std/enums.zig index cfcc8e4f1d..9ac80bd907 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def fields = fields ++ &[_]StructField{.{ .name = field.name, .field_type = Data, - .default_value = field_default, + .default_value = if (field_default) |d| &d else null, .is_comptime = false, .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0, }}; diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 03dc27ea53..71de42aad7 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -296,7 +296,8 @@ pub fn zeroes(comptime T: type) T { } }, .Array => |info| { - if (info.sentinel) |sentinel| { + if (info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const info.child, sentinel_ptr).*; return [_:sentinel]info.child{zeroes(info.child)} ** info.len; } return [_]info.child{zeroes(info.child)} ** info.len; @@ -453,7 +454,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| { + } else if (field.default_value) |default_value_ptr| { + const default_value = @ptrCast(*const field.field_type, default_value_ptr).*; @field(value, field.name) = default_value; } } @@ -599,7 +601,7 @@ pub fn Span(comptime T: type) type { else => @compileError("invalid type given to std.mem.Span"), }, .C => { - new_ptr_info.sentinel = 0; + new_ptr_info.sentinel = &@as(ptr_info.child, 0); new_ptr_info.is_allowzero = false; }, .Many, .Slice => {}, @@ -651,7 +653,9 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) { } const Result = Span(@TypeOf(ptr)); const l = len(ptr); - if (@typeInfo(Result).Pointer.sentinel) |s| { + const ptr_info = @typeInfo(Result).Pointer; + if (ptr_info.sentinel) |s_ptr| { + const s = @ptrCast(*const ptr_info.child, s_ptr).*; return ptr[0..l :s]; } else { return ptr[0..l]; @@ -684,9 +688,10 @@ fn SliceTo(comptime T: type, comptime end: 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| { + if (array_info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*; if (end == sentinel) { - new_ptr_info.sentinel = end; + new_ptr_info.sentinel = &end; } else { new_ptr_info.sentinel = null; } @@ -698,16 +703,17 @@ fn SliceTo(comptime T: type, comptime end: 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| { + if (ptr_info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*; if (end == sentinel) { - new_ptr_info.sentinel = end; + new_ptr_info.sentinel = &end; } else { new_ptr_info.sentinel = null; } } }, .C => { - new_ptr_info.sentinel = end; + new_ptr_info.sentinel = &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; @@ -734,7 +740,9 @@ pub fn sliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) SliceTo(@Typ } const Result = SliceTo(@TypeOf(ptr), end); const length = lenSliceTo(ptr, end); - if (@typeInfo(Result).Pointer.sentinel) |s| { + const ptr_info = @typeInfo(Result).Pointer; + if (ptr_info.sentinel) |s_ptr| { + const s = @ptrCast(*const ptr_info.child, s_ptr).*; return ptr[0..length :s]; } else { return ptr[0..length]; @@ -786,7 +794,8 @@ fn lenSliceTo(ptr: anytype, comptime end: 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| { + if (array_info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*; if (sentinel == end) { return indexOfSentinel(array_info.child, end, ptr); } @@ -795,7 +804,8 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize { }, else => {}, }, - .Many => if (ptr_info.sentinel) |sentinel| { + .Many => if (ptr_info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*; // We may be looking for something other than the sentinel, // but iterating past the sentinel would be a bug so we need // to check for both. @@ -808,7 +818,8 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize { return indexOfSentinel(ptr_info.child, end, ptr); }, .Slice => { - if (ptr_info.sentinel) |sentinel| { + if (ptr_info.sentinel) |sentinel_ptr| { + const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*; if (sentinel == end) { return indexOfSentinel(ptr_info.child, sentinel, ptr); } @@ -867,10 +878,12 @@ pub fn len(value: anytype) usize { .Array => value.len, else => @compileError("invalid type given to std.mem.len"), }, - .Many => if (info.sentinel) |sentinel| - indexOfSentinel(info.child, sentinel, value) - else - @compileError("length of pointer with no sentinel"), + .Many => { + const sentinel_ptr = info.sentinel orelse + @compileError("length of pointer with no sentinel"); + const sentinel = @ptrCast(*const info.child, sentinel_ptr).*; + return indexOfSentinel(info.child, sentinel, value); + }, .C => { assert(value != null); return indexOfSentinel(info.child, 0, value); @@ -2572,7 +2585,11 @@ test "alignPointer" { try S.checkAlign([*]u32, math.maxInt(usize) - 3, 8, 0); } -fn CopyPtrAttrs(comptime source: type, comptime size: std.builtin.TypeInfo.Pointer.Size, comptime child: type) type { +fn CopyPtrAttrs( + comptime source: type, + comptime size: std.builtin.TypeInfo.Pointer.Size, + comptime child: type, +) type { const info = @typeInfo(source).Pointer; return @Type(.{ .Pointer = .{ diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 72a073fbfb..03825e2140 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -190,12 +190,21 @@ test "std.meta.Elem" { /// Types which cannot possibly have a sentinel will be a compile error. pub fn sentinel(comptime T: type) ?Elem(T) { switch (@typeInfo(T)) { - .Array => |info| return info.sentinel, + .Array => |info| { + const sentinel_ptr = info.sentinel orelse return null; + return @ptrCast(*const info.child, sentinel_ptr).*; + }, .Pointer => |info| { switch (info.size) { - .Many, .Slice => return info.sentinel, + .Many, .Slice => { + const sentinel_ptr = info.sentinel orelse return null; + return @ptrCast(*const info.child, sentinel_ptr).*; + }, .One => switch (@typeInfo(info.child)) { - .Array => |array_info| return array_info.sentinel, + .Array => |array_info| { + const sentinel_ptr = array_info.sentinel orelse return null; + return @ptrCast(*const array_info.child, sentinel_ptr).*; + }, else => {}, }, else => {}, @@ -239,7 +248,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .Array = .{ .len = array_info.len, .child = array_info.child, - .sentinel = sentinel_val, + .sentinel = &sentinel_val, }, }), .is_allowzero = info.is_allowzero, @@ -257,7 +266,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 = sentinel_val, + .sentinel = &sentinel_val, }, }), else => {}, @@ -275,7 +284,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 = sentinel_val, + .sentinel = &sentinel_val, }, }), }, -- cgit v1.2.3 From 449554a7307731047fcd9c132386fdf405c3b237 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Feb 2022 18:25:28 -0700 Subject: stage2: remove anytype fields from the language closes #10705 --- lib/std/crypto/benchmark.zig | 4 ++-- lib/std/json.zig | 3 ++- lib/std/zig/Ast.zig | 5 ----- lib/std/zig/parse.zig | 15 ++------------- lib/std/zig/render.zig | 2 -- src/AstGen.zig | 20 ++------------------ src/stage1/ir.cpp | 4 ++++ test/behavior/struct_llvm.zig | 15 --------------- test/behavior/type_info.zig | 7 ------- test/behavior/union_stage1.zig | 5 ----- test/compile_errors.zig | 10 ---------- 11 files changed, 12 insertions(+), 78 deletions(-) (limited to 'lib') diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig index 9fd3c42544..50a031af84 100644 --- a/lib/std/crypto/benchmark.zig +++ b/lib/std/crypto/benchmark.zig @@ -297,8 +297,8 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 { } const CryptoPwhash = struct { - hashFn: anytype, - params: anytype, + hashFn: @compileError("anytype fields are removed from the language"), + params: @compileError("anytype fields are removed from the language"), name: []const u8, }; const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 }; diff --git a/lib/std/json.zig b/lib/std/json.zig index 4009bf9c7f..ec3544364f 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1791,8 +1791,9 @@ fn parseInternal( } inline for (structInfo.fields) |field, i| { if (!fields_seen[i]) { - if (field.default_value) |default| { + if (field.default_value) |default_ptr| { if (!field.is_comptime) { + const default = @ptrCast(*const field.field_type, default_ptr).*; @field(r, field.name) = default; } } else { diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig index f7a18d2c04..17da4f5315 100644 --- a/lib/std/zig/Ast.zig +++ b/lib/std/zig/Ast.zig @@ -366,7 +366,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex { .builtin_call, .builtin_call_comma, .error_set_decl, - .@"anytype", .@"comptime", .@"nosuspend", .asm_simple, @@ -729,7 +728,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex { .error_value, => return datas[n].rhs + end_offset, - .@"anytype", .anyframe_literal, .char_literal, .integer_literal, @@ -2935,9 +2933,6 @@ pub const Node = struct { /// main_token is the field name identifier. /// lastToken() does not include the possible trailing comma. container_field, - /// `anytype`. both lhs and rhs unused. - /// Used by `ContainerField`. - @"anytype", /// `comptime lhs`. rhs unused. @"comptime", /// `nosuspend lhs`. rhs unused. diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 28219fa085..a70d0309e3 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -786,19 +786,8 @@ const Parser = struct { var align_expr: Node.Index = 0; var type_expr: Node.Index = 0; if (p.eatToken(.colon)) |_| { - if (p.eatToken(.keyword_anytype)) |anytype_tok| { - type_expr = try p.addNode(.{ - .tag = .@"anytype", - .main_token = anytype_tok, - .data = .{ - .lhs = undefined, - .rhs = undefined, - }, - }); - } else { - type_expr = try p.expectTypeExpr(); - align_expr = try p.parseByteAlign(); - } + type_expr = try p.expectTypeExpr(); + align_expr = try p.parseByteAlign(); } const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index de33f380f8..f17ee1e097 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -229,8 +229,6 @@ fn renderExpression(gpa: Allocator, ais: *Ais, tree: Ast, node: Ast.Node.Index, return renderToken(ais, tree, main_tokens[node] + 2, space); }, - .@"anytype" => return renderToken(ais, tree, main_tokens[node], space), - .block_two, .block_two_semicolon, => { diff --git a/src/AstGen.zig b/src/AstGen.zig index 4fddc0a3e2..59878f940c 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -468,7 +468,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins .for_simple, .@"suspend", .@"continue", - .@"anytype", .fn_proto_simple, .fn_proto_multi, .fn_proto_one, @@ -558,8 +557,6 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr .asm_output => unreachable, // Handled in `asmExpr`. .asm_input => unreachable, // Handled in `asmExpr`. - .@"anytype" => unreachable, // Handled in `containerDecl`. - .assign => { try assign(gz, scope, node); return rvalue(gz, rl, .void_value, node); @@ -3826,7 +3823,6 @@ fn structDeclInner( const astgen = gz.astgen; const gpa = astgen.gpa; const tree = astgen.tree; - const node_tags = tree.nodes.items(.tag); var namespace: Scope.Namespace = .{ .parent = scope, @@ -3875,10 +3871,7 @@ fn structDeclInner( return astgen.failTok(member.ast.name_token, "struct field missing type", .{}); } - const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype") - .none - else - try typeExpr(&block_scope, &namespace.base, member.ast.type_expr); + const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr); wip_members.appendToField(@enumToInt(field_type)); const doc_comment_index = try astgen.docCommentAsString(member.firstToken()); @@ -3951,8 +3944,6 @@ fn unionDeclInner( const astgen = gz.astgen; const gpa = astgen.gpa; - const tree = astgen.tree; - const node_tags = tree.nodes.items(.tag); var namespace: Scope.Namespace = .{ .parent = scope, @@ -4013,10 +4004,7 @@ fn unionDeclInner( wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused }); if (have_type) { - const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype") - .none - else - try typeExpr(&block_scope, &namespace.base, member.ast.type_expr); + const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr); wip_members.appendToField(@enumToInt(field_type)); } else if (arg_inst == .none and !have_auto_enum) { return astgen.failNode(member_node, "union field missing type", .{}); @@ -7791,7 +7779,6 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index, have_ .ptr_type, .ptr_type_bit_range, .@"suspend", - .@"anytype", .fn_proto_simple, .fn_proto_multi, .fn_proto_one, @@ -8052,7 +8039,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev .ptr_type, .ptr_type_bit_range, .@"suspend", - .@"anytype", .fn_proto_simple, .fn_proto_multi, .fn_proto_one, @@ -8232,7 +8218,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In .@"resume", .array_type, .@"suspend", - .@"anytype", .fn_decl, .anyframe_literal, .integer_literal, @@ -8474,7 +8459,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool { .@"resume", .array_type, .@"suspend", - .@"anytype", .fn_decl, .anyframe_literal, .integer_literal, diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 89f7de9d4d..5694db22ee 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -19048,6 +19048,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_ return ira->codegen->invalid_inst_gen->value->type; } + if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown))) { + return ira->codegen->invalid_inst_gen->value->type; + } + ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type, is_const, diff --git a/test/behavior/struct_llvm.zig b/test/behavior/struct_llvm.zig index 5aaefc8b38..eef7b16487 100644 --- a/test/behavior/struct_llvm.zig +++ b/test/behavior/struct_llvm.zig @@ -618,21 +618,6 @@ test "anonymous struct literal assigned to variable" { try expect(vec.@"2" == 99); } -test "struct with var field" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - const Point = struct { - x: anytype, - y: anytype, - }; - const pt = Point{ - .x = 1, - .y = 2, - }; - try expect(pt.x == 1); - try expect(pt.y == 2); -} - test "comptime struct field" { if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index ad300dac4e..14adc4dad5 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -436,13 +436,6 @@ test "@typeInfo does not force declarations into existence" { comptime try expect(@typeInfo(S).Struct.fields.len == 1); } -test "default value for a anytype field" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - const S = struct { x: anytype }; - try expect(@typeInfo(S).Struct.fields[0].default_value == null); -} - fn add(a: i32, b: i32) i32 { return a + b; } diff --git a/test/behavior/union_stage1.zig b/test/behavior/union_stage1.zig index 2eefa46705..b71a7ac25d 100644 --- a/test/behavior/union_stage1.zig +++ b/test/behavior/union_stage1.zig @@ -419,8 +419,3 @@ test "union enum type gets a separate scope" { try S.doTheTest(); } - -test "anytype union field: issue #9233" { - const Quux = union(enum) { bar: anytype }; - _ = Quux; -} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 88a596a880..252ec1496b 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -4207,16 +4207,6 @@ pub fn addCases(ctx: *TestContext) !void { "tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'", }); - ctx.objErrStage1("var makes structs required to be comptime known", - \\export fn entry() void { - \\ const S = struct{v: anytype}; - \\ var s = S{.v=@as(i32, 10)}; - \\ _ = s; - \\} - , &[_][]const u8{ - "tmp.zig:3:4: error: variable of type 'S' must be const or comptime", - }); - ctx.objErrStage1("@ptrCast discards const qualifier", \\export fn entry() void { \\ const x: i32 = 1234; -- cgit v1.2.3