From c8f60b2e2f15713754ac4b0911a7d13b6057264d Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Fri, 26 Jun 2020 04:36:17 -0400 Subject: [PATCH] Stage2: handle missing function names --- src-self-hosted/Module.zig | 13 +++++++++++-- test/stage2/compile_errors.zig | 12 +++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src-self-hosted/Module.zig b/src-self-hosted/Module.zig index 134693b6fd..8ac4e89807 100644 --- a/src-self-hosted/Module.zig +++ b/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); diff --git a/test/stage2/compile_errors.zig b/test/stage2/compile_errors.zig index f7766abcef..1184048cbb 100644 --- a/test/stage2/compile_errors.zig +++ b/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;