Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve std.sort.binarySearch's return type, fix std.sort functions to adhere to their documentation #22300

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/compiler/aro/aro/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ fn clearBuffers(pp: *Preprocessor) void {

pub fn expansionSlice(pp: *Preprocessor, tok: Tree.TokenIndex) []Source.Location {
const S = struct {
fn orderTokenIndex(context: Tree.TokenIndex, item: Tree.TokenIndex) std.math.Order {
return std.math.order(context, item);
fn order(probed: Tree.TokenIndex, context: Tree.TokenIndex) std.math.Order {
return std.math.order(probed, context);
}
};

const indices = pp.expansion_entries.items(.idx);
const idx = std.sort.binarySearch(Tree.TokenIndex, indices, tok, S.orderTokenIndex) orelse return &.{};
const idx, const found = std.sort.binarySearch(Tree.TokenIndex, indices, tok, S.order);
if (!found) return &.{};
const locs = pp.expansion_entries.items(.locs)[idx];
var i: usize = 0;
while (locs[i].id != .unused) : (i += 1) {}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/debug/Coverage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ pub fn resolveAddressesDwarf(
const slc = &cu.src_loc_cache.?;
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(context, item);
fn order(probed: u64, context: u64) std.math.Order {
return std.math.order(probed, context);
}
}.order);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/debug/Dwarf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ 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(context, item);
fn order(probed: u64, context: u64) std.math.Order {
return std.math.order(probed, context);
}
}.order);
if (index == 0) return missing();
Expand Down
21 changes: 11 additions & 10 deletions lib/std/debug/SelfInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1651,18 +1651,19 @@ pub fn unwindFrameDwarf(
break :blk .{ cie, fde };
}

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 .lt;

const range_end = item.pc_begin + item.pc_range;
if (pc < range_end) return .eq;

return .gt;
const index, const found = std.sort.binarySearch(Dwarf.FrameDescriptionEntry, di.fde_list.items, context.pc, struct {
pub fn order(probed: Dwarf.FrameDescriptionEntry, pc: usize) std.math.Order {
return if (probed.pc_begin > pc) // start of range too great
.gt
else if (probed.pc_begin + probed.pc_range > pc) // target within range
.eq
else // end of range too little
.lt;
}
}.compareFn);
}.order);

const fde = if (index) |i| di.fde_list.items[i] else return error.MissingFDE;
if (!found) return error.MissingFDE;
const fde = di.fde_list.items[index];
const cie = di.cie_map.get(fde.cie_length_offset) orelse return error.MissingCIE;

break :blk .{ cie, fde };
Expand Down
Loading