authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-28 14:38:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-28 14:38:55-04:00
loge8a9caa3dd0aef7b745b8197afecec90598b2cdd
tree3ccc5dfb9f4c883b1644606cecd34c26f19661a9
parentaf90da153178032df109aa955df0aac113de032d
signaturelock-open Commit is signed but in an unrecognized format.

add suggestion to AutoHash compile error message


1 files changed, 12 insertions(+), 2 deletions(-)

std/hash/auto_hash.zig+12-2
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const builtin = @import("builtin");
3const assert = std.debug.assert;
34const mem = std.mem;
45const meta = std.meta;
56
......@@ -165,8 +166,17 @@ pub fn hash(hasher: var, key: var, comptime strat: HashStrategy) void {
165166/// Slices are rejected to avoid ambiguity on the user's intention.
166167pub fn autoHash(hasher: var, key: var) void {
167168 const Key = @typeOf(key);
168 if (comptime meta.trait.isSlice(Key))
169 @compileError("std.auto_hash.autoHash does not allow slices (here " ++ @typeName(Key) ++ " because the intent is unclear. Consider using std.auto_hash.hash or providing your own hash function instead.");
169 if (comptime meta.trait.isSlice(Key)) {
170 comptime assert(@hasDecl(std, "StringHashMap")); // detect when the following message needs updated
171 const extra_help = if (Key == []const u8)
172 " Consider std.StringHashMap for hashing the contents of []const u8."
173 else
174 "";
175
176 @compileError("std.auto_hash.autoHash does not allow slices (here " ++ @typeName(Key) ++
177 ") because the intent is unclear. Consider using std.auto_hash.hash or providing your own hash function instead." ++
178 extra_help);
179 }
170180
171181 hash(hasher, key, .Shallow);
172182}