Skip to content

Commit a63feae

Browse files
committed
add private fields
1 parent ec13d74 commit a63feae

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/std/zig/tokenizer.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ pub const Tokenizer = struct {
437437
result.tag = .char_literal;
438438
continue :state .char_literal;
439439
},
440-
'a'...'z', 'A'...'Z', '_' => {
440+
'a'...'z', 'A'...'Z', '_', '#' => {
441441
result.tag = .identifier;
442442
continue :state .identifier;
443443
},

src/Sema.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28567,6 +28567,21 @@ fn structFieldPtr(
2856728567
const field_index = struct_type.nameIndex(ip, field_name) orelse
2856828568
return sema.failWithBadStructFieldAccess(block, struct_ty, struct_type, field_name_src, field_name);
2856928569

28570+
const field_name_slice = field_name.toSlice(ip);
28571+
if (std.mem.startsWith(u8, field_name_slice, "#")) {
28572+
// Get the file scope of the struct
28573+
const struct_namespace = struct_ty.getNamespace(zcu).unwrap().?;
28574+
const struct_file_scope = zcu.namespacePtr(struct_namespace).file_scope;
28575+
28576+
// Get the current file scope
28577+
const current_file_scope = block.getFileScopeIndex(zcu);
28578+
28579+
// If not in the same file, deny access
28580+
if (struct_file_scope != current_file_scope) {
28581+
return sema.fail(block, field_name_src, "field '{}' is private and cannot be accessed outside its defining file", .{field_name.fmt(ip)});
28582+
}
28583+
}
28584+
2857028585
return sema.structFieldPtrByIndex(block, src, struct_ptr, field_index, struct_ty);
2857128586
}
2857228587

@@ -28681,6 +28696,23 @@ fn structFieldVal(
2868128696

2868228697
const field_index = struct_type.nameIndex(ip, field_name) orelse
2868328698
return sema.failWithBadStructFieldAccess(block, struct_ty, struct_type, field_name_src, field_name);
28699+
28700+
// Check if field is private (starts with '#')
28701+
const field_name_slice = field_name.toSlice(ip);
28702+
if (std.mem.startsWith(u8, field_name_slice, "#")) {
28703+
// Get the file scope of the struct
28704+
const struct_namespace = struct_ty.getNamespace(zcu).unwrap().?;
28705+
const struct_file_scope = zcu.namespacePtr(struct_namespace).file_scope;
28706+
28707+
// Get the current file scope
28708+
const current_file_scope = block.getFileScopeIndex(zcu);
28709+
28710+
// If not in the same file, deny access
28711+
if (struct_file_scope != current_file_scope) {
28712+
return sema.fail(block, field_name_src, "field '{}' is private and cannot be accessed outside its defining file", .{field_name.fmt(ip)});
28713+
}
28714+
}
28715+
2868428716
if (struct_type.fieldIsComptime(ip, field_index)) {
2868528717
try struct_ty.resolveStructFieldInits(pt);
2868628718
return Air.internedToRef(struct_type.field_inits.get(ip)[field_index]);

0 commit comments

Comments
 (0)