diff --git a/src/Module.zig b/src/Module.zig index af3a04fe98..d570652e73 100644 --- a/src/Module.zig +++ b/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; } } diff --git a/src/Sema.zig b/src/Sema.zig index 09c38f3a65..1b6941806e 100644 --- a/src/Sema.zig +++ b/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); } } diff --git a/test/stage2/test.zig b/test/stage2/test.zig index e1130e5e28..b4bc1a413e 100644 --- a/test/stage2/test.zig +++ b/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