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(...@@ -18707,13 +18707,14 @@ fn typeInfoNamespaceDecls(
18707 const decls = namespace.decls.keys();18707 const decls = namespace.decls.keys();
18708 for (decls) |decl_index| {18708 for (decls) |decl_index| {
18709 const decl = mod.declPtr(decl_index);18709 const decl = mod.declPtr(decl_index);
18710 if (!decl.is_pub) continue;
18710 if (decl.kind == .@"usingnamespace") {18711 if (decl.kind == .@"usingnamespace") {
18711 if (decl.analysis == .in_progress) continue;18712 if (decl.analysis == .in_progress) continue;
18712 try mod.ensureDeclAnalyzed(decl_index);18713 try mod.ensureDeclAnalyzed(decl_index);
18713 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);18714 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
18714 continue;18715 continue;
18715 }18716 }
18716 if (decl.kind != .named or !decl.is_pub) continue;18717 if (decl.kind != .named) continue;
18717 const name_val = v: {18718 const name_val = v: {
18718 // TODO: write something like getCoercedInts to avoid needing to dupe18719 // TODO: write something like getCoercedInts to avoid needing to dupe
18719 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(decl.name));18720 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(decl.name));
test/behavior.zig-1
...@@ -97,7 +97,6 @@ test {...@@ -97,7 +97,6 @@ test {
97 _ = @import("behavior/tuple_declarations.zig");97 _ = @import("behavior/tuple_declarations.zig");
98 _ = @import("behavior/type.zig");98 _ = @import("behavior/type.zig");
99 _ = @import("behavior/type_info.zig");99 _ = @import("behavior/type_info.zig");
100 _ = @import("behavior/type_info_only_pub_decls.zig");
101 _ = @import("behavior/type_info_mul_linksection_addrspace_decls.zig");100 _ = @import("behavior/type_info_mul_linksection_addrspace_decls.zig");
102 _ = @import("behavior/typename.zig");101 _ = @import("behavior/typename.zig");
103 _ = @import("behavior/undefined.zig");102 _ = @import("behavior/undefined.zig");
test/behavior/type_info.zig+32-4
...@@ -571,11 +571,13 @@ test "typeInfo resolves usingnamespace declarations" {...@@ -571,11 +571,13 @@ test "typeInfo resolves usingnamespace declarations" {
571571
572 const B = struct {572 const B = struct {
573 pub const f0 = 42;573 pub const f0 = 42;
574 usingnamespace A;574 pub usingnamespace A;
575 };575 };
576576
577 try expect(@typeInfo(B).Struct.decls.len == 2);577 const decls = @typeInfo(B).Struct.decls;
578 //a578 try expect(decls.len == 2);
579 try expectEqualStrings(decls[0].name, "f0");
580 try expectEqualStrings(decls[1].name, "f1");
579}581}
580582
581test "value from struct @typeInfo default_value can be loaded at comptime" {583test "value from struct @typeInfo default_value can be loaded at comptime" {
...@@ -596,7 +598,7 @@ test "@typeInfo decls and usingnamespace" {...@@ -596,7 +598,7 @@ test "@typeInfo decls and usingnamespace" {
596 comptime {}598 comptime {}
597 };599 };
598 const B = struct {600 const B = struct {
599 usingnamespace A;601 pub usingnamespace A;
600 pub const z = 56;602 pub const z = 56;
601603
602 test {}604 test {}
...@@ -626,3 +628,29 @@ test "type info of tuple of string literal default value" {...@@ -626,3 +628,29 @@ test "type info of tuple of string literal default value" {
626 const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*;628 const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*;
627 comptime std.debug.assert(value[0] == 'h');629 comptime std.debug.assert(value[0] == 'h');
628}630}
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}