From 51c4299433f4ca3e018edcf4665282cfc5537c60 Mon Sep 17 00:00:00 2001 From: Vastargazing Date: Fri, 3 Apr 2026 18:37:13 +0300 Subject: [PATCH] coretests: add argument order regression tests for min_by/max_by/minmax_by MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A recent regression swapped the argument order passed to the compare closure in min_by, max_by and minmax_by (compare(&v2, &v1) instead of compare(&v1, &v2)). This was fixed, but no regression test was added. Add tests that record the arguments the compare closure receives and assert they match (v1, v2) — the documented contract. --- library/coretests/tests/cmp.rs | 31 +++++++++++++++++++++++++++++++ library/coretests/tests/lib.rs | 1 + 2 files changed, 32 insertions(+) diff --git a/library/coretests/tests/cmp.rs b/library/coretests/tests/cmp.rs index 0a14470060c3d..888a3cfd57a85 100644 --- a/library/coretests/tests/cmp.rs +++ b/library/coretests/tests/cmp.rs @@ -48,6 +48,37 @@ fn test_ord_min_max_by() { assert_eq!(cmp::max_by(2, -1, f), 2); } +// Regression test for #136307 / #139357: ensure compare() receives (v1, v2), not (v2, v1). +#[test] +fn min_by_compare_argument_order() { + let mut order = vec![]; + let _ = cmp::min_by(1i32, 2, |a, b| { + order.push((*a, *b)); + a.cmp(b) + }); + assert_eq!(order, [(1, 2)]); +} + +#[test] +fn max_by_compare_argument_order() { + let mut order = vec![]; + let _ = cmp::max_by(1i32, 2, |a, b| { + order.push((*a, *b)); + a.cmp(b) + }); + assert_eq!(order, [(1, 2)]); +} + +#[test] +fn minmax_by_compare_argument_order() { + let mut order = vec![]; + let _ = cmp::minmax_by(1i32, 2, |a, b| { + order.push((*a, *b)); + a.cmp(b) + }); + assert_eq!(order, [(1, 2)]); +} + #[test] fn test_ord_min_max_by_key() { let f = |x: &i32| x.abs(); diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index 90a33aeead150..851eb12eb65c3 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -13,6 +13,7 @@ #![feature(char_internals)] #![feature(char_max_len)] #![feature(clone_to_uninit)] +#![feature(cmp_minmax)] #![feature(const_array)] #![feature(const_bool)] #![feature(const_cell_traits)]