Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = .zig_utils,
.version = "0.7.2",
.version = "0.7.3",
.fingerprint = 0x6dc482caf73c4a75,
.minimum_zig_version = "0.16.0",

Expand Down
10 changes: 5 additions & 5 deletions src/a_star.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ pub fn AStar(comptime WeightType: type) type {
var closed_set = try std.DynamicBitSet.initEmpty(self.allocator, n);
defer closed_set.deinit();

var open_set = std.PriorityQueue(PQNode, void, PQNode.compare).init(self.allocator, {});
defer open_set.deinit();
var open_set = std.PriorityQueue(PQNode, void, PQNode.compare).initContext({});
defer open_set.deinit(self.allocator);

// Initialize source
g_score[source] = 0;
const h = self.calculateHeuristic(source, dest);
try open_set.add(.{ .vertex = source, .f_score = h });
try open_set.push(self.allocator, .{ .vertex = source, .f_score = h });

while (open_set.removeOrNull()) |current| {
while (open_set.pop()) |current| {
if (current.vertex == dest) {
// Reconstruct path
var node = dest;
Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn AStar(comptime WeightType: type) type {
g_score[edge.to] = tentative_g;

const f = @as(f32, @floatFromInt(tentative_g)) + self.calculateHeuristic(edge.to, dest);
try open_set.add(.{ .vertex = edge.to, .f_score = f });
try open_set.push(self.allocator, .{ .vertex = edge.to, .f_score = f });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/z_index_buckets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn ZIndexBuckets(comptime T: type, comptime ZIndexType: type) type {

pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.buckets = [_]Bucket{.{}} ** bucket_count,
.buckets = [_]Bucket{.empty} ** bucket_count,
.allocator = allocator,
.total_count = 0,
};
Expand Down
4 changes: 2 additions & 2 deletions tests/floyd_warshall_optimized_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub const FloydWarshallOptimizedSpec = struct {

fw.generate();

var path = std.ArrayListUnmanaged(u32){};
var path = std.ArrayListUnmanaged(u32).empty;
defer path.deinit(allocator);

try fw.setPathWithMappingUnmanaged(allocator, &path, 10, 40);
Expand Down Expand Up @@ -152,7 +152,7 @@ pub const FloydWarshallOptimizedSpec = struct {
fw.next[idx_a * size + idx_d] = idx_b; // from 10 toward 40 -> 20
fw.next[idx_b * size + idx_d] = idx_a; // from 20 toward 40 -> back to 10

var path = std.ArrayListUnmanaged(u32){};
var path = std.ArrayListUnmanaged(u32).empty;
defer path.deinit(allocator);

// Must TERMINATE with an error, not hang.
Expand Down
25 changes: 24 additions & 1 deletion tests/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,31 @@ pub const a_star_test = @import("a_star_test.zig");
pub const heuristics_test = @import("heuristics_test.zig");
pub const zon_coercion_test = @import("zon_coercion_test.zig");

// std's `refAllDecls` is ONE level deep — it references the imported test
// structs above, but NOT the `pub const FooSpec = struct { pub const group =
// struct { test "..." {} } }` groups nested inside them. Under Zig 0.16 a
// `test` block is only collected into the test binary once its containing
// struct is semantically analyzed, so every doubly-nested zspec test was
// silently never run, and `zig build test` passed even with a failing
// assertion (#14). Recurse into every nested container type so each group
// struct is analyzed and its tests get collected. (Zig 0.16 dropped
// `std.testing.refAllDeclsRecursive`, hence the local copy.)
fn refAllDeclsRecursive(comptime T: type) void {
if (!@import("builtin").is_test) return;
inline for (comptime std.meta.declarations(T)) |decl| {
const field = @field(T, decl.name);
if (@TypeOf(field) == type) {
switch (@typeInfo(field)) {
.@"struct", .@"enum", .@"union", .@"opaque" => refAllDeclsRecursive(field),
else => {},
}
}
_ = &@field(T, decl.name);
}
Comment on lines +26 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Checking decl.is_pub is highly recommended here. Without this check, refAllDeclsRecursive will attempt to inspect private declarations (such as private imports like const std = @import("std"); or other helper constants/functions). This can lead to compile-time errors (since private declarations of other files/structs cannot be accessed from outside) or cause the compiler to recursively traverse the entire standard library, significantly increasing compile times or hitting recursion limits.

    inline for (comptime std.meta.declarations(T)) |decl| {
        if (!decl.is_pub) continue;
        const field = @field(T, decl.name);
        if (@TypeOf(field) == type) {
            switch (@typeInfo(field)) {
                .@"struct", .@"enum", .@"union", .@"opaque" => refAllDeclsRecursive(field),
                else => {},
            }
        }
        _ = &@field(T, decl.name);
    }

}

test {
std.testing.refAllDecls(@This());
refAllDeclsRecursive(@This());
}

// Regression for #13 — kept at the test ROOT (top-level) on purpose: the
Expand Down
Loading