Skip to content

Commit 4a8121c

Browse files
jacobly0andrewrk
authored andcommitted
Sema: fix non-pub usingnamespace in @typeInfo
1 parent 4e85536 commit 4a8121c

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

src/Sema.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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

581583
test "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+
632+
test "@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

-24
This file was deleted.

0 commit comments

Comments
 (0)