Skip to content

Commit

Permalink
make @typeInfo not return private decls
Browse files Browse the repository at this point in the history
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
  • Loading branch information
g-w1 authored and andrewrk committed Jul 25, 2023
1 parent 972e70b commit 3c08fe9
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 52 deletions.
1 change: 0 additions & 1 deletion lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ pub const Type = union(enum) {
/// therefore must be kept in sync with the compiler implementation.
pub const Declaration = struct {
name: []const u8,
is_pub: bool,
};
};

Expand Down
30 changes: 14 additions & 16 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,18 @@ test "std.meta.declarations" {
const E1 = enum {
A,

fn a() void {}
pub fn a() void {}
};
const S1 = struct {
fn a() void {}
pub fn a() void {}
};
const U1 = union {
a: u8,

fn a() void {}
pub fn a() void {}
};
const O1 = opaque {
fn a() void {}
pub fn a() void {}
};

const decls = comptime [_][]const Type.Declaration{
Expand Down Expand Up @@ -333,15 +333,15 @@ test "std.meta.declarationInfo" {
const E1 = enum {
A,

fn a() void {}
pub fn a() void {}
};
const S1 = struct {
fn a() void {}
pub fn a() void {}
};
const U1 = union {
a: u8,

fn a() void {}
pub fn a() void {}
};

const infos = comptime [_]Type.Declaration{
Expand All @@ -352,7 +352,6 @@ test "std.meta.declarationInfo" {

inline for (infos) |info| {
try testing.expect(comptime mem.eql(u8, info.name, "a"));
try testing.expect(!info.is_pub);
}
}
pub fn fields(comptime T: type) switch (@typeInfo(T)) {
Expand Down Expand Up @@ -597,7 +596,6 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
if (expected_decls.len != actual_decls.len) return error.FailedTest;
for (expected_decls, 0..) |expected_decl, i| {
const actual_decl = actual_decls[i];
try testing.expectEqual(expected_decl.is_pub, actual_decl.is_pub);
try testing.expectEqualStrings(expected_decl.name, actual_decl.name);
}
}
Expand Down Expand Up @@ -644,21 +642,21 @@ pub fn DeclEnum(comptime T: type) type {

test "std.meta.DeclEnum" {
const A = struct {
const a: u8 = 0;
pub const a: u8 = 0;
};
const B = union {
foo: void,

const a: u8 = 0;
const b: void = {};
const c: f32 = 0;
pub const a: u8 = 0;
pub const b: void = {};
pub const c: f32 = 0;
};
const C = enum {
bar,

const a: u8 = 0;
const b: void = {};
const c: f32 = 0;
pub const a: u8 = 0;
pub const b: void = {};
pub const c: f32 = 0;
};
try expectEqualEnum(enum { a }, DeclEnum(A));
try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
Expand Down
14 changes: 6 additions & 8 deletions lib/std/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
pub fn refAllDecls(comptime T: type) void {
if (!builtin.is_test) return;
inline for (comptime std.meta.declarations(T)) |decl| {
if (decl.is_pub) _ = &@field(T, decl.name);
_ = &@field(T, decl.name);
}
}

Expand All @@ -1131,14 +1131,12 @@ pub fn refAllDecls(comptime T: type) void {
pub fn refAllDeclsRecursive(comptime T: type) void {
if (!builtin.is_test) return;
inline for (comptime std.meta.declarations(T)) |decl| {
if (decl.is_pub) {
if (@TypeOf(@field(T, decl.name)) == type) {
switch (@typeInfo(@field(T, decl.name))) {
.Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
else => {},
}
if (@TypeOf(@field(T, decl.name)) == type) {
switch (@typeInfo(@field(T, decl.name))) {
.Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
else => {},
}
_ = &@field(T, decl.name);
}
_ = &@field(T, decl.name);
}
}
4 changes: 1 addition & 3 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17526,7 +17526,7 @@ fn typeInfoNamespaceDecls(
try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
continue;
}
if (decl.kind != .named) continue;
if (decl.kind != .named or !decl.is_pub) continue;
const name_val = v: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
Expand Down Expand Up @@ -17554,8 +17554,6 @@ fn typeInfoNamespaceDecls(
const fields = .{
//name: []const u8,
name_val,
//is_pub: bool,
Value.makeBool(decl.is_pub).toIntern(),
};
try decl_vals.append(try mod.intern(.{ .aggregate = .{
.ty = declaration_ty.toIntern(),
Expand Down
1 change: 0 additions & 1 deletion src/codegen/llvm/Builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,6 @@ pub const Attribute = union(Kind) {
var any = false;
var remaining: Int = @bitCast(fpclass);
inline for (@typeInfo(FpClass).Struct.decls) |decl| {
if (!decl.is_pub) continue;
const pattern: Int = @bitCast(@field(FpClass, decl.name));
if (remaining & pattern == pattern) {
if (!any) {
Expand Down
13 changes: 5 additions & 8 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,11 @@ pub fn translate(
}

inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
if (decl.is_pub) {
const builtin = try Tag.pub_var_simple.create(arena, .{
.name = decl.name,
.init = try Tag.import_c_builtin.create(arena, decl.name),
});
try addTopLevelDecl(&context, decl.name, builtin);
}
const builtin = try Tag.pub_var_simple.create(arena, .{
.name = decl.name,
.init = try Tag.import_c_builtin.create(arena, decl.name),
});
try addTopLevelDecl(&context, decl.name, builtin);
}

try prepopulateGlobalNameTable(ast_unit, &context);
Expand Down Expand Up @@ -2120,7 +2118,6 @@ fn transImplicitCastExpr(

fn isBuiltinDefined(name: []const u8) bool {
inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
if (!decl.is_pub) continue;
if (std.mem.eql(u8, name, decl.name)) return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions test/behavior.zig
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ 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/typename.zig");
_ = @import("behavior/undefined.zig");
_ = @import("behavior/underscore.zig");
Expand Down
2 changes: 1 addition & 1 deletion test/behavior/eval.zig
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ test "debug variable type resolved through indirect zero-bit types" {
test "const local with comptime init through array init" {
const E1 = enum {
A,
fn a() void {}
pub fn a() void {}
};

const S = struct {
Expand Down
27 changes: 13 additions & 14 deletions test/behavior/type_info.zig
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ fn testPackedStruct() !void {
try expect(struct_info.Struct.fields[2].default_value == null);
try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4);
try expect(struct_info.Struct.fields[3].alignment == 0);
try expect(struct_info.Struct.decls.len == 2);
try expect(struct_info.Struct.decls[0].is_pub);
try expect(struct_info.Struct.decls.len == 1);
}

const TestPackedStruct = packed struct {
Expand All @@ -344,8 +343,8 @@ test "type info: opaque info" {

fn testOpaque() !void {
const Foo = opaque {
const A = 1;
fn b() void {}
pub const A = 1;
pub fn b() void {}
};

const foo_info = @typeInfo(Foo);
Expand Down Expand Up @@ -514,11 +513,11 @@ test "Declarations are returned in declaration order" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

const S = struct {
const a = 1;
const b = 2;
const c = 3;
const d = 4;
const e = 5;
pub const a = 1;
pub const b = 2;
pub const c = 3;
pub const d = 4;
pub const e = 5;
};
const d = @typeInfo(S).Struct.decls;
try expect(std.mem.eql(u8, d[0].name, "a"));
Expand Down Expand Up @@ -553,7 +552,7 @@ test "typeInfo resolves usingnamespace declarations" {
};

const B = struct {
const f0 = 42;
pub const f0 = 42;
usingnamespace A;
};

Expand All @@ -574,14 +573,14 @@ test "@typeInfo decls and usingnamespace" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

const A = struct {
const x = 5;
const y = 34;
pub const x = 5;
pub const y = 34;

comptime {}
};
const B = struct {
usingnamespace A;
const z = 56;
pub const z = 56;

test {}
};
Expand All @@ -594,7 +593,7 @@ test "@typeInfo decls and usingnamespace" {

test "@typeInfo decls ignore dependency loops" {
const S = struct {
fn Def(comptime T: type) type {
pub fn Def(comptime T: type) type {
std.debug.assert(@typeInfo(T).Struct.decls.len == 1);
return struct {
const foo = u32;
Expand Down
23 changes: 23 additions & 0 deletions test/behavior/type_info_only_pub_decls.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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);
}

0 comments on commit 3c08fe9

Please sign in to comment.