87. sort(cmp:) / sorted(cmp:) with checked strict-weak-order traps (2026-11-21)
- Decision: Add a
cmp: (a, b) => Booloption tosortandsorted(mutually exclusive withby:, composing withdesc: true), lowering throughSortBy::Fn { func, is_order: true }to stable O(n log n) sorting (cpl::sort_cmpin C++,checked_merge_sort_byin the interpreter). Incpl runandcpl build --checked(-DLOCAL), non-strict-weak-order comparators trap on irreflexivity (cmp(x, x)), asymmetry (cmp(a, b) && cmp(b, a)), and post-sort order violations (intransitivity); release builds compile to zero-overheadstd::stable_sort. - Reason: Exchange-argument sorts (
a.x * b.y < b.x * a.y), angle sorts, and comparator-over-state sorts (likecf2135f) cannot be expressed as a single key and previously forced a wrapper struct +impl Ord. Namedcmp:fits the options scheme, avoids UB in checked builds, and keeps release performance identical to C++. - Reverse: Remove the
cmparm fromsort_placeincrates/cpl-ir/src/lower/call.rsandsort_cmpincrates/cpl-codegen/src/runtime.rs.