Commit 30c9808391

Andrew Kelley <andrew@ziglang.org>
2021-04-21 02:38:06
AstGen: implement anytype parameters
1 parent a62db38
Changed files (3)
lib
std
src
lib/std/zig/ast.zig
@@ -2172,6 +2172,10 @@ pub const full = struct {
                         };
                         it.param_i += 1;
                         it.tok_i = it.tree.lastToken(param_type) + 1;
+                        // Look for anytype and ... params afterwards.
+                        if (token_tags[it.tok_i] == .comma) {
+                            it.tok_i += 1;
+                        }
                         it.tok_flag = true;
                         return Param{
                             .first_doc_comment = first_doc_comment,
@@ -2181,10 +2185,7 @@ pub const full = struct {
                             .type_expr = param_type,
                         };
                     }
-                    // Look for anytype and ... params afterwards.
-                    if (token_tags[it.tok_i] == .comma) {
-                        it.tok_i += 1;
-                    } else {
+                    if (token_tags[it.tok_i] == .r_paren) {
                         return null;
                     }
                     if (token_tags[it.tok_i] == .doc_comment) {
@@ -2236,8 +2237,8 @@ pub const full = struct {
                 .tree = &tree,
                 .fn_proto = &fn_proto,
                 .param_i = 0,
-                .tok_i = undefined,
-                .tok_flag = false,
+                .tok_i = fn_proto.lparen + 1,
+                .tok_flag = true,
             };
         }
     };
src/AstGen.zig
@@ -2334,7 +2334,11 @@ fn fnDecl(
         var count: usize = 0;
         var it = fn_proto.iterate(tree.*);
         while (it.next()) |param| {
-            if (param.anytype_ellipsis3) |some| if (token_tags[some] == .ellipsis3) break;
+            if (param.anytype_ellipsis3) |token| switch (token_tags[token]) {
+                .ellipsis3 => break,
+                .keyword_anytype => {},
+                else => unreachable,
+            };
             count += 1;
         }
         break :blk count;
@@ -2358,11 +2362,10 @@ fn fnDecl(
         while (it.next()) |param| : (param_type_i += 1) {
             if (param.anytype_ellipsis3) |token| {
                 switch (token_tags[token]) {
-                    .keyword_anytype => return astgen.failTok(
-                        token,
-                        "TODO implement anytype parameter",
-                        .{},
-                    ),
+                    .keyword_anytype => {
+                        param_types[param_type_i] = .none;
+                        continue;
+                    },
                     .ellipsis3 => {
                         is_var_args = true;
                         break;
src/Zir.zig
@@ -1816,6 +1816,7 @@ pub const Inst = struct {
 
     /// Trailing:
     /// 0. param_type: Ref // for each param_types_len
+    ///    - `none` indicates that the param type is `anytype`.
     /// 1. body: Index // for each body_len
     pub const Func = struct {
         return_type: Ref,
@@ -3304,7 +3305,10 @@ const Writer = struct {
 
         try stream.writeAll(", {\n");
         self.indent += 2;
+        const prev_param_count = self.param_count;
+        self.param_count = param_types.len;
         try self.writeBody(stream, body);
+        self.param_count = prev_param_count;
         self.indent -= 2;
         try stream.writeByteNTimes(' ', self.indent);
         try stream.writeAll("}) ");