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 {...@@ -297,8 +297,8 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
297}297}
298298
299const CryptoPwhash = struct {299const CryptoPwhash = struct {
300 hashFn: anytype,300 hashFn: @compileError("anytype fields are removed from the language"),
301 params: anytype,301 params: @compileError("anytype fields are removed from the language"),
302 name: []const u8,302 name: []const u8,
303};303};
304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
lib/std/json.zig+2-1
...@@ -1791,8 +1791,9 @@ fn parseInternal(...@@ -1791,8 +1791,9 @@ fn parseInternal(
1791 }1791 }
1792 inline for (structInfo.fields) |field, i| {1792 inline for (structInfo.fields) |field, i| {
1793 if (!fields_seen[i]) {1793 if (!fields_seen[i]) {
1794 if (field.default_value) |default| {1794 if (field.default_value) |default_ptr| {
1795 if (!field.is_comptime) {1795 if (!field.is_comptime) {
1796 const default = @ptrCast(*const field.field_type, default_ptr).*;
1796 @field(r, field.name) = default;1797 @field(r, field.name) = default;
1797 }1798 }
1798 } else {1799 } else {
lib/std/zig/Ast.zig-5
...@@ -366,7 +366,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -366,7 +366,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
366 .builtin_call,366 .builtin_call,
367 .builtin_call_comma,367 .builtin_call_comma,
368 .error_set_decl,368 .error_set_decl,
369 .@"anytype",
370 .@"comptime",369 .@"comptime",
371 .@"nosuspend",370 .@"nosuspend",
372 .asm_simple,371 .asm_simple,
...@@ -729,7 +728,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -729,7 +728,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
729 .error_value,728 .error_value,
730 => return datas[n].rhs + end_offset,729 => return datas[n].rhs + end_offset,
731730
732 .@"anytype",
733 .anyframe_literal,731 .anyframe_literal,
734 .char_literal,732 .char_literal,
735 .integer_literal,733 .integer_literal,
...@@ -2935,9 +2933,6 @@ pub const Node = struct {...@@ -2935,9 +2933,6 @@ pub const Node = struct {
2935 /// main_token is the field name identifier.2933 /// main_token is the field name identifier.
2936 /// lastToken() does not include the possible trailing comma.2934 /// lastToken() does not include the possible trailing comma.
2937 container_field,2935 container_field,
2938 /// `anytype`. both lhs and rhs unused.
2939 /// Used by `ContainerField`.
2940 @"anytype",
2941 /// `comptime lhs`. rhs unused.2936 /// `comptime lhs`. rhs unused.
2942 @"comptime",2937 @"comptime",
2943 /// `nosuspend lhs`. rhs unused.2938 /// `nosuspend lhs`. rhs unused.
lib/std/zig/parse.zig+2-13
...@@ -786,19 +786,8 @@ const Parser = struct {...@@ -786,19 +786,8 @@ const Parser = struct {
786 var align_expr: Node.Index = 0;786 var align_expr: Node.Index = 0;
787 var type_expr: Node.Index = 0;787 var type_expr: Node.Index = 0;
788 if (p.eatToken(.colon)) |_| {788 if (p.eatToken(.colon)) |_| {
789 if (p.eatToken(.keyword_anytype)) |anytype_tok| {789 type_expr = try p.expectTypeExpr();
790 type_expr = try p.addNode(.{790 align_expr = try p.parseByteAlign();
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 }
802 }791 }
803792
804 const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();793 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,...@@ -229,8 +229,6 @@ fn renderExpression(gpa: Allocator, ais: *Ais, tree: Ast, node: Ast.Node.Index,
229 return renderToken(ais, tree, main_tokens[node] + 2, space);229 return renderToken(ais, tree, main_tokens[node] + 2, space);
230 },230 },
231231
232 .@"anytype" => return renderToken(ais, tree, main_tokens[node], space),
233
234 .block_two,232 .block_two,
235 .block_two_semicolon,233 .block_two_semicolon,
236 => {234 => {
src/AstGen.zig+2-18
...@@ -468,7 +468,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins...@@ -468,7 +468,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins
468 .for_simple,468 .for_simple,
469 .@"suspend",469 .@"suspend",
470 .@"continue",470 .@"continue",
471 .@"anytype",
472 .fn_proto_simple,471 .fn_proto_simple,
473 .fn_proto_multi,472 .fn_proto_multi,
474 .fn_proto_one,473 .fn_proto_one,
...@@ -558,8 +557,6 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr...@@ -558,8 +557,6 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
558 .asm_output => unreachable, // Handled in `asmExpr`.557 .asm_output => unreachable, // Handled in `asmExpr`.
559 .asm_input => unreachable, // Handled in `asmExpr`.558 .asm_input => unreachable, // Handled in `asmExpr`.
560559
561 .@"anytype" => unreachable, // Handled in `containerDecl`.
562
563 .assign => {560 .assign => {
564 try assign(gz, scope, node);561 try assign(gz, scope, node);
565 return rvalue(gz, rl, .void_value, node);562 return rvalue(gz, rl, .void_value, node);
...@@ -3826,7 +3823,6 @@ fn structDeclInner(...@@ -3826,7 +3823,6 @@ fn structDeclInner(
3826 const astgen = gz.astgen;3823 const astgen = gz.astgen;
3827 const gpa = astgen.gpa;3824 const gpa = astgen.gpa;
3828 const tree = astgen.tree;3825 const tree = astgen.tree;
3829 const node_tags = tree.nodes.items(.tag);
38303826
3831 var namespace: Scope.Namespace = .{3827 var namespace: Scope.Namespace = .{
3832 .parent = scope,3828 .parent = scope,
...@@ -3875,10 +3871,7 @@ fn structDeclInner(...@@ -3875,10 +3871,7 @@ fn structDeclInner(
3875 return astgen.failTok(member.ast.name_token, "struct field missing type", .{});3871 return astgen.failTok(member.ast.name_token, "struct field missing type", .{});
3876 }3872 }
38773873
3878 const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")3874 const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
3879 .none
3880 else
3881 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
3882 wip_members.appendToField(@enumToInt(field_type));3875 wip_members.appendToField(@enumToInt(field_type));
38833876
3884 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());3877 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
...@@ -3951,8 +3944,6 @@ fn unionDeclInner(...@@ -3951,8 +3944,6 @@ fn unionDeclInner(
39513944
3952 const astgen = gz.astgen;3945 const astgen = gz.astgen;
3953 const gpa = astgen.gpa;3946 const gpa = astgen.gpa;
3954 const tree = astgen.tree;
3955 const node_tags = tree.nodes.items(.tag);
39563947
3957 var namespace: Scope.Namespace = .{3948 var namespace: Scope.Namespace = .{
3958 .parent = scope,3949 .parent = scope,
...@@ -4013,10 +4004,7 @@ fn unionDeclInner(...@@ -4013,10 +4004,7 @@ fn unionDeclInner(
4013 wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused });4004 wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused });
40144005
4015 if (have_type) {4006 if (have_type) {
4016 const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")4007 const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
4017 .none
4018 else
4019 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
4020 wip_members.appendToField(@enumToInt(field_type));4008 wip_members.appendToField(@enumToInt(field_type));
4021 } else if (arg_inst == .none and !have_auto_enum) {4009 } else if (arg_inst == .none and !have_auto_enum) {
4022 return astgen.failNode(member_node, "union field missing type", .{});4010 return astgen.failNode(member_node, "union field missing type", .{});
...@@ -7791,7 +7779,6 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index, have_...@@ -7791,7 +7779,6 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index, have_
7791 .ptr_type,7779 .ptr_type,
7792 .ptr_type_bit_range,7780 .ptr_type_bit_range,
7793 .@"suspend",7781 .@"suspend",
7794 .@"anytype",
7795 .fn_proto_simple,7782 .fn_proto_simple,
7796 .fn_proto_multi,7783 .fn_proto_multi,
7797 .fn_proto_one,7784 .fn_proto_one,
...@@ -8052,7 +8039,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev...@@ -8052,7 +8039,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev
8052 .ptr_type,8039 .ptr_type,
8053 .ptr_type_bit_range,8040 .ptr_type_bit_range,
8054 .@"suspend",8041 .@"suspend",
8055 .@"anytype",
8056 .fn_proto_simple,8042 .fn_proto_simple,
8057 .fn_proto_multi,8043 .fn_proto_multi,
8058 .fn_proto_one,8044 .fn_proto_one,
...@@ -8232,7 +8218,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In...@@ -8232,7 +8218,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
8232 .@"resume",8218 .@"resume",
8233 .array_type,8219 .array_type,
8234 .@"suspend",8220 .@"suspend",
8235 .@"anytype",
8236 .fn_decl,8221 .fn_decl,
8237 .anyframe_literal,8222 .anyframe_literal,
8238 .integer_literal,8223 .integer_literal,
...@@ -8474,7 +8459,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {...@@ -8474,7 +8459,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
8474 .@"resume",8459 .@"resume",
8475 .array_type,8460 .array_type,
8476 .@"suspend",8461 .@"suspend",
8477 .@"anytype",
8478 .fn_decl,8462 .fn_decl,
8479 .anyframe_literal,8463 .anyframe_literal,
8480 .integer_literal,8464 .integer_literal,
src/stage1/ir.cpp+4
...@@ -19048,6 +19048,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_...@@ -19048,6 +19048,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
19048 return ira->codegen->invalid_inst_gen->value->type;19048 return ira->codegen->invalid_inst_gen->value->type;
19049 }19049 }
1905019050
19051 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown))) {
19052 return ira->codegen->invalid_inst_gen->value->type;
19053 }
19054
19051 ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen,19055 ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen,
19052 elem_type,19056 elem_type,
19053 is_const,19057 is_const,
test/behavior/struct_llvm.zig-15
...@@ -618,21 +618,6 @@ test "anonymous struct literal assigned to variable" {...@@ -618,21 +618,6 @@ test "anonymous struct literal assigned to variable" {
618 try expect(vec.@"2" == 99);618 try expect(vec.@"2" == 99);
619}619}
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
636test "comptime struct field" {621test "comptime struct field" {
637 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO622 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" {...@@ -436,13 +436,6 @@ test "@typeInfo does not force declarations into existence" {
436 comptime try expect(@typeInfo(S).Struct.fields.len == 1);436 comptime try expect(@typeInfo(S).Struct.fields.len == 1);
437}437}
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
446fn add(a: i32, b: i32) i32 {439fn add(a: i32, b: i32) i32 {
447 return a + b;440 return a + b;
448}441}
test/behavior/union_stage1.zig-5
...@@ -419,8 +419,3 @@ test "union enum type gets a separate scope" {...@@ -419,8 +419,3 @@ test "union enum type gets a separate scope" {
419419
420 try S.doTheTest();420 try S.doTheTest();
421}421}
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 {...@@ -4207,16 +4207,6 @@ pub fn addCases(ctx: *TestContext) !void {
4207 "tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'",4207 "tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'",
4208 });4208 });
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
4220 ctx.objErrStage1("@ptrCast discards const qualifier",4210 ctx.objErrStage1("@ptrCast discards const qualifier",
4221 \\export fn entry() void {4211 \\export fn entry() void {
4222 \\ const x: i32 = 1234;4212 \\ const x: i32 = 1234;