stage2: add error for private decls accessed from other files

This commit is contained in:
jacob gw 2021-04-09 00:09:21 -04:00 committed by Andrew Kelley
parent c6791d87d4
commit afe5862111
3 changed files with 43 additions and 3 deletions

View File

@ -3550,6 +3550,7 @@ fn semaContainerFn(
mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
}
}
new_decl.is_pub = fn_proto.visib_token != null;
}
}

View File

@ -4708,7 +4708,8 @@ fn namedFieldPtr(
.Struct, .Opaque, .Union => {
if (child_type.getContainerScope()) |container_scope| {
if (mod.lookupDeclName(&container_scope.base, field_name)) |decl| {
// TODO if !decl.is_pub and inDifferentFiles() "{} is private"
if (!decl.is_pub and !(decl.container.file_scope == block.base.namespace().file_scope))
return mod.fail(&block.base, src, "'{s}' is private", .{field_name});
return sema.analyzeDeclRef(block, src, decl);
}
@ -4736,7 +4737,8 @@ fn namedFieldPtr(
.Enum => {
if (child_type.getContainerScope()) |container_scope| {
if (mod.lookupDeclName(&container_scope.base, field_name)) |decl| {
// TODO if !decl.is_pub and inDifferentFiles() "{} is private"
if (!decl.is_pub and !(decl.container.file_scope == block.base.namespace().file_scope))
return mod.fail(&block.base, src, "'{s}' is private", .{field_name});
return sema.analyzeDeclRef(block, src, decl);
}
}

View File

@ -1048,7 +1048,7 @@ pub fn addCases(ctx: *TestContext) !void {
"Hello, World!\n",
);
try case.files.append(.{
.src =
.src =
\\pub fn print() void {
\\ asm volatile ("syscall"
\\ :
@ -1064,6 +1064,43 @@ pub fn addCases(ctx: *TestContext) !void {
.path = "print.zig",
});
}
{
var case = ctx.exe("import private", linux_x64);
case.addError(
\\export fn _start() noreturn {
\\ @import("print.zig").print();
\\ exit();
\\}
\\
\\fn exit() noreturn {
\\ asm volatile ("syscall"
\\ :
\\ : [number] "{rax}" (231),
\\ [arg1] "{rdi}" (@as(usize, 0))
\\ : "rcx", "r11", "memory"
\\ );
\\ unreachable;
\\}
,
&.{":2:25: error: 'print' is private"},
);
try case.files.append(.{
.src =
\\fn print() void {
\\ asm volatile ("syscall"
\\ :
\\ : [number] "{rax}" (@as(usize, 1)),
\\ [arg1] "{rdi}" (@as(usize, 1)),
\\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
\\ [arg3] "{rdx}" (@as(usize, 14))
\\ : "rcx", "r11", "memory"
\\ );
\\ return;
\\}
,
.path = "print.zig",
});
}
ctx.compileError("function redefinition", linux_x64,
\\// dummy comment