@@ -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