Commit 6510888039

Noam Preil <pleasantatk@gmail.com>
2020-06-26 10:03:54
Stage2: function redefinition detection for Zig code
1 parent 4a17e00
Changed files (2)
src-self-hosted
test
src-self-hosted/Module.zig
@@ -1733,10 +1733,15 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
                 // Update the AST Node index of the decl, even if its contents are unchanged, it may
                 // have been re-ordered.
                 decl.src_index = decl_i;
-                deleted_decls.removeAssertDiscard(decl);
-                if (!srcHashEql(decl.contents_hash, contents_hash)) {
-                    try self.markOutdatedDecl(decl);
-                    decl.contents_hash = contents_hash;
+                if (deleted_decls.remove(decl) == null) {
+                    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);
+                } else {
+                    if (!srcHashEql(decl.contents_hash, contents_hash)) {
+                        try self.markOutdatedDecl(decl);
+                        decl.contents_hash = contents_hash;
+                    }
                 }
             } else {
                 const new_decl = try self.createNewDecl(&root_scope.base, name, decl_i, name_hash, contents_hash);
test/stage2/compile_errors.zig
@@ -67,14 +67,28 @@ pub fn addCases(ctx: *TestContext) !void {
             \\@1 = export(@0, "start")
         );
     }
+    // TODO: need to make sure this works with other variants of export.
+    // As is, the same error occurs without export.
+    {
+        var case = ctx.obj("exported symbol collision", linux_x64);
+        case.addError(
+            \\export fn entry() void {}
+            \\export fn entry() void {}
+        , &[_][]const u8{":2:11: error: redefinition of 'entry'"});
+        case.compiles(
+            \\export fn entry() void {}
+        );
+        case.addError(
+            \\fn entry() void {}
+            \\fn entry() void {}
+        , &[_][]const u8{":2:4: error: redefinition of 'entry'"});
+        case.compiles(
+            \\export fn entry() void {}
+        );
+    }
     // TODO: re-enable these tests.
     // https://github.com/ziglang/zig/issues/1364
 
-    //     ctx.compileError("Export same symbol twice", linux_x64,
-    //         \\export fn entry() void {}
-    //         \\export fn entry() void {}
-    //     , &[_][]const u8{":2:1: error: exported symbol collision"});
-
     //    ctx.addError("Missing function name", linux_x64, .Zig,
     //        \\fn() void {}
     //    , &[_][]const u8{":1:3: error: missing function name"});