stage2: remove anytype fields from the language

closes #10705
This commit is contained in:
Andrew Kelley 2022-02-01 18:25:28 -07:00
parent f4a249325e
commit 449554a730
11 changed files with 12 additions and 78 deletions

View file

@ -297,8 +297,8 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
} }
const CryptoPwhash = struct { const CryptoPwhash = struct {
hashFn: anytype, hashFn: @compileError("anytype fields are removed from the language"),
params: anytype, params: @compileError("anytype fields are removed from the language"),
name: []const u8, name: []const u8,
}; };
const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 }; const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };

View file

@ -1791,8 +1791,9 @@ fn parseInternal(
} }
inline for (structInfo.fields) |field, i| { inline for (structInfo.fields) |field, i| {
if (!fields_seen[i]) { if (!fields_seen[i]) {
if (field.default_value) |default| { if (field.default_value) |default_ptr| {
if (!field.is_comptime) { if (!field.is_comptime) {
const default = @ptrCast(*const field.field_type, default_ptr).*;
@field(r, field.name) = default; @field(r, field.name) = default;
} }
} else { } else {

View file

@ -366,7 +366,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
.builtin_call, .builtin_call,
.builtin_call_comma, .builtin_call_comma,
.error_set_decl, .error_set_decl,
.@"anytype",
.@"comptime", .@"comptime",
.@"nosuspend", .@"nosuspend",
.asm_simple, .asm_simple,
@ -729,7 +728,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
.error_value, .error_value,
=> return datas[n].rhs + end_offset, => return datas[n].rhs + end_offset,
.@"anytype",
.anyframe_literal, .anyframe_literal,
.char_literal, .char_literal,
.integer_literal, .integer_literal,
@ -2935,9 +2933,6 @@ pub const Node = struct {
/// main_token is the field name identifier. /// main_token is the field name identifier.
/// lastToken() does not include the possible trailing comma. /// lastToken() does not include the possible trailing comma.
container_field, container_field,
/// `anytype`. both lhs and rhs unused.
/// Used by `ContainerField`.
@"anytype",
/// `comptime lhs`. rhs unused. /// `comptime lhs`. rhs unused.
@"comptime", @"comptime",
/// `nosuspend lhs`. rhs unused. /// `nosuspend lhs`. rhs unused.

View file

@ -786,19 +786,8 @@ const Parser = struct {
var align_expr: Node.Index = 0; var align_expr: Node.Index = 0;
var type_expr: Node.Index = 0; var type_expr: Node.Index = 0;
if (p.eatToken(.colon)) |_| { if (p.eatToken(.colon)) |_| {
if (p.eatToken(.keyword_anytype)) |anytype_tok| { type_expr = try p.expectTypeExpr();
type_expr = try p.addNode(.{ align_expr = try p.parseByteAlign();
.tag = .@"anytype",
.main_token = anytype_tok,
.data = .{
.lhs = undefined,
.rhs = undefined,
},
});
} else {
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(); const value_expr: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();

View file

@ -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); return renderToken(ais, tree, main_tokens[node] + 2, space);
}, },
.@"anytype" => return renderToken(ais, tree, main_tokens[node], space),
.block_two, .block_two,
.block_two_semicolon, .block_two_semicolon,
=> { => {

View file

@ -468,7 +468,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins
.for_simple, .for_simple,
.@"suspend", .@"suspend",
.@"continue", .@"continue",
.@"anytype",
.fn_proto_simple, .fn_proto_simple,
.fn_proto_multi, .fn_proto_multi,
.fn_proto_one, .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_output => unreachable, // Handled in `asmExpr`.
.asm_input => unreachable, // Handled in `asmExpr`. .asm_input => unreachable, // Handled in `asmExpr`.
.@"anytype" => unreachable, // Handled in `containerDecl`.
.assign => { .assign => {
try assign(gz, scope, node); try assign(gz, scope, node);
return rvalue(gz, rl, .void_value, node); return rvalue(gz, rl, .void_value, node);
@ -3826,7 +3823,6 @@ fn structDeclInner(
const astgen = gz.astgen; const astgen = gz.astgen;
const gpa = astgen.gpa; const gpa = astgen.gpa;
const tree = astgen.tree; const tree = astgen.tree;
const node_tags = tree.nodes.items(.tag);
var namespace: Scope.Namespace = .{ var namespace: Scope.Namespace = .{
.parent = scope, .parent = scope,
@ -3875,10 +3871,7 @@ fn structDeclInner(
return astgen.failTok(member.ast.name_token, "struct field missing type", .{}); 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") const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
.none
else
try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
wip_members.appendToField(@enumToInt(field_type)); wip_members.appendToField(@enumToInt(field_type));
const doc_comment_index = try astgen.docCommentAsString(member.firstToken()); const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
@ -3951,8 +3944,6 @@ fn unionDeclInner(
const astgen = gz.astgen; const astgen = gz.astgen;
const gpa = astgen.gpa; const gpa = astgen.gpa;
const tree = astgen.tree;
const node_tags = tree.nodes.items(.tag);
var namespace: Scope.Namespace = .{ var namespace: Scope.Namespace = .{
.parent = scope, .parent = scope,
@ -4013,10 +4004,7 @@ fn unionDeclInner(
wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused }); wip_members.nextField(bits_per_field, .{ have_type, have_align, have_value, unused });
if (have_type) { if (have_type) {
const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype") const field_type = try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
.none
else
try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
wip_members.appendToField(@enumToInt(field_type)); wip_members.appendToField(@enumToInt(field_type));
} else if (arg_inst == .none and !have_auto_enum) { } else if (arg_inst == .none and !have_auto_enum) {
return astgen.failNode(member_node, "union field missing type", .{}); 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,
.ptr_type_bit_range, .ptr_type_bit_range,
.@"suspend", .@"suspend",
.@"anytype",
.fn_proto_simple, .fn_proto_simple,
.fn_proto_multi, .fn_proto_multi,
.fn_proto_one, .fn_proto_one,
@ -8052,7 +8039,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev
.ptr_type, .ptr_type,
.ptr_type_bit_range, .ptr_type_bit_range,
.@"suspend", .@"suspend",
.@"anytype",
.fn_proto_simple, .fn_proto_simple,
.fn_proto_multi, .fn_proto_multi,
.fn_proto_one, .fn_proto_one,
@ -8232,7 +8218,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
.@"resume", .@"resume",
.array_type, .array_type,
.@"suspend", .@"suspend",
.@"anytype",
.fn_decl, .fn_decl,
.anyframe_literal, .anyframe_literal,
.integer_literal, .integer_literal,
@ -8474,7 +8459,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
.@"resume", .@"resume",
.array_type, .array_type,
.@"suspend", .@"suspend",
.@"anytype",
.fn_decl, .fn_decl,
.anyframe_literal, .anyframe_literal,
.integer_literal, .integer_literal,

View file

@ -19048,6 +19048,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
return ira->codegen->invalid_inst_gen->value->type; 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, ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen,
elem_type, elem_type,
is_const, is_const,

View file

@ -618,21 +618,6 @@ test "anonymous struct literal assigned to variable" {
try expect(vec.@"2" == 99); 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" { test "comptime struct field" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO

View file

@ -436,13 +436,6 @@ test "@typeInfo does not force declarations into existence" {
comptime try expect(@typeInfo(S).Struct.fields.len == 1); 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 { fn add(a: i32, b: i32) i32 {
return a + b; return a + b;
} }

View file

@ -419,8 +419,3 @@ test "union enum type gets a separate scope" {
try S.doTheTest(); try S.doTheTest();
} }
test "anytype union field: issue #9233" {
const Quux = union(enum) { bar: anytype };
_ = Quux;
}

View file

@ -4207,16 +4207,6 @@ pub fn addCases(ctx: *TestContext) !void {
"tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'", "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", ctx.objErrStage1("@ptrCast discards const qualifier",
\\export fn entry() void { \\export fn entry() void {
\\ const x: i32 = 1234; \\ const x: i32 = 1234;