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) {...@@ -437,7 +437,6 @@ pub const Type = union(enum) {
437 /// therefore must be kept in sync with the compiler implementation.437 /// therefore must be kept in sync with the compiler implementation.
438 pub const Declaration = struct {438 pub const Declaration = struct {
439 name: []const u8,439 name: []const u8,
440 is_pub: bool,
441 };440 };
442};441};
443442
lib/std/meta.zig+14-16
...@@ -293,18 +293,18 @@ test "std.meta.declarations" {...@@ -293,18 +293,18 @@ test "std.meta.declarations" {
293 const E1 = enum {293 const E1 = enum {
294 A,294 A,
295295
296 fn a() void {}296 pub fn a() void {}
297 };297 };
298 const S1 = struct {298 const S1 = struct {
299 fn a() void {}299 pub fn a() void {}
300 };300 };
301 const U1 = union {301 const U1 = union {
302 a: u8,302 a: u8,
303303
304 fn a() void {}304 pub fn a() void {}
305 };305 };
306 const O1 = opaque {306 const O1 = opaque {
307 fn a() void {}307 pub fn a() void {}
308 };308 };
309309
310 const decls = comptime [_][]const Type.Declaration{310 const decls = comptime [_][]const Type.Declaration{
...@@ -333,15 +333,15 @@ test "std.meta.declarationInfo" {...@@ -333,15 +333,15 @@ test "std.meta.declarationInfo" {
333 const E1 = enum {333 const E1 = enum {
334 A,334 A,
335335
336 fn a() void {}336 pub fn a() void {}
337 };337 };
338 const S1 = struct {338 const S1 = struct {
339 fn a() void {}339 pub fn a() void {}
340 };340 };
341 const U1 = union {341 const U1 = union {
342 a: u8,342 a: u8,
343343
344 fn a() void {}344 pub fn a() void {}
345 };345 };
346346
347 const infos = comptime [_]Type.Declaration{347 const infos = comptime [_]Type.Declaration{
...@@ -352,7 +352,6 @@ test "std.meta.declarationInfo" {...@@ -352,7 +352,6 @@ test "std.meta.declarationInfo" {
352352
353 inline for (infos) |info| {353 inline for (infos) |info| {
354 try testing.expect(comptime mem.eql(u8, info.name, "a"));354 try testing.expect(comptime mem.eql(u8, info.name, "a"));
355 try testing.expect(!info.is_pub);
356 }355 }
357}356}
358pub fn fields(comptime T: type) switch (@typeInfo(T)) {357pub fn fields(comptime T: type) switch (@typeInfo(T)) {
...@@ -597,7 +596,6 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {...@@ -597,7 +596,6 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
597 if (expected_decls.len != actual_decls.len) return error.FailedTest;596 if (expected_decls.len != actual_decls.len) return error.FailedTest;
598 for (expected_decls, 0..) |expected_decl, i| {597 for (expected_decls, 0..) |expected_decl, i| {
599 const actual_decl = actual_decls[i];598 const actual_decl = actual_decls[i];
600 try testing.expectEqual(expected_decl.is_pub, actual_decl.is_pub);
601 try testing.expectEqualStrings(expected_decl.name, actual_decl.name);599 try testing.expectEqualStrings(expected_decl.name, actual_decl.name);
602 }600 }
603 }601 }
...@@ -644,21 +642,21 @@ pub fn DeclEnum(comptime T: type) type {...@@ -644,21 +642,21 @@ pub fn DeclEnum(comptime T: type) type {
644642
645test "std.meta.DeclEnum" {643test "std.meta.DeclEnum" {
646 const A = struct {644 const A = struct {
647 const a: u8 = 0;645 pub const a: u8 = 0;
648 };646 };
649 const B = union {647 const B = union {
650 foo: void,648 foo: void,
651649
652 const a: u8 = 0;650 pub const a: u8 = 0;
653 const b: void = {};651 pub const b: void = {};
654 const c: f32 = 0;652 pub const c: f32 = 0;
655 };653 };
656 const C = enum {654 const C = enum {
657 bar,655 bar,
658656
659 const a: u8 = 0;657 pub const a: u8 = 0;
660 const b: void = {};658 pub const b: void = {};
661 const c: f32 = 0;659 pub const c: f32 = 0;
662 };660 };
663 try expectEqualEnum(enum { a }, DeclEnum(A));661 try expectEqualEnum(enum { a }, DeclEnum(A));
664 try expectEqualEnum(enum { a, b, c }, DeclEnum(B));662 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...@@ -1122,7 +1122,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
1122pub fn refAllDecls(comptime T: type) void {1122pub fn refAllDecls(comptime T: type) void {
1123 if (!builtin.is_test) return;1123 if (!builtin.is_test) return;
1124 inline for (comptime std.meta.declarations(T)) |decl| {1124 inline for (comptime std.meta.declarations(T)) |decl| {
1125 if (decl.is_pub) _ = &@field(T, decl.name);1125 _ = &@field(T, decl.name);
1126 }1126 }
1127}1127}
11281128
...@@ -1131,14 +1131,12 @@ pub fn refAllDecls(comptime T: type) void {...@@ -1131,14 +1131,12 @@ pub fn refAllDecls(comptime T: type) void {
1131pub fn refAllDeclsRecursive(comptime T: type) void {1131pub fn refAllDeclsRecursive(comptime T: type) void {
1132 if (!builtin.is_test) return;1132 if (!builtin.is_test) return;
1133 inline for (comptime std.meta.declarations(T)) |decl| {1133 inline for (comptime std.meta.declarations(T)) |decl| {
1134 if (decl.is_pub) {1134 if (@TypeOf(@field(T, decl.name)) == type) {
1135 if (@TypeOf(@field(T, decl.name)) == type) {1135 switch (@typeInfo(@field(T, decl.name))) {
1136 switch (@typeInfo(@field(T, decl.name))) {1136 .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
1137 .Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),1137 else => {},
1138 else => {},
1139 }
1140 }1138 }
1141 _ = &@field(T, decl.name);
1142 }1139 }
1140 _ = &@field(T, decl.name);
1143 }1141 }
1144}1142}
src/Sema.zig+1-3
...@@ -17526,7 +17526,7 @@ fn typeInfoNamespaceDecls(...@@ -17526,7 +17526,7 @@ fn typeInfoNamespaceDecls(
17526 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);17526 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
17527 continue;17527 continue;
17528 }17528 }
17529 if (decl.kind != .named) continue;17529 if (decl.kind != .named or !decl.is_pub) continue;
17530 const name_val = v: {17530 const name_val = v: {
17531 var anon_decl = try block.startAnonDecl();17531 var anon_decl = try block.startAnonDecl();
17532 defer anon_decl.deinit();17532 defer anon_decl.deinit();
...@@ -17554,8 +17554,6 @@ fn typeInfoNamespaceDecls(...@@ -17554,8 +17554,6 @@ fn typeInfoNamespaceDecls(
17554 const fields = .{17554 const fields = .{
17555 //name: []const u8,17555 //name: []const u8,
17556 name_val,17556 name_val,
17557 //is_pub: bool,
17558 Value.makeBool(decl.is_pub).toIntern(),
17559 };17557 };
17560 try decl_vals.append(try mod.intern(.{ .aggregate = .{17558 try decl_vals.append(try mod.intern(.{ .aggregate = .{
17561 .ty = declaration_ty.toIntern(),17559 .ty = declaration_ty.toIntern(),
src/codegen/llvm/Builder.zig-1
...@@ -1159,7 +1159,6 @@ pub const Attribute = union(Kind) {...@@ -1159,7 +1159,6 @@ pub const Attribute = union(Kind) {
1159 var any = false;1159 var any = false;
1160 var remaining: Int = @bitCast(fpclass);1160 var remaining: Int = @bitCast(fpclass);
1161 inline for (@typeInfo(FpClass).Struct.decls) |decl| {1161 inline for (@typeInfo(FpClass).Struct.decls) |decl| {
1162 if (!decl.is_pub) continue;
1163 const pattern: Int = @bitCast(@field(FpClass, decl.name));1162 const pattern: Int = @bitCast(@field(FpClass, decl.name));
1164 if (remaining & pattern == pattern) {1163 if (remaining & pattern == pattern) {
1165 if (!any) {1164 if (!any) {
src/translate_c.zig+5-8
...@@ -408,13 +408,11 @@ pub fn translate(...@@ -408,13 +408,11 @@ pub fn translate(
408 }408 }
409409
410 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {410 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
411 if (decl.is_pub) {411 const builtin = try Tag.pub_var_simple.create(arena, .{
412 const builtin = try Tag.pub_var_simple.create(arena, .{412 .name = decl.name,
413 .name = decl.name,413 .init = try Tag.import_c_builtin.create(arena, decl.name),
414 .init = try Tag.import_c_builtin.create(arena, decl.name),414 });
415 });415 try addTopLevelDecl(&context, decl.name, builtin);
416 try addTopLevelDecl(&context, decl.name, builtin);
417 }
418 }416 }
419417
420 try prepopulateGlobalNameTable(ast_unit, &context);418 try prepopulateGlobalNameTable(ast_unit, &context);
...@@ -2120,7 +2118,6 @@ fn transImplicitCastExpr(...@@ -2120,7 +2118,6 @@ fn transImplicitCastExpr(
21202118
2121fn isBuiltinDefined(name: []const u8) bool {2119fn isBuiltinDefined(name: []const u8) bool {
2122 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {2120 inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
2123 if (!decl.is_pub) continue;
2124 if (std.mem.eql(u8, name, decl.name)) return true;2121 if (std.mem.eql(u8, name, decl.name)) return true;
2125 }2122 }
2126 return false;2123 return false;
test/behavior.zig+1
...@@ -222,6 +222,7 @@ test {...@@ -222,6 +222,7 @@ test {
222 _ = @import("behavior/tuple_declarations.zig");222 _ = @import("behavior/tuple_declarations.zig");
223 _ = @import("behavior/type.zig");223 _ = @import("behavior/type.zig");
224 _ = @import("behavior/type_info.zig");224 _ = @import("behavior/type_info.zig");
225 _ = @import("behavior/type_info_only_pub_decls.zig");
225 _ = @import("behavior/typename.zig");226 _ = @import("behavior/typename.zig");
226 _ = @import("behavior/undefined.zig");227 _ = @import("behavior/undefined.zig");
227 _ = @import("behavior/underscore.zig");228 _ = @import("behavior/underscore.zig");
test/behavior/eval.zig+1-1
...@@ -959,7 +959,7 @@ test "debug variable type resolved through indirect zero-bit types" {...@@ -959,7 +959,7 @@ test "debug variable type resolved through indirect zero-bit types" {
959test "const local with comptime init through array init" {959test "const local with comptime init through array init" {
960 const E1 = enum {960 const E1 = enum {
961 A,961 A,
962 fn a() void {}962 pub fn a() void {}
963 };963 };
964964
965 const S = struct {965 const S = struct {
test/behavior/type_info.zig+13-14
...@@ -321,8 +321,7 @@ fn testPackedStruct() !void {...@@ -321,8 +321,7 @@ fn testPackedStruct() !void {
321 try expect(struct_info.Struct.fields[2].default_value == null);321 try expect(struct_info.Struct.fields[2].default_value == null);
322 try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4);322 try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4);
323 try expect(struct_info.Struct.fields[3].alignment == 0);323 try expect(struct_info.Struct.fields[3].alignment == 0);
324 try expect(struct_info.Struct.decls.len == 2);324 try expect(struct_info.Struct.decls.len == 1);
325 try expect(struct_info.Struct.decls[0].is_pub);
326}325}
327326
328const TestPackedStruct = packed struct {327const TestPackedStruct = packed struct {
...@@ -344,8 +343,8 @@ test "type info: opaque info" {...@@ -344,8 +343,8 @@ test "type info: opaque info" {
344343
345fn testOpaque() !void {344fn testOpaque() !void {
346 const Foo = opaque {345 const Foo = opaque {
347 const A = 1;346 pub const A = 1;
348 fn b() void {}347 pub fn b() void {}
349 };348 };
350349
351 const foo_info = @typeInfo(Foo);350 const foo_info = @typeInfo(Foo);
...@@ -514,11 +513,11 @@ test "Declarations are returned in declaration order" {...@@ -514,11 +513,11 @@ test "Declarations are returned in declaration order" {
514 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO513 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
515514
516 const S = struct {515 const S = struct {
517 const a = 1;516 pub const a = 1;
518 const b = 2;517 pub const b = 2;
519 const c = 3;518 pub const c = 3;
520 const d = 4;519 pub const d = 4;
521 const e = 5;520 pub const e = 5;
522 };521 };
523 const d = @typeInfo(S).Struct.decls;522 const d = @typeInfo(S).Struct.decls;
524 try expect(std.mem.eql(u8, d[0].name, "a"));523 try expect(std.mem.eql(u8, d[0].name, "a"));
...@@ -553,7 +552,7 @@ test "typeInfo resolves usingnamespace declarations" {...@@ -553,7 +552,7 @@ test "typeInfo resolves usingnamespace declarations" {
553 };552 };
554553
555 const B = struct {554 const B = struct {
556 const f0 = 42;555 pub const f0 = 42;
557 usingnamespace A;556 usingnamespace A;
558 };557 };
559558
...@@ -574,14 +573,14 @@ test "@typeInfo decls and usingnamespace" {...@@ -574,14 +573,14 @@ test "@typeInfo decls and usingnamespace" {
574 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;573 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
575574
576 const A = struct {575 const A = struct {
577 const x = 5;576 pub const x = 5;
578 const y = 34;577 pub const y = 34;
579578
580 comptime {}579 comptime {}
581 };580 };
582 const B = struct {581 const B = struct {
583 usingnamespace A;582 usingnamespace A;
584 const z = 56;583 pub const z = 56;
585584
586 test {}585 test {}
587 };586 };
...@@ -594,7 +593,7 @@ test "@typeInfo decls and usingnamespace" {...@@ -594,7 +593,7 @@ test "@typeInfo decls and usingnamespace" {
594593
595test "@typeInfo decls ignore dependency loops" {594test "@typeInfo decls ignore dependency loops" {
596 const S = struct {595 const S = struct {
597 fn Def(comptime T: type) type {596 pub fn Def(comptime T: type) type {
598 std.debug.assert(@typeInfo(T).Struct.decls.len == 1);597 std.debug.assert(@typeInfo(T).Struct.decls.len == 1);
599 return struct {598 return struct {
600 const foo = u32;599 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}