Commit afe5862111

jacob gw <jacoblevgw@gmail.com>
2021-04-09 06:09:21
stage2: add error for private decls accessed from other files
1 parent c6791d8
Changed files (3)
src/Module.zig
@@ -3550,6 +3550,7 @@ fn semaContainerFn(
                 mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
             }
         }
+        new_decl.is_pub = fn_proto.visib_token != null;
     }
 }
 
src/Sema.zig
@@ -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);
                         }
                     }
test/stage2/test.zig
@@ -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