aboutsummaryrefslogtreecommitdiff
path: root/lib/std/debug
diff options
context:
space:
mode:
authorJay Petacat <jay@jayschwa.net>2024-09-09 22:23:18 -0600
committerAndrew Kelley <andrew@ziglang.org>2024-09-16 14:04:18 -0700
commit812557bfde3c577b5f00cb556201c71ad5ed6fa4 (patch)
tree78dc955c7aa665ea95f32a3820903f63c025aed7 /lib/std/debug
parent7caa3d9da71c38665340247a1c2bf9bedb8db925 (diff)
downloadzig-812557bfde3c577b5f00cb556201c71ad5ed6fa4.tar.gz
zig-812557bfde3c577b5f00cb556201c71ad5ed6fa4.zip
std: Restore conventional `compareFn` behavior for `binarySearch`
PR #20927 made some improvements to the `binarySearch` API, but one change I found surprising was the relationship between the left-hand and right-hand parameters of `compareFn` was inverted. This is different from how comparison functions typically behave, both in other parts of Zig (e.g. `std.math.order`) and in other languages (e.g. C's `bsearch`). Unless a strong reason can be identified and documented for doing otherwise, I think it'll be better to stick with convention. While writing this patch and changing things back to the way they were, the predicates of `lowerBound` and `upperBound` seemed to be the only areas that benefited from the inversion. I don't think that benefit is worth the cost, personally. Calling `Order.invert()` in the predicates accomplishes the same goal.
Diffstat (limited to 'lib/std/debug')
-rw-r--r--lib/std/debug/Coverage.zig2
-rw-r--r--lib/std/debug/Dwarf.zig2
-rw-r--r--lib/std/debug/SelfInfo.zig4
3 files changed, 4 insertions, 4 deletions
diff --git a/lib/std/debug/Coverage.zig b/lib/std/debug/Coverage.zig
index 2d0e054673..58e600dc63 100644
--- a/lib/std/debug/Coverage.zig
+++ b/lib/std/debug/Coverage.zig
@@ -196,7 +196,7 @@ pub fn resolveAddressesDwarf(
const table_addrs = slc.line_table.keys();
line_table_i = std.sort.upperBound(u64, table_addrs, pc, struct {
fn order(context: u64, item: u64) std.math.Order {
- return std.math.order(item, context);
+ return std.math.order(context, item);
}
}.order);
}
diff --git a/lib/std/debug/Dwarf.zig b/lib/std/debug/Dwarf.zig
index d36e4f961d..73b1871c46 100644
--- a/lib/std/debug/Dwarf.zig
+++ b/lib/std/debug/Dwarf.zig
@@ -182,7 +182,7 @@ pub const CompileUnit = struct {
pub fn findSource(slc: *const SrcLocCache, address: u64) !LineEntry {
const index = std.sort.upperBound(u64, slc.line_table.keys(), address, struct {
fn order(context: u64, item: u64) std.math.Order {
- return std.math.order(item, context);
+ return std.math.order(context, item);
}
}.order);
if (index == 0) return missing();
diff --git a/lib/std/debug/SelfInfo.zig b/lib/std/debug/SelfInfo.zig
index 76c0505e96..5e9afc8cfd 100644
--- a/lib/std/debug/SelfInfo.zig
+++ b/lib/std/debug/SelfInfo.zig
@@ -1624,12 +1624,12 @@ pub fn unwindFrameDwarf(
} else {
const index = std.sort.binarySearch(Dwarf.FrameDescriptionEntry, di.fde_list.items, context.pc, struct {
pub fn compareFn(pc: usize, item: Dwarf.FrameDescriptionEntry) std.math.Order {
- if (pc < item.pc_begin) return .gt;
+ if (pc < item.pc_begin) return .lt;
const range_end = item.pc_begin + item.pc_range;
if (pc < range_end) return .eq;
- return .lt;
+ return .gt;
}
}.compareFn);