zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6510888039baaa0058a7e2bade2750569af1abbd (tree)
parent 4a17e008daa90d5dbb0fc917d53e52c1ec990f2d
Author: Noam Preil <pleasantatk@gmail.com>
Date:   Fri, 26 Jun 2020 04:03:54 -0400

Stage2: function redefinition detection for Zig code

Diffstat:
Msrc-self-hosted/Module.zig | 13+++++++++----
Mtest/stage2/compile_errors.zig | 24+++++++++++++++++++-----
2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src-self-hosted/Module.zig b/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); diff --git a/test/stage2/compile_errors.zig b/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"});