authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-01 18:25:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-01 19:06:40-07:00
log449554a7307731047fcd9c132386fdf405c3b237
treec579c496ed162736b24930a17070c2544d4fc033
parentf4a249325e8e3741a6294462ae37a79cb9089c56

stage2: remove anytype fields from the language

closes #10705

11 files changed, 12 insertions(+), 78 deletions(-)

lib/std/crypto/benchmark.zig+2-2
......@@ -297,8 +297,8 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
297297}
298298
299299const CryptoPwhash = struct {
300 hashFn: anytype,
301 params: anytype,
300 hashFn: @compileError("anytype fields are removed from the language"),
301 params: @compileError("anytype fields are removed from the language"),
302302 name: []const u8,
303303};
304304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
lib/std/json.zig+2-1
......@@ -1791,8 +1791,9 @@ fn parseInternal(
17911791 }
17921792 inline for (structInfo.fields) |field, i| {
17931793 if (!fields_seen[i]) {
1794 if (field.default_value) |default| {
1794 if (field.default_value) |default_ptr| {
17951795 if (!field.is_comptime) {
1796 const default = @ptrCast(*const field.field_type, default_ptr).*;
17961797 @field(r, field.name) = default;
17971798 }
17981799 } else {
lib/std/zig/Ast.zig-5
......@@ -366,7 +366,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
366366 .builtin_call,
367367 .builtin_call_comma,
368368 .error_set_decl,
369 .@"anytype",
370369 .@"comptime",
371370 .@"nosuspend",
372371 .asm_simple,
......@@ -729,7 +728,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
729728 .error_value,
730729 => return datas[n].rhs + end_offset,
731730
732 .@"anytype",
733731 .anyframe_literal,
734732 .char_literal,
735733 .integer_literal,
......@@ -2935,9 +2933,6 @@ pub const Node = struct {
29352933 /// main_token is the field name identifier.
29362934 /// lastToken() does not include the possible trailing comma.
29372935 container_field,
2938 /// `anytype`. both lhs and rhs unused.
2939 /// Used by `ContainerField`.
2940 @"anytype",
29412936 /// `comptime lhs`. rhs unused.
29422937 @"comptime",
29432938 /// `nosuspend lhs`. rhs unused.
lib/std/zig/parse.zig+2-13
......@@ -786,19 +786,8 @@ const Parser = struct {
786786 var align_expr: Node.Index = 0;
787787 var type_expr: Node.Index = 0;
788788 if (p.eatToken(.colon)) |_| {
789 if (p.eatToken(.keyword_anytype)) |anytype_tok| {
790 type_expr = try p.addNode(.{
791 .tag = .@"anytype",
792 .main_token = anytype_tok,
793 .data = .{
794 .lhs = undefined,
795 .rhs = undefined,
796 },
797 });
798 } else {
799 type_expr = try p.expectTypeExpr();
800 align_expr = try p.parseByteAlign();
801 }
789 type_expr = try p.expectTypeExpr();
790 align_expr = try p.parseByteAlign();
802791 }
803792
804793 const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();
lib/std/zig/render.zig-2
......@@ -229,8 +229,6 @@ fn renderExpression(gpa: Allocator, ais: *Ais, tree: Ast, node: Ast.Node.Index,
229229 return renderToken(ais, tree, main_tokens[node] + 2, space);
230230 },
231231
232 .@"anytype" => return renderToken(ais, tree, main_tokens[node], space),
233
234232 .block_two,
235233 .block_two_semicolon,
236234 => {
src/AstGen.zig+2-18
......@@ -468,7 +468,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins
468468 .for_simple,
469469 .@"suspend",
470470 .@"continue",
471 .@"anytype",
472471 .fn_proto_simple,
473472 .fn_proto_multi,
474473 .fn_proto_one,
......@@ -558,8 +557,6 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
558557 .asm_output => unreachable, // Handled in `asmExpr`.
559558 .asm_input => unreachable, // Handled in `asmExpr`.
560559
561 .@"anytype" => unreachable, // Handled in `containerDecl`.
562
563560 .assign => {
564561 try assign(gz, scope, node);
565562 return rvalue(gz, rl, .void_value, node);
......@@ -3826,7 +3823,6 @@ fn structDeclInner(
38263823 const astgen = gz.astgen;
38273824 const gpa = astgen.gpa;
38283825 const tree = astgen.tree;
3829 const node_tags = tree.nodes.items(.tag);
38303826
38313827 var namespace: Scope.Namespace = .{
38323828 .parent = scope,
......@@ -3875,10 +3871,7 @@ fn structDeclInner(
38753871 return astgen.failTok(member.ast.name_token, "struct field missing type", .{});
38763872 }
38773873
3878 const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")
3879 .none
3880 else
3881 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
3874 const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
38823875 wip_members.appendToField(@enumToInt(field_type));
38833876
38843877 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
......@@ -3951,8 +3944,6 @@ fn unionDeclInner(
39513944
39523945 const astgen = gz.astgen;
39533946 const gpa = astgen.gpa;
3954 const tree = astgen.tree;
3955 const node_tags = tree.nodes.items(.tag);
39563947
39573948 var namespace: Scope.Namespace = .{
39583949 .parent = scope,
......@@ -4013,10 +4004,7 @@ fn unionDeclInner(
40134004 wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused });
40144005
40154006 if (have_type) {
4016 const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")
4017 .none
4018 else
4019 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
4007 const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
40204008 wip_members.appendToField(@enumToInt(field_type));
40214009 } else if (arg_inst == .none and !have_auto_enum) {
40224010 return astgen.failNode(member_node, "union field missing type", .{});
......@@ -7791,7 +7779,6 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index, have_
77917779 .ptr_type,
77927780 .ptr_type_bit_range,
77937781 .@"suspend",
7794 .@"anytype",
77957782 .fn_proto_simple,
77967783 .fn_proto_multi,
77977784 .fn_proto_one,
......@@ -8052,7 +8039,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev
80528039 .ptr_type,
80538040 .ptr_type_bit_range,
80548041 .@"suspend",
8055 .@"anytype",
80568042 .fn_proto_simple,
80578043 .fn_proto_multi,
80588044 .fn_proto_one,
......@@ -8232,7 +8218,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
82328218 .@"resume",
82338219 .array_type,
82348220 .@"suspend",
8235 .@"anytype",
82368221 .fn_decl,
82378222 .anyframe_literal,
82388223 .integer_literal,
......@@ -8474,7 +8459,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
84748459 .@"resume",
84758460 .array_type,
84768461 .@"suspend",
8477 .@"anytype",
84788462 .fn_decl,
84798463 .anyframe_literal,
84808464 .integer_literal,
src/stage1/ir.cpp+4
......@@ -19048,6 +19048,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
1904819048 return ira->codegen->invalid_inst_gen->value->type;
1904919049 }
1905019050
19051 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown))) {
19052 return ira->codegen->invalid_inst_gen->value->type;
19053 }
19054
1905119055 ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen,
1905219056 elem_type,
1905319057 is_const,
test/behavior/struct_llvm.zig-15
......@@ -618,21 +618,6 @@ test "anonymous struct literal assigned to variable" {
618618 try expect(vec.@"2" == 99);
619619}
620620
621test "struct with var field" {
622 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
623
624 const Point = struct {
625 x: anytype,
626 y: anytype,
627 };
628 const pt = Point{
629 .x = 1,
630 .y = 2,
631 };
632 try expect(pt.x == 1);
633 try expect(pt.y == 2);
634}
635
636621test "comptime struct field" {
637622 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
638623
test/behavior/type_info.zig-7
......@@ -436,13 +436,6 @@ test "@typeInfo does not force declarations into existence" {
436436 comptime try expect(@typeInfo(S).Struct.fields.len == 1);
437437}
438438
439test "default value for a anytype field" {
440 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
441
442 const S = struct { x: anytype };
443 try expect(@typeInfo(S).Struct.fields[0].default_value == null);
444}
445
446439fn add(a: i32, b: i32) i32 {
447440 return a + b;
448441}
test/behavior/union_stage1.zig-5
......@@ -419,8 +419,3 @@ test "union enum type gets a separate scope" {
419419
420420 try S.doTheTest();
421421}
422
423test "anytype union field: issue #9233" {
424 const Quux = union(enum) { bar: anytype };
425 _ = Quux;
426}
test/compile_errors.zig-10
......@@ -4207,16 +4207,6 @@ pub fn addCases(ctx: *TestContext) !void {
42074207 "tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'",
42084208 });
42094209
4210 ctx.objErrStage1("var makes structs required to be comptime known",
4211 \\export fn entry() void {
4212 \\ const S = struct{v: anytype};
4213 \\ var s = S{.v=@as(i32, 10)};
4214 \\ _ = s;
4215 \\}
4216 , &[_][]const u8{
4217 "tmp.zig:3:4: error: variable of type 'S' must be const or comptime",
4218 });
4219
42204210 ctx.objErrStage1("@ptrCast discards const qualifier",
42214211 \\export fn entry() void {
42224212 \\ const x: i32 = 1234;