From be1507a7afe4c8869abdbab67a32ede6afe3d938 Mon Sep 17 00:00:00 2001 From: Vexu Date: Sat, 11 Jul 2020 22:04:38 +0300 Subject: [PATCH] update compile error tests and some doc comments --- doc/docgen.zig | 11 +++++---- lib/std/fmt.zig | 2 +- lib/std/io/serialization.zig | 48 +++++++++++++++++++----------------- lib/std/log.zig | 2 +- lib/std/zig/ast.zig | 4 +-- src-self-hosted/Module.zig | 2 +- src/analyze.cpp | 4 +-- test/compile_errors.zig | 34 ++++++++++++------------- 8 files changed, 56 insertions(+), 51 deletions(-) diff --git a/doc/docgen.zig b/doc/docgen.zig index e2acfae768..af4d2530d0 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -212,7 +212,7 @@ const Tokenizer = struct { } }; -fn parseError(tokenizer: *Tokenizer, token: Token, comptime fmt: []const u8, args: var) anyerror { +fn parseError(tokenizer: *Tokenizer, token: Token, comptime fmt: []const u8, args: anytype) anyerror { const loc = tokenizer.getTokenLocation(token); const args_prefix = .{ tokenizer.source_file_name, loc.line + 1, loc.column + 1 }; warn("{}:{}:{}: error: " ++ fmt ++ "\n", args_prefix ++ args); @@ -634,7 +634,7 @@ fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 { return buf.toOwnedSlice(); } -fn writeEscaped(out: var, input: []const u8) !void { +fn writeEscaped(out: anytype, input: []const u8) !void { for (input) |c| { try switch (c) { '&' => out.writeAll("&"), @@ -765,7 +765,7 @@ fn isType(name: []const u8) bool { return false; } -fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Token, raw_src: []const u8) !void { +fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token, raw_src: []const u8) !void { const src = mem.trim(u8, raw_src, " \n"); try out.writeAll(""); var tokenizer = std.zig.Tokenizer.init(src); @@ -825,6 +825,7 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok .Keyword_volatile, .Keyword_allowzero, .Keyword_while, + .Keyword_anytype, => { try out.writeAll(""); try writeEscaped(out, src[token.loc.start..token.loc.end]); @@ -977,12 +978,12 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok try out.writeAll(""); } -fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: var, source_token: Token) !void { +fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token) !void { const raw_src = docgen_tokenizer.buffer[source_token.start..source_token.end]; return tokenizeAndPrintRaw(docgen_tokenizer, out, source_token, raw_src); } -fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void { +fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: anytype, zig_exe: []const u8) !void { var code_progress_index: usize = 0; var env_map = try process.getEnvMap(allocator); diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 7415b1b520..7e288170af 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -69,7 +69,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool { /// /// If a formatted user type contains a function of the type /// ``` -/// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: var) !void +/// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void /// ``` /// with `?` being the type formatted, this function will be called instead of the default implementation. /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. diff --git a/lib/std/io/serialization.zig b/lib/std/io/serialization.zig index 8fe0782c84..317dde6417 100644 --- a/lib/std/io/serialization.zig +++ b/lib/std/io/serialization.zig @@ -16,14 +16,16 @@ pub const Packing = enum { }; /// Creates a deserializer that deserializes types from any stream. -/// If `is_packed` is true, the data stream is treated as bit-packed, -/// otherwise data is expected to be packed to the smallest byte. -/// Types may implement a custom deserialization routine with a -/// function named `deserialize` in the form of: -/// pub fn deserialize(self: *Self, deserializer: var) !void -/// which will be called when the deserializer is used to deserialize -/// that type. It will pass a pointer to the type instance to deserialize -/// into and a pointer to the deserializer struct. +/// If `is_packed` is true, the data stream is treated as bit-packed, +/// otherwise data is expected to be packed to the smallest byte. +/// Types may implement a custom deserialization routine with a +/// function named `deserialize` in the form of: +/// ``` +/// pub fn deserialize(self: *Self, deserializer: anytype) !void +/// ``` +/// which will be called when the deserializer is used to deserialize +/// that type. It will pass a pointer to the type instance to deserialize +/// into and a pointer to the deserializer struct. pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, comptime ReaderType: type) type { return struct { in_stream: if (packing == .Bit) io.BitReader(endian, ReaderType) else ReaderType, @@ -108,7 +110,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, const C = comptime meta.Child(T); const child_type_id = @typeInfo(C); - //custom deserializer: fn(self: *Self, deserializer: var) !void + //custom deserializer: fn(self: *Self, deserializer: anytype) !void if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self); if (comptime trait.isPacked(C) and packing != .Bit) { @@ -196,18 +198,20 @@ pub fn deserializer( } /// Creates a serializer that serializes types to any stream. -/// If `is_packed` is true, the data will be bit-packed into the stream. -/// Note that the you must call `serializer.flush()` when you are done -/// writing bit-packed data in order ensure any unwritten bits are committed. -/// If `is_packed` is false, data is packed to the smallest byte. In the case -/// of packed structs, the struct will written bit-packed and with the specified -/// endianess, after which data will resume being written at the next byte boundary. -/// Types may implement a custom serialization routine with a -/// function named `serialize` in the form of: -/// pub fn serialize(self: Self, serializer: var) !void -/// which will be called when the serializer is used to serialize that type. It will -/// pass a const pointer to the type instance to be serialized and a pointer -/// to the serializer struct. +/// If `is_packed` is true, the data will be bit-packed into the stream. +/// Note that the you must call `serializer.flush()` when you are done +/// writing bit-packed data in order ensure any unwritten bits are committed. +/// If `is_packed` is false, data is packed to the smallest byte. In the case +/// of packed structs, the struct will written bit-packed and with the specified +/// endianess, after which data will resume being written at the next byte boundary. +/// Types may implement a custom serialization routine with a +/// function named `serialize` in the form of: +/// ``` +/// pub fn serialize(self: Self, serializer: anytype) !void +/// ``` +/// which will be called when the serializer is used to serialize that type. It will +/// pass a const pointer to the type instance to be serialized and a pointer +/// to the serializer struct. pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, comptime OutStreamType: type) type { return struct { out_stream: if (packing == .Bit) io.BitOutStream(endian, OutStreamType) else OutStreamType, @@ -270,7 +274,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co return; } - //custom serializer: fn(self: Self, serializer: var) !void + //custom serializer: fn(self: Self, serializer: anytype) !void if (comptime trait.hasFn("serialize")(T)) return T.serialize(value, self); if (comptime trait.isPacked(T) and packing != .Bit) { diff --git a/lib/std/log.zig b/lib/std/log.zig index 0006580031..3fb75b7e37 100644 --- a/lib/std/log.zig +++ b/lib/std/log.zig @@ -22,7 +22,7 @@ const root = @import("root"); //! comptime level: std.log.Level, //! comptime scope: @TypeOf(.EnumLiteral), //! comptime format: []const u8, -//! args: var, +//! args: anytype, //! ) void { //! // Ignore all non-critical logging from sources other than //! // .my_project and .nice_library diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 7cb4936444..0502f5bb64 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -1052,12 +1052,12 @@ pub const Node = struct { const params_len: usize = if (self.params_len == 0) 0 else switch (self.paramsConst()[self.params_len - 1].param_type) { - .var_type, .type_expr => self.params_len, + .any_type, .type_expr => self.params_len, .var_args => self.params_len - 1, }; if (i < params_len) { switch (self.paramsConst()[i].param_type) { - .var_type => |n| return n, + .any_type => |n| return n, .var_args => unreachable, .type_expr => |n| return n, } diff --git a/src-self-hosted/Module.zig b/src-self-hosted/Module.zig index b0678ea665..d173bd36e8 100644 --- a/src-self-hosted/Module.zig +++ b/src-self-hosted/Module.zig @@ -1132,7 +1132,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { const param_types = try fn_type_scope.arena.alloc(*zir.Inst, param_decls.len); for (param_decls) |param_decl, i| { const param_type_node = switch (param_decl.param_type) { - .var_type => |node| return self.failNode(&fn_type_scope.base, node, "TODO implement anytype parameter", .{}), + .any_type => |node| return self.failNode(&fn_type_scope.base, node, "TODO implement anytype parameter", .{}), .var_args => |tok| return self.failTok(&fn_type_scope.base, tok, "TODO implement var args", .{}), .type_expr => |node| node, }; diff --git a/src/analyze.cpp b/src/analyze.cpp index 5eba515e68..67c900507d 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1511,13 +1511,13 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) { } for (; i < fn_type_id->param_count; i += 1) { const char *comma_str = (i == 0) ? "" : ","; - buf_appendf(&fn_type->name, "%svar", comma_str); + buf_appendf(&fn_type->name, "%sanytype", comma_str); } buf_append_str(&fn_type->name, ")"); if (fn_type_id->cc != CallingConventionUnspecified) { buf_appendf(&fn_type->name, " callconv(.%s)", calling_convention_name(fn_type_id->cc)); } - buf_append_str(&fn_type->name, " var"); + buf_append_str(&fn_type->name, " anytype"); fn_type->data.fn.fn_type_id = *fn_type_id; fn_type->data.fn.is_generic = true; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index df2e759bf1..fb4428fbc6 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -42,7 +42,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\fn foo() Foo { \\ return .{ .x = 42 }; \\} - \\fn bar(val: var) Foo { + \\fn bar(val: anytype) Foo { \\ return .{ .x = val }; \\} \\export fn entry() void { @@ -1034,7 +1034,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ storev(&v[i], 42); \\} \\ - \\fn storev(ptr: var, val: i32) void { + \\fn storev(ptr: anytype, val: i32) void { \\ ptr.* = val; \\} , &[_][]const u8{ @@ -1049,7 +1049,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var x = loadv(&v[i]); \\} \\ - \\fn loadv(ptr: var) i32 { + \\fn loadv(ptr: anytype) i32 { \\ return ptr.*; \\} , &[_][]const u8{ @@ -1832,7 +1832,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ while (true) {} \\} , &[_][]const u8{ - "error: expected type 'fn([]const u8, ?*std.builtin.StackTrace) noreturn', found 'fn([]const u8,var) var'", + "error: expected type 'fn([]const u8, ?*std.builtin.StackTrace) noreturn', found 'fn([]const u8,anytype) anytype'", "note: only one of the functions is generic", }); @@ -2032,11 +2032,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("export generic function", - \\export fn foo(num: var) i32 { + \\export fn foo(num: anytype) i32 { \\ return 0; \\} , &[_][]const u8{ - "tmp.zig:1:15: error: parameter of type 'var' not allowed in function with calling convention 'C'", + "tmp.zig:1:15: error: parameter of type 'anytype' not allowed in function with calling convention 'C'", }); cases.add("C pointer to c_void", @@ -2836,7 +2836,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("missing parameter name of generic function", - \\fn dump(var) void {} + \\fn dump(anytype) void {} \\export fn entry() void { \\ var a: u8 = 9; \\ dump(a); @@ -2859,13 +2859,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("generic fn as parameter without comptime keyword", - \\fn f(_: fn (var) void) void {} - \\fn g(_: var) void {} + \\fn f(_: fn (anytype) void) void {} + \\fn g(_: anytype) void {} \\export fn entry() void { \\ f(g); \\} , &[_][]const u8{ - "tmp.zig:1:9: error: parameter of type 'fn(var) var' must be declared comptime", + "tmp.zig:1:9: error: parameter of type 'fn(anytype) anytype' must be declared comptime", }); cases.add("optional pointer to void in extern struct", @@ -3165,7 +3165,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add("var makes structs required to be comptime known", \\export fn entry() void { - \\ const S = struct{v: var}; + \\ const S = struct{v: anytype}; \\ var s = S{.v=@as(i32, 10)}; \\} , &[_][]const u8{ @@ -6072,10 +6072,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("calling a generic function only known at runtime", - \\var foos = [_]fn(var) void { foo1, foo2 }; + \\var foos = [_]fn(anytype) void { foo1, foo2 }; \\ - \\fn foo1(arg: var) void {} - \\fn foo2(arg: var) void {} + \\fn foo1(arg: anytype) void {} + \\fn foo2(arg: anytype) void {} \\ \\pub fn main() !void { \\ foos[0](true); @@ -6920,12 +6920,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("getting return type of generic function", - \\fn generic(a: var) void {} + \\fn generic(a: anytype) void {} \\comptime { \\ _ = @TypeOf(generic).ReturnType; \\} , &[_][]const u8{ - "tmp.zig:3:25: error: ReturnType has not been resolved because 'fn(var) var' is generic", + "tmp.zig:3:25: error: ReturnType has not been resolved because 'fn(anytype) anytype' is generic", }); cases.add("unsupported modifier at start of asm output constraint", @@ -7493,7 +7493,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add("issue #5221: invalid struct init type referenced by @typeInfo and passed into function", - \\fn ignore(comptime param: var) void {} + \\fn ignore(comptime param: anytype) void {} \\ \\export fn foo() void { \\ const MyStruct = struct {