authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-04-04 04:04:20-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-06 12:57:51-07:00
log4a8121c1abfe6ffe9c6720e2841d80a36927b11a
treeb6eaecaf340b315bf4a218c5711aab676d5e316c
parent4e8553660405a063d8abd335a600714af121aed9

Sema: fix non-`pub` `usingnamespace` in `@typeInfo`


4 files changed, 34 insertions(+), 30 deletions(-)

src/Sema.zig+2-1
......@@ -18707,13 +18707,14 @@ fn typeInfoNamespaceDecls(
1870718707 const decls = namespace.decls.keys();
1870818708 for (decls) |decl_index| {
1870918709 const decl = mod.declPtr(decl_index);
18710 if (!decl.is_pub) continue;
1871018711 if (decl.kind == .@"usingnamespace") {
1871118712 if (decl.analysis == .in_progress) continue;
1871218713 try mod.ensureDeclAnalyzed(decl_index);
1871318714 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
1871418715 continue;
1871518716 }
18716 if (decl.kind != .named or !decl.is_pub) continue;
18717 if (decl.kind != .named) continue;
1871718718 const name_val = v: {
1871818719 // TODO: write something like getCoercedInts to avoid needing to dupe
1871918720 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(decl.name));
test/behavior.zig-1
......@@ -97,7 +97,6 @@ test {
9797 _ = @import("behavior/tuple_declarations.zig");
9898 _ = @import("behavior/type.zig");
9999 _ = @import("behavior/type_info.zig");
100 _ = @import("behavior/type_info_only_pub_decls.zig");
101100 _ = @import("behavior/type_info_mul_linksection_addrspace_decls.zig");
102101 _ = @import("behavior/typename.zig");
103102 _ = @import("behavior/undefined.zig");
test/behavior/type_info.zig+32-4
......@@ -571,11 +571,13 @@ test "typeInfo resolves usingnamespace declarations" {
571571
572572 const B = struct {
573573 pub const f0 = 42;
574 usingnamespace A;
574 pub usingnamespace A;
575575 };
576576
577 try expect(@typeInfo(B).Struct.decls.len == 2);
578 //a
577 const decls = @typeInfo(B).Struct.decls;
578 try expect(decls.len == 2);
579 try expectEqualStrings(decls[0].name, "f0");
580 try expectEqualStrings(decls[1].name, "f1");
579581}
580582
581583test "value from struct @typeInfo default_value can be loaded at comptime" {
......@@ -596,7 +598,7 @@ test "@typeInfo decls and usingnamespace" {
596598 comptime {}
597599 };
598600 const B = struct {
599 usingnamespace A;
601 pub usingnamespace A;
600602 pub const z = 56;
601603
602604 test {}
......@@ -626,3 +628,29 @@ test "type info of tuple of string literal default value" {
626628 const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*;
627629 comptime std.debug.assert(value[0] == 'h');
628630}
631
632test "@typeInfo only contains pub decls" {
633 const other = struct {
634 const std = @import("std");
635
636 usingnamespace struct {
637 pub const inside_non_pub_usingnamespace = 0;
638 };
639
640 pub const Enum = enum {
641 a,
642 b,
643 c,
644 };
645
646 pub const Struct = struct {
647 foo: i32,
648 };
649 };
650 const ti = @typeInfo(other);
651 const decls = ti.Struct.decls;
652
653 try std.testing.expectEqual(2, decls.len);
654 try std.testing.expectEqualStrings("Enum", decls[0].name);
655 try std.testing.expectEqualStrings("Struct", decls[1].name);
656}
test/behavior/type_info_only_pub_decls.zig deleted-24
......@@ -1,24 +0,0 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const other = struct {
4 const std = @import("std");
5
6 pub const Enum = enum {
7 a,
8 b,
9 c,
10 };
11
12 pub const Struct = struct {
13 foo: i32,
14 };
15};
16
17test {
18 const ti = @typeInfo(other);
19 const decls = ti.Struct.decls;
20
21 try std.testing.expectEqual(2, decls.len);
22 try std.testing.expectEqualStrings("Enum", decls[0].name);
23 try std.testing.expectEqualStrings("Struct", decls[1].name);
24}