summaryrefslogtreecommitdiff
path: root/src/walk.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/walk.zig')
-rw-r--r--src/walk.zig46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/walk.zig b/src/walk.zig
index c5a6e7a2..14e1929e 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -302,12 +302,12 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void {
.bool => h.update(&.{@intFromBool(v.*)}),
.int => hashInt(T, v.*, h),
.float => h.update(std.mem.asBytes(v)),
- .@"enum" => |e| hashInt(e.tag_type, @backingInt(v.*), h),
+ .@"enum" => hashEnum(T, v.*, h),
.optional => |o| if (v.*) |*payload| {
h.update(&.{1});
hash(o.child, payload, h);
} else h.update(&.{0}),
- .array => |a| if (comptime flat(a.child)) h.update(std.mem.sliceAsBytes(v)) else for (v) |*e| hash(a.child, e, h),
+ .array => |a| if (comptime flatHash(a.child)) h.update(std.mem.sliceAsBytes(v)) else for (v) |*e| hash(a.child, e, h),
.pointer => |p| switch (p.size) {
.one => hash(p.child, v.*, h),
.slice => hashSlice(p.child, v.*, h),
@@ -361,6 +361,35 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void {
}
}
+/// Bytes a digest takes whole: an enum's are its build's number, not it.
+fn flatHash(comptime T: type) bool {
+ return flat(T) and @typeInfo(T) != .@"enum";
+}
+
+/// An enum by its name, as a dump reads it, not by its number: an enum
+/// made at compile time numbers its words as its build has them
+/// (`registry.Builtin` has fewer in a FreeBSD build than a Linux one, which
+/// has Tty9p), so a window client here of a session on the box loaded a
+/// +LocationsConfig to another digest. A name's word (its hash, fixed
+/// width, so that two names in a row are not one), looked up by number
+/// where the words are numbered 0 up; one numbered by hand, or a value no
+/// word has, by its number.
+fn hashEnum(comptime T: type, v: T, h: *Hasher) void {
+ const e = @typeInfo(T).@"enum";
+ const words: ?[e.field_names.len]u64 = comptime words: {
+ @setEvalBranchQuota(100_000);
+ var out: [e.field_names.len]u64 = undefined;
+ for (e.field_names, e.field_values, 0..) |name, n, i| {
+ if (n != i) break :words null;
+ out[i] = std.hash.Wyhash.hash(0, name);
+ }
+ break :words out;
+ };
+ const n = @backingInt(v);
+ if (words) |table| if (n >= 0 and n < table.len) return h.update(std.mem.asBytes(&table[@intCast(n)]));
+ hashInt(e.tag_type, n, h);
+}
+
/// An integer by its value: one narrower than its bytes (a u21, an i2) has
/// bits in memory that are no part of it.
fn hashInt(comptime I: type, n: I, h: *Hasher) void {
@@ -372,7 +401,7 @@ fn hashInt(comptime I: type, n: I, h: *Hasher) void {
fn hashSlice(comptime E: type, items: []const E, h: *Hasher) void {
if (in_blob and items.len * @sizeOf(E) >= blob_min) return hashBlob(E, items, h);
h.update(std.mem.asBytes(&items.len));
- if (comptime flat(E)) return h.update(std.mem.sliceAsBytes(items));
+ if (comptime flatHash(E)) return h.update(std.mem.sliceAsBytes(items));
for (items) |*e| hash(E, e, h);
}
@@ -652,6 +681,17 @@ test "an error is taped by its name, which another build reads back as its own"
try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err);
}
+test "an enum is digested by its name, which another build may number otherwise" {
+ // registry.Builtin as a Linux build makes it (Tty9p in it) and as a
+ // FreeBSD one does: LocationsConfig is another number in each.
+ const Linux = enum { Look, Tty9p, LocationsConfig };
+ const FreeBSD = enum { Look, LocationsConfig };
+ const S = struct { from: Linux, also: [2]Linux };
+ const T = struct { from: FreeBSD, also: [2]FreeBSD };
+ try std.testing.expectEqual(digest(S, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }), digest(T, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }));
+ try std.testing.expect(digest(S, &.{ .from = .Look, .also = .{ .Look, .Look } }) != digest(S, &.{ .from = .Tty9p, .also = .{ .Look, .Look } }));
+}
+
test "a digest is of values: the same text at two addresses is one digest" {
const S = struct { name: []const u8, n: u32, next: ?*const u32 };
const a = "same text";