From 75bbc74d56f26da812c47436c6f5712d3cfde12d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 31 Jan 2022 22:25:49 -0700 Subject: [PATCH] a small crusade against std.meta.declarations --- lib/std/crypto.zig | 104 +++++++++++++++++++++++++++++++++++--------- lib/std/meta.zig | 2 + lib/std/target.zig | 2 +- lib/std/testing.zig | 2 +- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/lib/std/crypto.zig b/lib/std/crypto.zig index b13ba000cf..c52e207eeb 100644 --- a/lib/std/crypto.zig +++ b/lib/std/crypto.zig @@ -164,31 +164,93 @@ const std = @import("std.zig"); pub const errors = @import("crypto/errors.zig"); -test "crypto" { +test { const please_windows_dont_oom = @import("builtin").os.tag == .windows; if (please_windows_dont_oom) return error.SkipZigTest; - inline for (std.meta.declarations(@This())) |decl| { - switch (decl.data) { - .Type => |t| { - if (@typeInfo(t) != .ErrorSet) { - std.testing.refAllDecls(t); - } - }, - .Var => |v| { - _ = v; - }, - .Fn => |f| { - _ = f; - }, - } - } + _ = aead.aegis.Aegis128L; + _ = aead.aegis.Aegis256; - _ = @import("crypto/aegis.zig"); - _ = @import("crypto/aes_gcm.zig"); - _ = @import("crypto/aes_ocb.zig"); - _ = @import("crypto/blake2.zig"); - _ = @import("crypto/chacha20.zig"); + _ = aead.aes_gcm.Aes128Gcm; + _ = aead.aes_gcm.Aes256Gcm; + + _ = aead.aes_ocb.Aes128Ocb; + _ = aead.aes_ocb.Aes256Ocb; + + _ = aead.Gimli; + + _ = aead.chacha_poly.ChaCha20Poly1305; + _ = aead.chacha_poly.ChaCha12Poly1305; + _ = aead.chacha_poly.ChaCha8Poly1305; + _ = aead.chacha_poly.XChaCha20Poly1305; + _ = aead.chacha_poly.XChaCha12Poly1305; + _ = aead.chacha_poly.XChaCha8Poly1305; + + _ = aead.isap; + _ = aead.salsa_poly.XSalsa20Poly1305; + + _ = auth.hmac; + _ = auth.siphash; + + _ = core.aes; + _ = core.Gimli; + _ = core.modes; + + _ = dh.X25519; + + _ = ecc.Curve25519; + _ = ecc.Edwards25519; + _ = ecc.P256; + _ = ecc.Ristretto255; + + _ = hash.blake2; + _ = hash.Blake3; + _ = hash.Gimli; + _ = hash.Md5; + _ = hash.Sha1; + _ = hash.sha2; + _ = hash.sha3; + + _ = kdf.hkdf; + + _ = onetimeauth.Ghash; + _ = onetimeauth.Poly1305; + + _ = pwhash.Encoding; + + _ = pwhash.Error; + _ = pwhash.HasherError; + _ = pwhash.KdfError; + + _ = pwhash.argon2; + _ = pwhash.bcrypt; + _ = pwhash.scrypt; + _ = pwhash.pbkdf2; + + _ = pwhash.phc_format; + + _ = sign.Ed25519; + + _ = stream.chacha.ChaCha20IETF; + _ = stream.chacha.ChaCha12IETF; + _ = stream.chacha.ChaCha8IETF; + _ = stream.chacha.ChaCha20With64BitNonce; + _ = stream.chacha.ChaCha12With64BitNonce; + _ = stream.chacha.ChaCha8With64BitNonce; + _ = stream.chacha.XChaCha20IETF; + _ = stream.chacha.XChaCha12IETF; + _ = stream.chacha.XChaCha8IETF; + + _ = stream.salsa.Salsa20; + _ = stream.salsa.XSalsa20; + + _ = nacl.Box; + _ = nacl.SecretBox; + _ = nacl.SealedBox; + + _ = utils; + _ = random; + _ = errors; } test "CSPRNG" { diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 8e270654b8..72a073fbfb 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -361,6 +361,8 @@ test "std.meta.containerLayout" { try testing.expect(containerLayout(U3) == .Extern); } +/// Instead of this function, prefer to use e.g. `@TypeInfo(foo).Struct.decls` +/// directly when you know what kind of type it is. pub fn declarations(comptime T: type) []const TypeInfo.Declaration { return switch (@typeInfo(T)) { .Struct => |info| info.decls, diff --git a/lib/std/target.zig b/lib/std/target.zig index 5848bfeb6f..182690484e 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1236,7 +1236,7 @@ pub const Target = struct { } fn allCpusFromDecls(comptime cpus: type) []const *const Cpu.Model { - const decls = std.meta.declarations(cpus); + const decls = @typeInfo(cpus).Struct.decls; var array: [decls.len]*const Cpu.Model = undefined; for (decls) |decl, i| { array[i] = &@field(cpus, decl.name); diff --git a/lib/std/testing.zig b/lib/std/testing.zig index a09d448e0a..a9874d4df1 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -465,7 +465,7 @@ test { /// Given a type, reference all the declarations inside, so that the semantic analyzer sees them. pub fn refAllDecls(comptime T: type) void { if (!builtin.is_test) return; - inline for (std.meta.declarations(T)) |decl| { + inline for (comptime std.meta.declarations(T)) |decl| { _ = decl; } }