Commit 4a8121c1ab

Jacob Young <jacobly0@users.noreply.github.com>
2024-04-04 10:04:20
Sema: fix non-`pub` `usingnamespace` in `@typeInfo`
1 parent 4e85536
src/Sema.zig
@@ -18707,13 +18707,14 @@ fn typeInfoNamespaceDecls(
     const decls = namespace.decls.keys();
     for (decls) |decl_index| {
         const decl = mod.declPtr(decl_index);
+        if (!decl.is_pub) continue;
         if (decl.kind == .@"usingnamespace") {
             if (decl.analysis == .in_progress) continue;
             try mod.ensureDeclAnalyzed(decl_index);
             try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
             continue;
         }
-        if (decl.kind != .named or !decl.is_pub) continue;
+        if (decl.kind != .named) continue;
         const name_val = v: {
             // TODO: write something like getCoercedInts to avoid needing to dupe
             const name = try sema.arena.dupeZ(u8, ip.stringToSlice(decl.name));
test/behavior/type_info.zig
@@ -571,11 +571,13 @@ test "typeInfo resolves usingnamespace declarations" {
 
     const B = struct {
         pub const f0 = 42;
-        usingnamespace A;
+        pub usingnamespace A;
     };
 
-    try expect(@typeInfo(B).Struct.decls.len == 2);
-    //a
+    const decls = @typeInfo(B).Struct.decls;
+    try expect(decls.len == 2);
+    try expectEqualStrings(decls[0].name, "f0");
+    try expectEqualStrings(decls[1].name, "f1");
 }
 
 test "value from struct @typeInfo default_value can be loaded at comptime" {
@@ -596,7 +598,7 @@ test "@typeInfo decls and usingnamespace" {
         comptime {}
     };
     const B = struct {
-        usingnamespace A;
+        pub usingnamespace A;
         pub const z = 56;
 
         test {}
@@ -626,3 +628,29 @@ test "type info of tuple of string literal default value" {
     const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*;
     comptime std.debug.assert(value[0] == 'h');
 }
+
+test "@typeInfo only contains pub decls" {
+    const other = struct {
+        const std = @import("std");
+
+        usingnamespace struct {
+            pub const inside_non_pub_usingnamespace = 0;
+        };
+
+        pub const Enum = enum {
+            a,
+            b,
+            c,
+        };
+
+        pub const Struct = struct {
+            foo: i32,
+        };
+    };
+    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/type_info_only_pub_decls.zig
@@ -1,24 +0,0 @@
-const builtin = @import("builtin");
-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
@@ -97,7 +97,6 @@ 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/type_info_mul_linksection_addrspace_decls.zig");
     _ = @import("behavior/typename.zig");
     _ = @import("behavior/undefined.zig");