Skip to content

Commit

Permalink
Sema: fix non-pub usingnamespace in @typeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobly0 authored and andrewrk committed Apr 6, 2024
1 parent 4e85536 commit 4a8121c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 0 additions & 1 deletion test/behavior.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
36 changes: 32 additions & 4 deletions test/behavior/type_info.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -596,7 +598,7 @@ test "@typeInfo decls and usingnamespace" {
comptime {}
};
const B = struct {
usingnamespace A;
pub usingnamespace A;
pub const z = 56;

test {}
Expand Down Expand Up @@ -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);
}
24 changes: 0 additions & 24 deletions test/behavior/type_info_only_pub_decls.zig

This file was deleted.

0 comments on commit 4a8121c

Please sign in to comment.