Skip to content

Commit 609f0bb

Browse files
committed
workaround for stage2_riscv64 backend for now. Removed a few (hopefully) unnecessary comptime. Fixed StaticStringMapWithEql and added a test.
1 parent c38b4f1 commit 609f0bb

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

lib/std/static_array_map.zig

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn StaticStringMapIgnoreCase(comptime V: type) type {
1414
/// Same as StaticStringMap, but allows you to provide the `eql` function yourself.
1515
pub fn StaticStringMapWithEql(
1616
comptime V: type,
17-
comptime eql: fn (comptime usize, comptime anytype, anytype) bool,
17+
comptime eql: fn (comptime anytype, anytype) bool,
1818
) type {
1919
return StaticArrayMapWithEql(V, u8, eql);
2020
}
@@ -34,7 +34,11 @@ pub fn defaultEql(comptime expected: anytype, actual: anytype) bool {
3434
// which exceed 65535 bits (there are no integer types that large).
3535
const unique = std.meta.hasUniqueRepresentation(Array);
3636
const too_many_bits = child_bits > 65535;
37-
if (comptime !unique or too_many_bits) {
37+
38+
// TODO: riscv64 backend can't airBitCast [7]u8 to u56
39+
const limited_backend = @import("builtin").zig_backend == .stage2_riscv64;
40+
41+
if (!unique or too_many_bits or limited_backend) {
3842
var match: bool = true;
3943
for (expected, actual) |a, b| {
4044
match = match and a == b;
@@ -49,7 +53,7 @@ pub fn defaultEql(comptime expected: anytype, actual: anytype) bool {
4953
}
5054

5155
fn ignoreCaseEql(comptime expected: anytype, actual: anytype) bool {
52-
const lower_expected = comptime toLowerSimd(expected);
56+
const lower_expected = toLowerSimd(expected);
5357

5458
// TODO: x86_64 self hosted backend hasn't implemented genBinOp for cmp_gte
5559
const lower_actual = blk: {
@@ -129,7 +133,7 @@ pub fn StaticArrayMapWithEql(
129133

130134
/// Returns the value for the key if any, else null.
131135
pub fn get(comptime self: Self, key: []const T) ?V {
132-
switch (comptime self.kvs.len) {
136+
switch (self.kvs.len) {
133137
0 => return null,
134138
else => return self.filterLength(key),
135139
}
@@ -461,6 +465,31 @@ test "array elements that are padded" {
461465
try testing.expectEqual(2, map.get(&.{ 0, 1, 2, 126, 4, 5 }));
462466
}
463467

468+
fn lastElementEql(comptime expected: anytype, actual: anytype) bool {
469+
if (expected.len == 0) {
470+
return false;
471+
} else {
472+
const last_idx = expected.len - 1;
473+
return expected[last_idx] == actual[last_idx];
474+
}
475+
}
476+
477+
test "custom equal function" {
478+
const map = StaticStringMapWithEql(u2, lastElementEql).initComptime(.{
479+
.{ "last byte is a t", 0 },
480+
.{ "last byte is a b", 1 },
481+
.{ "last byte is a s", 2 },
482+
.{ "last byte is a c", 3 },
483+
});
484+
485+
// limitation: eql functions only are called on same-length inputs
486+
try testing.expectEqual(false, map.has("t"));
487+
488+
try testing.expectEqual(1, map.get("my magic byte: b"));
489+
try testing.expectEqual(0, map.get("my magic byte: t"));
490+
try testing.expectEqual(2, map.get("my magic byte: s"));
491+
}
492+
464493
test "single string StaticStringMap" {
465494
const map = StaticStringMap(void).initComptime(.{.{"o kama pona"}});
466495

0 commit comments

Comments
 (0)