authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-08 18:21:15+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-08 18:21:15+03:00
logf2d326607505f1f16d4964a824444aaa0da2c259
tree27e1bcdd67522c073dfbd2764e10b0d09549c58c
parentf1e5a4f1639d4c571bfc26610593a15f9e0e2cf3
parent10abffcd98f2f1ac9e8532c53820d7c1eea6d88f
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4932 from Qix-/fix-private-access

Fix private access

10 files changed, 72 insertions(+), 19 deletions(-)

lib/std/build.zig+5-5
...@@ -284,11 +284,11 @@ pub const Builder = struct {...@@ -284,11 +284,11 @@ pub const Builder = struct {
284 return run_step;284 return run_step;
285 }285 }
286286
287 fn dupe(self: *Builder, bytes: []const u8) []u8 {287 pub fn dupe(self: *Builder, bytes: []const u8) []u8 {
288 return mem.dupe(self.allocator, u8, bytes) catch unreachable;288 return mem.dupe(self.allocator, u8, bytes) catch unreachable;
289 }289 }
290290
291 fn dupePath(self: *Builder, bytes: []const u8) []u8 {291 pub fn dupePath(self: *Builder, bytes: []const u8) []u8 {
292 const the_copy = self.dupe(bytes);292 const the_copy = self.dupe(bytes);
293 for (the_copy) |*byte| {293 for (the_copy) |*byte| {
294 switch (byte.*) {294 switch (byte.*) {
...@@ -717,7 +717,7 @@ pub const Builder = struct {...@@ -717,7 +717,7 @@ pub const Builder = struct {
717 return self.invalid_user_input;717 return self.invalid_user_input;
718 }718 }
719719
720 fn spawnChild(self: *Builder, argv: []const []const u8) !void {720 pub fn spawnChild(self: *Builder, argv: []const []const u8) !void {
721 return self.spawnChildEnvMap(null, self.env_map, argv);721 return self.spawnChildEnvMap(null, self.env_map, argv);
722 }722 }
723723
...@@ -843,7 +843,7 @@ pub const Builder = struct {...@@ -843,7 +843,7 @@ pub const Builder = struct {
843 }) catch unreachable;843 }) catch unreachable;
844 }844 }
845845
846 fn updateFile(self: *Builder, source_path: []const u8, dest_path: []const u8) !void {846 pub fn updateFile(self: *Builder, source_path: []const u8, dest_path: []const u8) !void {
847 if (self.verbose) {847 if (self.verbose) {
848 warn("cp {} {} ", .{ source_path, dest_path });848 warn("cp {} {} ", .{ source_path, dest_path });
849 }849 }
...@@ -855,7 +855,7 @@ pub const Builder = struct {...@@ -855,7 +855,7 @@ pub const Builder = struct {
855 };855 };
856 }856 }
857857
858 fn pathFromRoot(self: *Builder, rel_path: []const u8) []u8 {858 pub fn pathFromRoot(self: *Builder, rel_path: []const u8) []u8 {
859 return fs.path.resolve(self.allocator, &[_][]const u8{ self.build_root, rel_path }) catch unreachable;859 return fs.path.resolve(self.allocator, &[_][]const u8{ self.build_root, rel_path }) catch unreachable;
860 }860 }
861861
lib/std/dwarf.zig+4-4
...@@ -121,7 +121,7 @@ const Die = struct {...@@ -121,7 +121,7 @@ const Die = struct {
121 };121 };
122 }122 }
123123
124 fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]const u8 {124 pub fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]const u8 {
125 const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;125 const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
126 return switch (form_value.*) {126 return switch (form_value.*) {
127 FormValue.String => |value| value,127 FormValue.String => |value| value,
...@@ -389,7 +389,7 @@ pub const DwarfInfo = struct {...@@ -389,7 +389,7 @@ pub const DwarfInfo = struct {
389 return self.abbrev_table_list.allocator;389 return self.abbrev_table_list.allocator;
390 }390 }
391391
392 fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 {392 pub fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 {
393 for (di.func_list.span()) |*func| {393 for (di.func_list.span()) |*func| {
394 if (func.pc_range) |range| {394 if (func.pc_range) |range| {
395 if (address >= range.start and address < range.end) {395 if (address >= range.start and address < range.end) {
...@@ -578,7 +578,7 @@ pub const DwarfInfo = struct {...@@ -578,7 +578,7 @@ pub const DwarfInfo = struct {
578 }578 }
579 }579 }
580580
581 fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit {581 pub fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit {
582 for (di.compile_unit_list.span()) |*compile_unit| {582 for (di.compile_unit_list.span()) |*compile_unit| {
583 if (compile_unit.pc_range) |range| {583 if (compile_unit.pc_range) |range| {
584 if (target_address >= range.start and target_address < range.end) return compile_unit;584 if (target_address >= range.start and target_address < range.end) return compile_unit;
...@@ -690,7 +690,7 @@ pub const DwarfInfo = struct {...@@ -690,7 +690,7 @@ pub const DwarfInfo = struct {
690 return result;690 return result;
691 }691 }
692692
693 fn getLineNumberInfo(di: *DwarfInfo, compile_unit: CompileUnit, target_address: usize) !debug.LineInfo {693 pub fn getLineNumberInfo(di: *DwarfInfo, compile_unit: CompileUnit, target_address: usize) !debug.LineInfo {
694 var stream = io.fixedBufferStream(di.debug_line);694 var stream = io.fixedBufferStream(di.debug_line);
695 const in = &stream.inStream();695 const in = &stream.inStream();
696 const seekable = &stream.seekableStream();696 const seekable = &stream.seekableStream();
lib/std/dynamic_library.zig+2-2
...@@ -33,11 +33,11 @@ const LinkMap = extern struct {...@@ -33,11 +33,11 @@ const LinkMap = extern struct {
33 pub const Iterator = struct {33 pub const Iterator = struct {
34 current: ?*LinkMap,34 current: ?*LinkMap,
3535
36 fn end(self: *Iterator) bool {36 pub fn end(self: *Iterator) bool {
37 return self.current == null;37 return self.current == null;
38 }38 }
3939
40 fn next(self: *Iterator) ?*LinkMap {40 pub fn next(self: *Iterator) ?*LinkMap {
41 if (self.current) |it| {41 if (self.current) |it| {
42 self.current = it.l_next;42 self.current = it.l_next;
43 return it;43 return it;
lib/std/json.zig+1-1
...@@ -2336,7 +2336,7 @@ pub const StringifyOptions = struct {...@@ -2336,7 +2336,7 @@ pub const StringifyOptions = struct {
2336 /// After a colon, should whitespace be inserted?2336 /// After a colon, should whitespace be inserted?
2337 separator: bool = true,2337 separator: bool = true,
23382338
2339 fn outputIndent(2339 pub fn outputIndent(
2340 whitespace: @This(),2340 whitespace: @This(),
2341 out_stream: var,2341 out_stream: var,
2342 ) @TypeOf(out_stream).Error!void {2342 ) @TypeOf(out_stream).Error!void {
lib/std/net.zig+1-1
...@@ -386,7 +386,7 @@ pub const AddressList = struct {...@@ -386,7 +386,7 @@ pub const AddressList = struct {
386 addrs: []Address,386 addrs: []Address,
387 canon_name: ?[]u8,387 canon_name: ?[]u8,
388388
389 fn deinit(self: *AddressList) void {389 pub fn deinit(self: *AddressList) void {
390 // Here we copy the arena allocator into stack memory, because390 // Here we copy the arena allocator into stack memory, because
391 // otherwise it would destroy itself while it was still working.391 // otherwise it would destroy itself while it was still working.
392 var arena = self.arena;392 var arena = self.arena;
lib/std/pdb.zig+4-4
...@@ -644,7 +644,7 @@ const MsfStream = struct {...@@ -644,7 +644,7 @@ const MsfStream = struct {
644 return stream;644 return stream;
645 }645 }
646646
647 fn readNullTermString(self: *MsfStream, allocator: *mem.Allocator) ![]u8 {647 pub fn readNullTermString(self: *MsfStream, allocator: *mem.Allocator) ![]u8 {
648 var list = ArrayList(u8).init(allocator);648 var list = ArrayList(u8).init(allocator);
649 while (true) {649 while (true) {
650 const byte = try self.inStream().readByte();650 const byte = try self.inStream().readByte();
...@@ -684,13 +684,13 @@ const MsfStream = struct {...@@ -684,13 +684,13 @@ const MsfStream = struct {
684 return buffer.len;684 return buffer.len;
685 }685 }
686686
687 fn seekBy(self: *MsfStream, len: i64) !void {687 pub fn seekBy(self: *MsfStream, len: i64) !void {
688 self.pos = @intCast(u64, @intCast(i64, self.pos) + len);688 self.pos = @intCast(u64, @intCast(i64, self.pos) + len);
689 if (self.pos >= self.blocks.len * self.block_size)689 if (self.pos >= self.blocks.len * self.block_size)
690 return error.EOF;690 return error.EOF;
691 }691 }
692692
693 fn seekTo(self: *MsfStream, len: u64) !void {693 pub fn seekTo(self: *MsfStream, len: u64) !void {
694 self.pos = len;694 self.pos = len;
695 if (self.pos >= self.blocks.len * self.block_size)695 if (self.pos >= self.blocks.len * self.block_size)
696 return error.EOF;696 return error.EOF;
...@@ -708,7 +708,7 @@ const MsfStream = struct {...@@ -708,7 +708,7 @@ const MsfStream = struct {
708 return block * self.block_size + offset;708 return block * self.block_size + offset;
709 }709 }
710710
711 fn inStream(self: *MsfStream) std.io.InStream(*MsfStream, Error, read) {711 pub fn inStream(self: *MsfStream) std.io.InStream(*MsfStream, Error, read) {
712 return .{ .context = self };712 return .{ .context = self };
713 }713 }
714};714};
lib/std/zig/cross_target.zig+1-1
...@@ -660,7 +660,7 @@ pub const CrossTarget = struct {...@@ -660,7 +660,7 @@ pub const CrossTarget = struct {
660 return Target.getObjectFormatSimple(self.getOsTag(), self.getCpuArch());660 return Target.getObjectFormatSimple(self.getOsTag(), self.getCpuArch());
661 }661 }
662662
663 fn updateCpuFeatures(self: CrossTarget, set: *Target.Cpu.Feature.Set) void {663 pub fn updateCpuFeatures(self: CrossTarget, set: *Target.Cpu.Feature.Set) void {
664 set.removeFeatureSet(self.cpu_features_sub);664 set.removeFeatureSet(self.cpu_features_sub);
665 set.addFeatureSet(self.cpu_features_add);665 set.addFeatureSet(self.cpu_features_add);
666 set.populateDependencies(self.getCpuArch().allFeaturesList());666 set.populateDependencies(self.getCpuArch().allFeaturesList());
src-self-hosted/ir/text.zig+1-1
...@@ -236,7 +236,7 @@ pub const Inst = struct {...@@ -236,7 +236,7 @@ pub const Inst = struct {
236 @"comptime_int",236 @"comptime_int",
237 @"comptime_float",237 @"comptime_float",
238238
239 fn toType(self: BuiltinType) Type {239 pub fn toType(self: BuiltinType) Type {
240 return switch (self) {240 return switch (self) {
241 .@"isize" => Type.initTag(.@"isize"),241 .@"isize" => Type.initTag(.@"isize"),
242 .@"usize" => Type.initTag(.@"usize"),242 .@"usize" => Type.initTag(.@"usize"),
src/ir.cpp+9
...@@ -21624,6 +21624,15 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -21624,6 +21624,15 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
21624 if (tld->resolution == TldResolutionResolving)21624 if (tld->resolution == TldResolutionResolving)
21625 return ir_error_dependency_loop(ira, source_instr);21625 return ir_error_dependency_loop(ira, source_instr);
2162621626
21627 if (tld->visib_mod == VisibModPrivate &&
21628 tld->import != get_scope_import(source_instr->scope))
21629 {
21630 ErrorMsg *msg = ir_add_error(ira, source_instr,
21631 buf_sprintf("'%s' is private", buf_ptr(field_name)));
21632 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
21633 return ira->codegen->invalid_inst_gen;
21634 }
21635
21627 TldFn *tld_fn = (TldFn *)tld;21636 TldFn *tld_fn = (TldFn *)tld;
21628 ZigFn *fn_entry = tld_fn->fn_entry;21637 ZigFn *fn_entry = tld_fn->fn_entry;
21629 assert(fn_entry != nullptr);21638 assert(fn_entry != nullptr);
test/compile_errors.zig+44
...@@ -5375,6 +5375,50 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5375,6 +5375,50 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5375 break :x tc;5375 break :x tc;
5376 });5376 });
53775377
5378 cases.addCase(x: {
5379 const tc = cases.create("multiple files with private member instance function (canonical invocation) error",
5380 \\const Foo = @import("foo.zig",).Foo;
5381 \\
5382 \\export fn callPrivFunction() void {
5383 \\ var foo = Foo{};
5384 \\ Foo.privateFunction(foo);
5385 \\}
5386 , &[_][]const u8{
5387 "tmp.zig:5:8: error: 'privateFunction' is private",
5388 "foo.zig:2:5: note: declared here",
5389 });
5390
5391 tc.addSourceFile("foo.zig",
5392 \\pub const Foo = struct {
5393 \\ fn privateFunction(self: *Foo) void { }
5394 \\};
5395 );
5396
5397 break :x tc;
5398 });
5399
5400 cases.addCase(x: {
5401 const tc = cases.create("multiple files with private member instance function error",
5402 \\const Foo = @import("foo.zig",).Foo;
5403 \\
5404 \\export fn callPrivFunction() void {
5405 \\ var foo = Foo{};
5406 \\ foo.privateFunction();
5407 \\}
5408 , &[_][]const u8{
5409 "tmp.zig:5:8: error: 'privateFunction' is private",
5410 "foo.zig:2:5: note: declared here",
5411 });
5412
5413 tc.addSourceFile("foo.zig",
5414 \\pub const Foo = struct {
5415 \\ fn privateFunction(self: *Foo) void { }
5416 \\};
5417 );
5418
5419 break :x tc;
5420 });
5421
5378 cases.add("container init with non-type",5422 cases.add("container init with non-type",
5379 \\const zero: i32 = 0;5423 \\const zero: i32 = 0;
5380 \\const a = zero{1};5424 \\const a = zero{1};