Stage2: function redefinition detection for Zig code

This commit is contained in:
Noam Preil 2020-06-26 04:03:54 -04:00
parent 4a17e008da
commit 6510888039
No known key found for this signature in database
GPG Key ID: FC347E7C85BE8238
2 changed files with 28 additions and 9 deletions

View File

@ -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);

View File

@ -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"});