From fc5ae1c4096fa814b97671525db17c0b4f0b786d Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 21 Jan 2021 09:48:57 +0100 Subject: [PATCH 1/3] stage1: don't filter test blocks with empty label The common pattern of including a file containing all the tests in a empty-label test block breaks down when using --test-filter. --- src/stage1/analyze.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 5f86321e9d..a98bd28bb6 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -3875,7 +3875,8 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope Buf *test_name = g->test_name_prefix ? buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf; - if (g->test_filter != nullptr && strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) { + if (g->test_filter != nullptr && buf_len(test_name) > 0 && + strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) { return; } From ac004e1bf13d00804c30f55d479b554774798307 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 22 Jan 2021 15:18:39 +0100 Subject: [PATCH 2/3] stage1: Allow nameless test blocks Nameless blocks are never filtered, the test prefix is still applied. --- lib/std/zig/ast.zig | 2 +- lib/std/zig/parse.zig | 4 +--- lib/std/zig/render.zig | 3 ++- src/stage1/all_types.hpp | 1 + src/stage1/analyze.cpp | 15 ++++++++++----- src/stage1/parser.cpp | 4 ++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index eb894fa1f6..b7975fc0f7 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -3231,7 +3231,7 @@ pub const Node = struct { base: Node = Node{ .tag = .TestDecl }, doc_comments: ?*DocComment, test_token: TokenIndex, - name: *Node, + name: ?*Node, body_node: *Node, pub fn iterate(self: *const TestDecl, index: usize) ?*Node { diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 6d9b8ff5c6..5dd27cbcb3 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -366,9 +366,7 @@ const Parser = struct { /// TestDecl <- KEYWORD_test STRINGLITERALSINGLE Block fn parseTestDecl(p: *Parser) !?*Node { const test_token = p.eatToken(.Keyword_test) orelse return null; - const name_node = try p.expectNode(parseStringLiteralSingle, .{ - .ExpectedStringLiteral = .{ .token = p.tok_i }, - }); + const name_node = try p.parseStringLiteralSingle(); const block_node = (try p.parseBlock(null)) orelse { try p.errors.append(p.gpa, .{ .ExpectedLBrace = .{ .token = p.tok_i } }); return error.ParseError; diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 3cced0dd6b..0fbd6890fc 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -228,7 +228,8 @@ fn renderContainerDecl(allocator: *mem.Allocator, ais: anytype, tree: *ast.Tree, try renderDocComments(tree, ais, test_decl, test_decl.doc_comments); try renderToken(tree, ais, test_decl.test_token, .Space); - try renderExpression(allocator, ais, tree, test_decl.name, .Space); + if (test_decl.name) |name| + try renderExpression(allocator, ais, tree, name, .Space); try renderExpression(allocator, ais, tree, test_decl.body_node, space); }, diff --git a/src/stage1/all_types.hpp b/src/stage1/all_types.hpp index c235bb362d..88e47791ab 100644 --- a/src/stage1/all_types.hpp +++ b/src/stage1/all_types.hpp @@ -797,6 +797,7 @@ struct AstNodeVariableDeclaration { }; struct AstNodeTestDecl { + // nullptr if the test declaration has no name Buf *name; AstNode *body; diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index a98bd28bb6..faf66f59f3 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -3871,13 +3871,18 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope return; Buf *decl_name_buf = node->data.test_decl.name; + Buf *test_name; - Buf *test_name = g->test_name_prefix ? - buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf; + if (decl_name_buf != nullptr) { + test_name = g->test_name_prefix ? + buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf; - if (g->test_filter != nullptr && buf_len(test_name) > 0 && - strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) { - return; + if (g->test_filter != nullptr && strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) { + return; + } + } else { + // Unnamed test blocks are always executed. + test_name = buf_sprintf("%s", g->test_name_prefix ? buf_ptr(g->test_name_prefix) : ""); } TldFn *tld_fn = heap::c_allocator.create(); diff --git a/src/stage1/parser.cpp b/src/stage1/parser.cpp index 3027c7ae47..3fe85adbf5 100644 --- a/src/stage1/parser.cpp +++ b/src/stage1/parser.cpp @@ -658,10 +658,10 @@ static AstNode *ast_parse_test_decl(ParseContext *pc) { if (test == nullptr) return nullptr; - Token *name = expect_token(pc, TokenIdStringLiteral); + Token *name = eat_token_if(pc, TokenIdStringLiteral); AstNode *block = ast_expect(pc, ast_parse_block); AstNode *res = ast_create_node(pc, NodeTypeTestDecl, test); - res->data.test_decl.name = token_buf(name); + res->data.test_decl.name = name ? token_buf(name) : nullptr; res->data.test_decl.body = block; return res; } From 134f5fd3d690cfdce2ab7d622cb233b3e510bf3a Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 22 Jan 2021 15:45:28 +0100 Subject: [PATCH 3/3] std: Update `test ""` to `test` where it makes sense --- lib/std/Thread.zig | 2 +- lib/std/build.zig | 2 +- lib/std/build/emit_raw.zig | 2 +- lib/std/c.zig | 2 +- lib/std/compress.zig | 2 +- lib/std/fs.zig | 2 +- lib/std/io.zig | 2 +- lib/std/io/c_writer.zig | 2 +- lib/std/math.zig | 2 +- lib/std/math/big.zig | 2 +- lib/std/math/big/int.zig | 2 +- lib/std/net.zig | 2 +- lib/std/os.zig | 2 +- lib/std/os/linux.zig | 2 +- lib/std/rand.zig | 2 +- lib/std/special/compiler_rt/shift.zig | 2 +- lib/std/special/compiler_rt/sparc.zig | 1 - lib/std/std.zig | 2 +- lib/std/target.zig | 2 +- lib/std/testing.zig | 2 +- lib/std/valgrind.zig | 2 +- lib/std/zig.zig | 2 +- lib/std/zig/system.zig | 2 +- src/codegen/aarch64.zig | 2 +- 24 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index 82111c5759..ea878bbdb0 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -553,7 +553,7 @@ pub fn getCurrentThreadId() u64 { } } -test "" { +test { if (!builtin.single_threaded) { std.testing.refAllDecls(@This()); } diff --git a/lib/std/build.zig b/lib/std/build.zig index 381488d800..3e6cf7a981 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -2985,7 +2985,7 @@ test "LibExeObjStep.addPackage" { std.testing.expectEqualStrings(pkg_top.name, dupe.name); } -test "" { +test { // The only purpose of this test is to get all these untested functions // to be referenced to avoid regression so it is okay to skip some targets. if (comptime std.Target.current.cpu.arch.ptrBitWidth() == 64) { diff --git a/lib/std/build/emit_raw.zig b/lib/std/build/emit_raw.zig index 3d2c6124c1..721b38b7a2 100644 --- a/lib/std/build/emit_raw.zig +++ b/lib/std/build/emit_raw.zig @@ -223,6 +223,6 @@ pub const InstallRawStep = struct { } }; -test "" { +test { std.testing.refAllDecls(InstallRawStep); } diff --git a/lib/std/c.zig b/lib/std/c.zig index 6b389c23ef..b5ee8cd893 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -14,7 +14,7 @@ pub const parse = @import("c/parse.zig").parse; pub const ast = @import("c/ast.zig"); pub const builtins = @import("c/builtins.zig"); -test "" { +test { _ = tokenizer; } diff --git a/lib/std/compress.zig b/lib/std/compress.zig index e7971fae8f..972031c182 100644 --- a/lib/std/compress.zig +++ b/lib/std/compress.zig @@ -9,7 +9,7 @@ pub const deflate = @import("compress/deflate.zig"); pub const gzip = @import("compress/gzip.zig"); pub const zlib = @import("compress/zlib.zig"); -test "" { +test { _ = gzip; _ = zlib; } diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 89984cda07..17c0cb7b1d 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -2475,7 +2475,7 @@ fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { } } -test "" { +test { if (builtin.os.tag != .wasi) { _ = makeDirAbsolute; _ = makeDirAbsoluteZ; diff --git a/lib/std/io.zig b/lib/std/io.zig index e9a03445f6..240faaa452 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -171,7 +171,7 @@ test "null_writer" { null_writer.writeAll("yay" ** 10) catch |err| switch (err) {}; } -test "" { +test { _ = @import("io/bit_reader.zig"); _ = @import("io/bit_writer.zig"); _ = @import("io/buffered_atomic_file.zig"); diff --git a/lib/std/io/c_writer.zig b/lib/std/io/c_writer.zig index ec8718e381..fa7d7eb13a 100644 --- a/lib/std/io/c_writer.zig +++ b/lib/std/io/c_writer.zig @@ -34,7 +34,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u } } -test "" { +test { if (!builtin.link_libc) return error.SkipZigTest; const filename = "tmp_io_test_file.txt"; diff --git a/lib/std/math.zig b/lib/std/math.zig index 77eed37304..de243135a1 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -278,7 +278,7 @@ pub const Complex = complex.Complex; pub const big = @import("math/big.zig"); -test "" { +test { std.testing.refAllDecls(@This()); } diff --git a/lib/std/math/big.zig b/lib/std/math/big.zig index 80649f867c..8ae214c666 100644 --- a/lib/std/math/big.zig +++ b/lib/std/math/big.zig @@ -20,7 +20,7 @@ comptime { assert(limb_info.signedness == .unsigned); } -test "" { +test { _ = int; _ = Rational; _ = Limb; diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 504083dcce..3cd72dd8e4 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -2344,6 +2344,6 @@ fn fixedIntFromSignedDoubleLimb(A: SignedDoubleLimb, storage: []Limb) Mutable { }; } -test "" { +test { _ = @import("int_test.zig"); } diff --git a/lib/std/net.zig b/lib/std/net.zig index 6fe2d1cd08..28ae2b9857 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1769,6 +1769,6 @@ pub const StreamServer = struct { } }; -test "" { +test { _ = @import("net/test.zig"); } diff --git a/lib/std/os.zig b/lib/std/os.zig index 8c3ea7baa8..ece47828d1 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -43,7 +43,7 @@ comptime { assert(@import("std") == std); // std lib tests require --override-lib-dir } -test "" { +test { _ = darwin; _ = freebsd; _ = linux; diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 37f30da1df..ffc1029708 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1355,7 +1355,7 @@ pub fn madvise(address: [*]u8, len: usize, advice: u32) usize { return syscall3(.madvise, @ptrToInt(address), len, advice); } -test "" { +test { if (builtin.os.tag == .linux) { _ = @import("linux/test.zig"); } diff --git a/lib/std/rand.zig b/lib/std/rand.zig index 8e6aab63df..d0d400b5b0 100644 --- a/lib/std/rand.zig +++ b/lib/std/rand.zig @@ -602,6 +602,6 @@ test "CSPRNG" { expect(a ^ b ^ c != 0); } -test "" { +test { std.testing.refAllDecls(@This()); } diff --git a/lib/std/special/compiler_rt/shift.zig b/lib/std/special/compiler_rt/shift.zig index 0c9938343f..46712738ab 100644 --- a/lib/std/special/compiler_rt/shift.zig +++ b/lib/std/special/compiler_rt/shift.zig @@ -124,7 +124,7 @@ pub fn __aeabi_llsr(a: i64, b: i32) callconv(.AAPCS) i64 { return __lshrdi3(a, b); } -test "" { +test { _ = @import("ashrdi3_test.zig"); _ = @import("ashrti3_test.zig"); diff --git a/lib/std/special/compiler_rt/sparc.zig b/lib/std/special/compiler_rt/sparc.zig index 72e297c514..e66bb25886 100644 --- a/lib/std/special/compiler_rt/sparc.zig +++ b/lib/std/special/compiler_rt/sparc.zig @@ -68,7 +68,6 @@ pub fn _Qp_fge(a: *f128, b: *f128) callconv(.C) bool { return cmp == @enumToInt(FCMP.Greater) or cmp == @enumToInt(FCMP.Equal); } - // Casting pub fn _Qp_dtoq(c: *f128, a: f64) callconv(.C) void { diff --git a/lib/std/std.zig b/lib/std/std.zig index d085d4fc41..780579debf 100644 --- a/lib/std/std.zig +++ b/lib/std/std.zig @@ -86,7 +86,7 @@ comptime { _ = start; } -test "" { +test { if (builtin.os.tag == .windows) { // We only test the Windows-relevant stuff to save memory because the CI // server is hitting OOM. TODO revert this after stage2 arrives. diff --git a/lib/std/target.zig b/lib/std/target.zig index 155ba046d2..70a5f08612 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1577,6 +1577,6 @@ pub const Target = struct { } }; -test "" { +test { std.testing.refAllDecls(Target.Cpu.Arch); } diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 288eb5b662..26938367e9 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -443,7 +443,7 @@ fn printLine(line: []const u8) void { print("{s}\n", .{line}); } -test "" { +test { expectEqualStrings("foo", "foo"); } diff --git a/lib/std/valgrind.zig b/lib/std/valgrind.zig index 4ae273694b..6930652fbc 100644 --- a/lib/std/valgrind.zig +++ b/lib/std/valgrind.zig @@ -262,7 +262,7 @@ pub fn monitorCommand(command: [*]u8) bool { pub const memcheck = @import("valgrind/memcheck.zig"); pub const callgrind = @import("valgrind/callgrind.zig"); -test "" { +test { _ = @import("valgrind/memcheck.zig"); _ = @import("valgrind/callgrind.zig"); } diff --git a/lib/std/zig.zig b/lib/std/zig.zig index c39eb6b05f..5b564864ad 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -255,6 +255,6 @@ test "parseCharLiteral" { std.testing.expectError(error.InvalidCharacter, parseCharLiteral("'\\u{FFFF}x'", &bad_index)); } -test "" { +test { @import("std").testing.refAllDecls(@This()); } diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index d8a9998274..2d9f286dd6 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -898,6 +898,6 @@ pub const NativeTargetInfo = struct { } }; -test "" { +test { _ = @import("system/macos.zig"); } diff --git a/src/codegen/aarch64.zig b/src/codegen/aarch64.zig index e7860790f4..8abc616e2f 100644 --- a/src/codegen/aarch64.zig +++ b/src/codegen/aarch64.zig @@ -906,7 +906,7 @@ pub const Instruction = union(enum) { } }; -test "" { +test { testing.refAllDecls(@This()); }