Commit 3c08fe931a

Jacob G-W <jacoblevgw@gmail.com>
2023-07-25 17:15:59
make `@typeInfo` not return private decls
fixes #10731 Thanks @nektro for previous work in #14878 This change creates a small breaking change: It removes the `is_pub` field of a decl in `@typeInfo`
1 parent 972e70b
lib/std/builtin.zig
@@ -437,7 +437,6 @@ pub const Type = union(enum) {
     /// therefore must be kept in sync with the compiler implementation.
     pub const Declaration = struct {
         name: []const u8,
-        is_pub: bool,
     };
 };
 
lib/std/meta.zig
@@ -293,18 +293,18 @@ test "std.meta.declarations" {
     const E1 = enum {
         A,
 
-        fn a() void {}
+        pub fn a() void {}
     };
     const S1 = struct {
-        fn a() void {}
+        pub fn a() void {}
     };
     const U1 = union {
         a: u8,
 
-        fn a() void {}
+        pub fn a() void {}
     };
     const O1 = opaque {
-        fn a() void {}
+        pub fn a() void {}
     };
 
     const decls = comptime [_][]const Type.Declaration{
@@ -333,15 +333,15 @@ test "std.meta.declarationInfo" {
     const E1 = enum {
         A,
 
-        fn a() void {}
+        pub fn a() void {}
     };
     const S1 = struct {
-        fn a() void {}
+        pub fn a() void {}
     };
     const U1 = union {
         a: u8,
 
-        fn a() void {}
+        pub fn a() void {}
     };
 
     const infos = comptime [_]Type.Declaration{
@@ -352,7 +352,6 @@ test "std.meta.declarationInfo" {
 
     inline for (infos) |info| {
         try testing.expect(comptime mem.eql(u8, info.name, "a"));
-        try testing.expect(!info.is_pub);
     }
 }
 pub fn fields(comptime T: type) switch (@typeInfo(T)) {
@@ -597,7 +596,6 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
         if (expected_decls.len != actual_decls.len) return error.FailedTest;
         for (expected_decls, 0..) |expected_decl, i| {
             const actual_decl = actual_decls[i];
-            try testing.expectEqual(expected_decl.is_pub, actual_decl.is_pub);
             try testing.expectEqualStrings(expected_decl.name, actual_decl.name);
         }
     }
@@ -644,21 +642,21 @@ pub fn DeclEnum(comptime T: type) type {
 
 test "std.meta.DeclEnum" {
     const A = struct {
-        const a: u8 = 0;
+        pub const a: u8 = 0;
     };
     const B = union {
         foo: void,
 
-        const a: u8 = 0;
-        const b: void = {};
-        const c: f32 = 0;
+        pub const a: u8 = 0;
+        pub const b: void = {};
+        pub const c: f32 = 0;
     };
     const C = enum {
         bar,
 
-        const a: u8 = 0;
-        const b: void = {};
-        const c: f32 = 0;
+        pub const a: u8 = 0;
+        pub const b: void = {};
+        pub const c: f32 = 0;
     };
     try expectEqualEnum(enum { a }, DeclEnum(A));
     try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
lib/std/testing.zig
@@ -1122,7 +1122,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
 pub fn refAllDecls(comptime T: type) void {
     if (!builtin.is_test) return;
     inline for (comptime std.meta.declarations(T)) |decl| {
-        if (decl.is_pub) _ = &@field(T, decl.name);
+        _ = &@field(T, decl.name);
     }
 }
 
@@ -1131,14 +1131,12 @@ pub fn refAllDecls(comptime T: type) void {
 pub fn refAllDeclsRecursive(comptime T: type) void {
     if (!builtin.is_test) return;
     inline for (comptime std.meta.declarations(T)) |decl| {
-        if (decl.is_pub) {
-            if (@TypeOf(@field(T, decl.name)) == type) {
-                switch (@typeInfo(@field(T, decl.name))) {
-                    .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
-                    else => {},
-                }
+        if (@TypeOf(@field(T, decl.name)) == type) {
+            switch (@typeInfo(@field(T, decl.name))) {
+                .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
+                else => {},
             }
-            _ = &@field(T, decl.name);
         }
+        _ = &@field(T, decl.name);
     }
 }
src/codegen/llvm/Builder.zig
@@ -1159,7 +1159,6 @@ pub const Attribute = union(Kind) {
                     var any = false;
                     var remaining: Int = @bitCast(fpclass);
                     inline for (@typeInfo(FpClass).Struct.decls) |decl| {
-                        if (!decl.is_pub) continue;
                         const pattern: Int = @bitCast(@field(FpClass, decl.name));
                         if (remaining & pattern == pattern) {
                             if (!any) {
src/Sema.zig
@@ -17526,7 +17526,7 @@ fn typeInfoNamespaceDecls(
             try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
             continue;
         }
-        if (decl.kind != .named) continue;
+        if (decl.kind != .named or !decl.is_pub) continue;
         const name_val = v: {
             var anon_decl = try block.startAnonDecl();
             defer anon_decl.deinit();
@@ -17554,8 +17554,6 @@ fn typeInfoNamespaceDecls(
         const fields = .{
             //name: []const u8,
             name_val,
-            //is_pub: bool,
-            Value.makeBool(decl.is_pub).toIntern(),
         };
         try decl_vals.append(try mod.intern(.{ .aggregate = .{
             .ty = declaration_ty.toIntern(),
src/translate_c.zig
@@ -408,13 +408,11 @@ pub fn translate(
     }
 
     inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
-        if (decl.is_pub) {
-            const builtin = try Tag.pub_var_simple.create(arena, .{
-                .name = decl.name,
-                .init = try Tag.import_c_builtin.create(arena, decl.name),
-            });
-            try addTopLevelDecl(&context, decl.name, builtin);
-        }
+        const builtin = try Tag.pub_var_simple.create(arena, .{
+            .name = decl.name,
+            .init = try Tag.import_c_builtin.create(arena, decl.name),
+        });
+        try addTopLevelDecl(&context, decl.name, builtin);
     }
 
     try prepopulateGlobalNameTable(ast_unit, &context);
@@ -2120,7 +2118,6 @@ fn transImplicitCastExpr(
 
 fn isBuiltinDefined(name: []const u8) bool {
     inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
-        if (!decl.is_pub) continue;
         if (std.mem.eql(u8, name, decl.name)) return true;
     }
     return false;
test/behavior/eval.zig
@@ -959,7 +959,7 @@ test "debug variable type resolved through indirect zero-bit types" {
 test "const local with comptime init through array init" {
     const E1 = enum {
         A,
-        fn a() void {}
+        pub fn a() void {}
     };
 
     const S = struct {
test/behavior/type_info.zig
@@ -321,8 +321,7 @@ fn testPackedStruct() !void {
     try expect(struct_info.Struct.fields[2].default_value == null);
     try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4);
     try expect(struct_info.Struct.fields[3].alignment == 0);
-    try expect(struct_info.Struct.decls.len == 2);
-    try expect(struct_info.Struct.decls[0].is_pub);
+    try expect(struct_info.Struct.decls.len == 1);
 }
 
 const TestPackedStruct = packed struct {
@@ -344,8 +343,8 @@ test "type info: opaque info" {
 
 fn testOpaque() !void {
     const Foo = opaque {
-        const A = 1;
-        fn b() void {}
+        pub const A = 1;
+        pub fn b() void {}
     };
 
     const foo_info = @typeInfo(Foo);
@@ -514,11 +513,11 @@ test "Declarations are returned in declaration order" {
     if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 
     const S = struct {
-        const a = 1;
-        const b = 2;
-        const c = 3;
-        const d = 4;
-        const e = 5;
+        pub const a = 1;
+        pub const b = 2;
+        pub const c = 3;
+        pub const d = 4;
+        pub const e = 5;
     };
     const d = @typeInfo(S).Struct.decls;
     try expect(std.mem.eql(u8, d[0].name, "a"));
@@ -553,7 +552,7 @@ test "typeInfo resolves usingnamespace declarations" {
     };
 
     const B = struct {
-        const f0 = 42;
+        pub const f0 = 42;
         usingnamespace A;
     };
 
@@ -574,14 +573,14 @@ test "@typeInfo decls and usingnamespace" {
     if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
 
     const A = struct {
-        const x = 5;
-        const y = 34;
+        pub const x = 5;
+        pub const y = 34;
 
         comptime {}
     };
     const B = struct {
         usingnamespace A;
-        const z = 56;
+        pub const z = 56;
 
         test {}
     };
@@ -594,7 +593,7 @@ test "@typeInfo decls and usingnamespace" {
 
 test "@typeInfo decls ignore dependency loops" {
     const S = struct {
-        fn Def(comptime T: type) type {
+        pub fn Def(comptime T: type) type {
             std.debug.assert(@typeInfo(T).Struct.decls.len == 1);
             return struct {
                 const foo = u32;
test/behavior/type_info_only_pub_decls.zig
@@ -0,0 +1,23 @@
+const std = @import("std");
+const other = struct {
+    const std = @import("std");
+
+    pub const Enum = enum {
+        a,
+        b,
+        c,
+    };
+
+    pub const Struct = struct {
+        foo: i32,
+    };
+};
+
+test {
+    const ti = @typeInfo(other);
+    const decls = ti.Struct.decls;
+
+    try std.testing.expectEqual(2, decls.len);
+    try std.testing.expectEqualStrings("Enum", decls[0].name);
+    try std.testing.expectEqualStrings("Struct", decls[1].name);
+}
test/behavior.zig
@@ -222,6 +222,7 @@ test {
     _ = @import("behavior/tuple_declarations.zig");
     _ = @import("behavior/type.zig");
     _ = @import("behavior/type_info.zig");
+    _ = @import("behavior/type_info_only_pub_decls.zig");
     _ = @import("behavior/typename.zig");
     _ = @import("behavior/undefined.zig");
     _ = @import("behavior/underscore.zig");