From 3b60ab4872355f0b9a9c7d0794ca8b548ab99412 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 5 May 2022 22:17:21 -0700 Subject: [PATCH] stage2: fix std lib tests always filtering out all tests --- src/Compilation.zig | 1 + src/Module.zig | 16 ++++++++++++++-- src/Package.zig | 8 ++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 2ad8a9e030..3f4d8d6976 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1418,6 +1418,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .gpa = gpa, .comp = comp, .main_pkg = main_pkg, + .main_pkg_is_std = try main_pkg.isInsideOf(std_pkg.*), .root_pkg = root_pkg, .zig_cache_artifact_directory = zig_cache_artifact_directory, .global_zir_cache = global_zir_cache, diff --git a/src/Module.zig b/src/Module.zig index 864663ada2..0966c8ffcf 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -130,6 +130,10 @@ stage1_flags: packed struct { } = .{}, job_queued_update_builtin_zig: bool = true, +/// This makes it so that we can run `zig test` on the standard library. +/// Otherwise, the logic for scanning test decls skips all of them because +/// `main_pkg != std_pkg`. +main_pkg_is_std: bool, compile_log_text: ArrayListUnmanaged(u8) = .{}, @@ -4528,14 +4532,22 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi // test decl with no name. Skip the part where we check against // the test name filter. if (!mod.comp.bin_file.options.is_test) break :blk false; - if (decl_pkg != mod.main_pkg) break :blk false; + if (decl_pkg != mod.main_pkg) { + if (!mod.main_pkg_is_std) break :blk false; + const std_pkg = mod.main_pkg.table.get("std").?; + if (std_pkg != decl_pkg) break :blk false; + } try mod.test_functions.put(gpa, new_decl_index, {}); break :blk true; }, else => blk: { if (!is_named_test) break :blk false; if (!mod.comp.bin_file.options.is_test) break :blk false; - if (decl_pkg != mod.main_pkg) break :blk false; + if (decl_pkg != mod.main_pkg) { + if (!mod.main_pkg_is_std) break :blk false; + const std_pkg = mod.main_pkg.table.get("std").?; + if (std_pkg != decl_pkg) break :blk false; + } // TODO check the name against --test-filter try mod.test_functions.put(gpa, new_decl_index, {}); break :blk true; diff --git a/src/Package.zig b/src/Package.zig index df894280a9..9ac1e2e5b1 100644 --- a/src/Package.zig +++ b/src/Package.zig @@ -124,3 +124,11 @@ pub fn addAndAdopt(parent: *Package, gpa: Allocator, name: []const u8, child: *P child.parent = parent; return parent.add(gpa, name, child); } + +pub fn isInsideOf(pkg: Package, another: Package) !bool { + var pkg_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; + var another_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const pkg_path = try pkg.root_src_directory.handle.realpath(pkg.root_src_path, &pkg_buffer); + const another_path = try another.root_src_directory.handle.realpath(".", &another_buffer); + return mem.startsWith(u8, pkg_path, another_path); +}