Commit c8f60b2e2f

Noam Preil <pleasantatk@gmail.com>
2020-06-26 10:36:17
Stage2: handle missing function names
1 parent 6510888
Changed files (2)
src-self-hosted
test
src-self-hosted/Module.zig
@@ -1722,8 +1722,16 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
     for (decls) |src_decl, decl_i| {
         if (src_decl.cast(ast.Node.FnProto)) |fn_proto| {
             // We will create a Decl for it regardless of analysis status.
-            const name_tok = fn_proto.name_token orelse
-                @panic("TODO handle missing function name in the parser");
+            const name_tok = fn_proto.name_token orelse {
+                const err_msg = try ErrorMsg.create(self.allocator, tree.token_locs[fn_proto.firstToken()].end, "missing function name", .{});
+                // TODO: cache a single invalid decl in the Module?
+                const new_decl = try self.createNewDecl(&root_scope.base, "", decl_i, [1]u8{0} ** 16, [1]u8{0} ** 16);
+                root_scope.decls.appendAssumeCapacity(new_decl);
+                errdefer err_msg.destroy(self.allocator);
+                try self.failed_decls.putNoClobber(new_decl, err_msg);
+                continue;
+            };
+
             const name_loc = tree.token_locs[name_tok];
             const name = tree.tokenSliceLoc(name_loc);
             const name_hash = root_scope.fullyQualifiedNameHash(name);
@@ -1734,6 +1742,7 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
                 // have been re-ordered.
                 decl.src_index = decl_i;
                 if (deleted_decls.remove(decl) == null) {
+                    decl.analysis = .sema_failure;
                     const err_msg = try ErrorMsg.create(self.allocator, tree.token_locs[name_tok].start, "redefinition of '{}'", .{decl.name});
                     errdefer err_msg.destroy(self.allocator);
                     try self.failed_decls.putNoClobber(decl, err_msg);
test/stage2/compile_errors.zig
@@ -86,12 +86,18 @@ pub fn addCases(ctx: *TestContext) !void {
             \\export fn entry() void {}
         );
     }
+    {
+        var case = ctx.obj("missing function name", linux_x64);
+        case.addError(
+            \\fn() void {}
+        , &[_][]const u8{":1:3: error: missing function name"});
+        case.compiles(
+            \\fn a() void {}
+        );
+    }
     // TODO: re-enable these tests.
     // https://github.com/ziglang/zig/issues/1364
 
-    //    ctx.addError("Missing function name", linux_x64, .Zig,
-    //        \\fn() void {}
-    //    , &[_][]const u8{":1:3: error: missing function name"});
     //ctx.testCompileError(
     //    \\comptime {
     //    \\    return;