aboutsummaryrefslogtreecommitdiff
path: root/lib/std/sort
diff options
context:
space:
mode:
authorAli Chraghi <alichraghi@proton.me>2023-06-24 17:31:50 +0330
committerAndrew Kelley <andrew@ziglang.org>2023-06-26 17:50:10 -0700
commit6bd54793060191ffce2c7492a90a40a30f9e2a1d (patch)
treea330c7f4d0882653cb2840a5faa2d04d38fa8fd6 /lib/std/sort
parent88284c124a0d930541f02ae9727118c0724f93f9 (diff)
downloadzig-6bd54793060191ffce2c7492a90a40a30f9e2a1d.tar.gz
zig-6bd54793060191ffce2c7492a90a40a30f9e2a1d.zip
std.sort.block: add safety check for lessThan return value
Diffstat (limited to 'lib/std/sort')
-rw-r--r--lib/std/sort/block.zig11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/std/sort/block.zig b/lib/std/sort/block.zig
index 518d148a73..a61ea6e326 100644
--- a/lib/std/sort/block.zig
+++ b/lib/std/sort/block.zig
@@ -1,3 +1,4 @@
+const builtin = @import("builtin");
const std = @import("../std.zig");
const sort = std.sort;
const math = std.math;
@@ -100,8 +101,16 @@ pub fn block(
comptime T: type,
items: []T,
context: anytype,
- comptime lessThan: fn (@TypeOf(context), lhs: T, rhs: T) bool,
+ comptime lessThanFn: fn (@TypeOf(context), lhs: T, rhs: T) bool,
) void {
+ const lessThan = if (builtin.mode == .Debug) struct {
+ fn lessThan(ctx: @TypeOf(context), lhs: T, rhs: T) bool {
+ const lt = lessThanFn(ctx, lhs, rhs);
+ const gt = lessThanFn(ctx, rhs, lhs);
+ std.debug.assert(!(lt and gt));
+ return lt;
+ }
+ }.lessThan else lessThanFn;
// Implementation ported from https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.c
var cache: [512]T = undefined;