authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2023-07-25 11:15:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-25 16:19:08-07:00
log3c08fe931a10618950c6af9e89226d1d9b20bbb9
treee745ce6e163c0af808adc8322a6c83e5ba426641
parent972e70b7941561b10f9b3062dcd7b7a91016c546

make `@typeInfo` not return private decls

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`

10 files changed, 64 insertions(+), 52 deletions(-)

lib/std/builtin.zig-1
......@@ -437,7 +437,6 @@ pub const Type = union(enum) {
437437 /// therefore must be kept in sync with the compiler implementation.
438438 pub const Declaration = struct {
439439 name: []const u8,
440 is_pub: bool,
441440 };
442441};
443442
lib/std/meta.zig+14-16
......@@ -293,18 +293,18 @@ test "std.meta.declarations" {
293293 const E1 = enum {
294294 A,
295295
296 fn a() void {}
296 pub fn a() void {}
297297 };
298298 const S1 = struct {
299 fn a() void {}
299 pub fn a() void {}
300300 };
301301 const U1 = union {
302302 a: u8,
303303
304 fn a() void {}
304 pub fn a() void {}
305305 };
306306 const O1 = opaque {
307 fn a() void {}
307 pub fn a() void {}
308308 };
309309
310310 const decls = comptime [_][]const Type.Declaration{
......@@ -333,15 +333,15 @@ test "std.meta.declarationInfo" {
333333 const E1 = enum {
334334 A,
335335
336 fn a() void {}
336 pub fn a() void {}
337337 };
338338 const S1 = struct {
339 fn a() void {}
339 pub fn a() void {}
340340 };
341341 const U1 = union {
342342 a: u8,
343343
344 fn a() void {}
344 pub fn a() void {}
345345 };
346346
347347 const infos = comptime [_]Type.Declaration{
......@@ -352,7 +352,6 @@ test "std.meta.declarationInfo" {
352352
353353 inline for (infos) |info| {
354354 try testing.expect(comptime mem.eql(u8, info.name, "a"));
355 try testing.expect(!info.is_pub);
356355 }
357356}
358357pub fn fields(comptime T: type) switch (@typeInfo(T)) {
......@@ -597,7 +596,6 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
597596 if (expected_decls.len != actual_decls.len) return error.FailedTest;
598597 for (expected_decls, 0..) |expected_decl, i| {
599598 const actual_decl = actual_decls[i];
600 try testing.expectEqual(expected_decl.is_pub, actual_decl.is_pub);
601599 try testing.expectEqualStrings(expected_decl.name, actual_decl.name);
602600 }
603601 }
......@@ -644,21 +642,21 @@ pub fn DeclEnum(comptime T: type) type {
644642
645643test "std.meta.DeclEnum" {
646644 const A = struct {
647 const a: u8 = 0;
645 pub const a: u8 = 0;
648646 };
649647 const B = union {
650648 foo: void,
651649
652 const a: u8 = 0;
653 const b: void = {};
654 const c: f32 = 0;
650 pub const a: u8 = 0;
651 pub const b: void = {};
652 pub const c: f32 = 0;
655653 };
656654 const C = enum {
657655 bar,
658656
659 const a: u8 = 0;
660 const b: void = {};
661 const c: f32 = 0;
657 pub const a: u8 = 0;
658 pub const b: void = {};
659 pub const c: f32 = 0;
662660 };
663661 try expectEqualEnum(enum { a }, DeclEnum(A));
664662 try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
lib/std/testing.zig+6-8
......@@ -1122,7 +1122,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
11221122pub fn refAllDecls(comptime T: type) void {
11231123 if (!builtin.is_test) return;
11241124 inline for (comptime std.meta.declarations(T)) |decl| {
1125 if (decl.is_pub) _ = &@field(T, decl.name);
1125 _ = &@field(T, decl.name);
11261126 }
11271127}
11281128
......@@ -1131,14 +1131,12 @@ pub fn refAllDecls(comptime T: type) void {
11311131pub fn refAllDeclsRecursive(comptime T: type) void {
11321132 if (!builtin.is_test) return;
11331133 inline for (comptime std.meta.declarations(T)) |decl| {
1134 if (decl.is_pub) {
1135 if (@TypeOf(@field(T, decl.name)) == type) {
1136 switch (@typeInfo(@field(T, decl.name))) {
1137 .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
1138 else => {},
1139 }
1134 if (@TypeOf(@field(T, decl.name)) == type) {
1135 switch (@typeInfo(@field(T, decl.name))) {
1136 .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
1137 else => {},
11401138 }
1141 _ = &@field(T, decl.name);
11421139 }
1140 _ = &@field(T, decl.name);
11431141 }
11441142}
src/Sema.zig+1-3
......@@ -17526,7 +17526,7 @@ fn typeInfoNamespaceDecls(
1752617526 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
1752717527 continue;
1752817528 }
17529 if (decl.kind != .named) continue;
17529 if (decl.kind != .named or !decl.is_pub) continue;
1753017530 const name_val = v: {
1753117531 var anon_decl = try block.startAnonDecl();
1753217532 defer anon_decl.deinit();
......@@ -17554,8 +17554,6 @@ fn typeInfoNamespaceDecls(
1755417554 const fields = .{
1755517555 //name: []const u8,
1755617556 name_val,
17557 //is_pub: bool,
17558 Value.makeBool(decl.is_pub).toIntern(),
1755917557 };
1756017558 try decl_vals.append(try mod.intern(.{ .aggregate = .{
1756117559 .ty = declaration_ty.toIntern(),
src/codegen/llvm/Builder.zig-1
......@@ -1159,7 +1159,6 @@ pub const Attribute = union(Kind) {
11591159 var any = false;
11601160 var remaining: Int = @bitCast(fpclass);
11611161 inline for (@typeInfo(FpClass).Struct.decls) |decl| {
1162 if (!decl.is_pub) continue;
11631162 const pattern: Int = @bitCast(@field(FpClass, decl.name));
11641163 if (remaining & pattern == pattern) {
11651164 if (!any) {
src/translate_c.zig+5-8
......@@ -408,13 +408,11 @@ pub fn translate(
408408 }
409409
410410 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
411 if (decl.is_pub) {
412 const builtin = try Tag.pub_var_simple.create(arena, .{
413 .name = decl.name,
414 .init = try Tag.import_c_builtin.create(arena, decl.name),
415 });
416 try addTopLevelDecl(&context, decl.name, builtin);
417 }
411 const builtin = try Tag.pub_var_simple.create(arena, .{
412 .name = decl.name,
413 .init = try Tag.import_c_builtin.create(arena, decl.name),
414 });
415 try addTopLevelDecl(&context, decl.name, builtin);
418416 }
419417
420418 try prepopulateGlobalNameTable(ast_unit, &context);
......@@ -2120,7 +2118,6 @@ fn transImplicitCastExpr(
21202118
21212119fn isBuiltinDefined(name: []const u8) bool {
21222120 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
2123 if (!decl.is_pub) continue;
21242121 if (std.mem.eql(u8, name, decl.name)) return true;
21252122 }
21262123 return false;
test/behavior.zig+1
......@@ -222,6 +222,7 @@ test {
222222 _ = @import("behavior/tuple_declarations.zig");
223223 _ = @import("behavior/type.zig");
224224 _ = @import("behavior/type_info.zig");
225 _ = @import("behavior/type_info_only_pub_decls.zig");
225226 _ = @import("behavior/typename.zig");
226227 _ = @import("behavior/undefined.zig");
227228 _ = @import("behavior/underscore.zig");
test/behavior/eval.zig+1-1
......@@ -959,7 +959,7 @@ test "debug variable type resolved through indirect zero-bit types" {
959959test "const local with comptime init through array init" {
960960 const E1 = enum {
961961 A,
962 fn a() void {}
962 pub fn a() void {}
963963 };
964964
965965 const S = struct {
test/behavior/type_info.zig+13-14
......@@ -321,8 +321,7 @@ fn testPackedStruct() !void {
321321 try expect(struct_info.Struct.fields[2].default_value == null);
322322 try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4);
323323 try expect(struct_info.Struct.fields[3].alignment == 0);
324 try expect(struct_info.Struct.decls.len == 2);
325 try expect(struct_info.Struct.decls[0].is_pub);
324 try expect(struct_info.Struct.decls.len == 1);
326325}
327326
328327const TestPackedStruct = packed struct {
......@@ -344,8 +343,8 @@ test "type info: opaque info" {
344343
345344fn testOpaque() !void {
346345 const Foo = opaque {
347 const A = 1;
348 fn b() void {}
346 pub const A = 1;
347 pub fn b() void {}
349348 };
350349
351350 const foo_info = @typeInfo(Foo);
......@@ -514,11 +513,11 @@ test "Declarations are returned in declaration order" {
514513 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
515514
516515 const S = struct {
517 const a = 1;
518 const b = 2;
519 const c = 3;
520 const d = 4;
521 const e = 5;
516 pub const a = 1;
517 pub const b = 2;
518 pub const c = 3;
519 pub const d = 4;
520 pub const e = 5;
522521 };
523522 const d = @typeInfo(S).Struct.decls;
524523 try expect(std.mem.eql(u8, d[0].name, "a"));
......@@ -553,7 +552,7 @@ test "typeInfo resolves usingnamespace declarations" {
553552 };
554553
555554 const B = struct {
556 const f0 = 42;
555 pub const f0 = 42;
557556 usingnamespace A;
558557 };
559558
......@@ -574,14 +573,14 @@ test "@typeInfo decls and usingnamespace" {
574573 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
575574
576575 const A = struct {
577 const x = 5;
578 const y = 34;
576 pub const x = 5;
577 pub const y = 34;
579578
580579 comptime {}
581580 };
582581 const B = struct {
583582 usingnamespace A;
584 const z = 56;
583 pub const z = 56;
585584
586585 test {}
587586 };
......@@ -594,7 +593,7 @@ test "@typeInfo decls and usingnamespace" {
594593
595594test "@typeInfo decls ignore dependency loops" {
596595 const S = struct {
597 fn Def(comptime T: type) type {
596 pub fn Def(comptime T: type) type {
598597 std.debug.assert(@typeInfo(T).Struct.decls.len == 1);
599598 return struct {
600599 const foo = u32;
test/behavior/type_info_only_pub_decls.zig created+23
......@@ -0,0 +1,23 @@
1const std = @import("std");
2const other = struct {
3 const std = @import("std");
4
5 pub const Enum = enum {
6 a,
7 b,
8 c,
9 };
10
11 pub const Struct = struct {
12 foo: i32,
13 };
14};
15
16test {
17 const ti = @typeInfo(other);
18 const decls = ti.Struct.decls;
19
20 try std.testing.expectEqual(2, decls.len);
21 try std.testing.expectEqualStrings("Enum", decls[0].name);
22 try std.testing.expectEqualStrings("Struct", decls[1].name);
23}