authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-04-09 00:09:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-09 10:05:15-07:00
logafe5862111034ea4100b0eea5971e181d70ffc39
tree454fcaa87f6f3c50ccac5c038dd5e6e6236fa54a
parentc6791d87d4f49598aab54064df683fc86e97dbd9

stage2: add error for private decls accessed from other files


3 files changed, 43 insertions(+), 3 deletions(-)

src/Module.zig+1
......@@ -3550,6 +3550,7 @@ fn semaContainerFn(
35503550 mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
35513551 }
35523552 }
3553 new_decl.is_pub = fn_proto.visib_token != null;
35533554 }
35543555}
35553556
src/Sema.zig+4-2
......@@ -4708,7 +4708,8 @@ fn namedFieldPtr(
47084708 .Struct, .Opaque, .Union => {
47094709 if (child_type.getContainerScope()) |container_scope| {
47104710 if (mod.lookupDeclName(&container_scope.base, field_name)) |decl| {
4711 // TODO if !decl.is_pub and inDifferentFiles() "{} is private"
4711 if (!decl.is_pub and !(decl.container.file_scope == block.base.namespace().file_scope))
4712 return mod.fail(&block.base, src, "'{s}' is private", .{field_name});
47124713 return sema.analyzeDeclRef(block, src, decl);
47134714 }
47144715
......@@ -4736,7 +4737,8 @@ fn namedFieldPtr(
47364737 .Enum => {
47374738 if (child_type.getContainerScope()) |container_scope| {
47384739 if (mod.lookupDeclName(&container_scope.base, field_name)) |decl| {
4739 // TODO if !decl.is_pub and inDifferentFiles() "{} is private"
4740 if (!decl.is_pub and !(decl.container.file_scope == block.base.namespace().file_scope))
4741 return mod.fail(&block.base, src, "'{s}' is private", .{field_name});
47404742 return sema.analyzeDeclRef(block, src, decl);
47414743 }
47424744 }
test/stage2/test.zig+38-1
......@@ -1048,7 +1048,7 @@ pub fn addCases(ctx: *TestContext) !void {
10481048 "Hello, World!\n",
10491049 );
10501050 try case.files.append(.{
1051 .src =
1051 .src =
10521052 \\pub fn print() void {
10531053 \\ asm volatile ("syscall"
10541054 \\ :
......@@ -1064,6 +1064,43 @@ pub fn addCases(ctx: *TestContext) !void {
10641064 .path = "print.zig",
10651065 });
10661066 }
1067 {
1068 var case = ctx.exe("import private", linux_x64);
1069 case.addError(
1070 \\export fn _start() noreturn {
1071 \\ @import("print.zig").print();
1072 \\ exit();
1073 \\}
1074 \\
1075 \\fn exit() noreturn {
1076 \\ asm volatile ("syscall"
1077 \\ :
1078 \\ : [number] "{rax}" (231),
1079 \\ [arg1] "{rdi}" (@as(usize, 0))
1080 \\ : "rcx", "r11", "memory"
1081 \\ );
1082 \\ unreachable;
1083 \\}
1084 ,
1085 &.{":2:25: error: 'print' is private"},
1086 );
1087 try case.files.append(.{
1088 .src =
1089 \\fn print() void {
1090 \\ asm volatile ("syscall"
1091 \\ :
1092 \\ : [number] "{rax}" (@as(usize, 1)),
1093 \\ [arg1] "{rdi}" (@as(usize, 1)),
1094 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
1095 \\ [arg3] "{rdx}" (@as(usize, 14))
1096 \\ : "rcx", "r11", "memory"
1097 \\ );
1098 \\ return;
1099 \\}
1100 ,
1101 .path = "print.zig",
1102 });
1103 }
10671104
10681105 ctx.compileError("function redefinition", linux_x64,
10691106 \\// dummy comment