Skip to content
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
93 changes: 42 additions & 51 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1935,24 +1935,19 @@ macro impl_tuples($($mac:ident,)+) {
}

/// Implementation detail for [`smallest`] and [`largest`].
/// Marker indicating that `Self` is a tuple where all members are of the same type.
/// Marker indicating that `Self` is a tuple where all members are of the type `T`.
/// Cannot be an associated type as we require the empty tuple to implement this trait
/// for all types `T`.
#[diagnostic::on_unimplemented(message = "`{Self}` is not a homogeneous tuple")]
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const trait HomogeneousTuple: crate::marker::Tuple {
/// The type of each item in this tuple.
type Item;
}
const trait HomogeneousTuple<T>: crate::marker::Tuple {}

/// Implements [`HomogeneousTuple`] for a provided tuple.
macro impl_homogeneous_tuple($($($x:ident,)+)?) {
$(
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> HomogeneousTuple for ($(${ignore($x)}T,)+) {
type Item = T;
}
)?
macro impl_homogeneous_tuple($($x:ident,)*) {
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> HomogeneousTuple<T> for ($(${ignore($x)}T,)*) { }
}

impl_tuples! {
Expand All @@ -1971,7 +1966,6 @@ impl_tuples! {
/// #![feature(cmp_splat)]
/// use std::cmp;
///
/// assert_eq!(cmp::smallest(1), 1);
/// assert_eq!(cmp::smallest(1, 2), 1);
/// assert_eq!(cmp::smallest(3, 2, 1), 1);
/// assert_eq!(cmp::smallest(1, 2, 3, 4), 1);
Expand Down Expand Up @@ -2008,36 +2002,35 @@ impl_tuples! {
#[expect(private_bounds, reason = "`SmallestArgs` is an internal implementation detail")]
#[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests
pub const fn smallest<T: [const] Ord + [const] Destruct>(
#[rustc_splat] args: impl [const] SmallestArgs<Item = T>,
v1: T,
v2: T,
#[rustc_splat] args: impl [const] SmallestArgs<T>,
Comment on lines +2005 to +2007

@scottmcm scottmcm Aug 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this worked with splat; cool!

My instinct would be to keep the 1-parameter version working, but to do the middle-ground here were it emphasizes that the 0-parameter version doesn't exist without understanding the trait impls. (If in future this could be #[rustc_splat] args: [T; N], for example, that'd be pretty sweet.)

So maybe something like

Suggested change
v1: T,
v2: T,
#[rustc_splat] args: impl [const] SmallestArgs<T>,
v: T,
#[rustc_splat] args: impl [const] SmallestArgs<T>,

instead?

View changes since the review

) -> T {
SmallestArgs::smallest(args)
SmallestArgs::smallest(v1, v2, args)
}

/// Implementation detail for [`smallest`].
#[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `smallest`")]
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const trait SmallestArgs: HomogeneousTuple {
const trait SmallestArgs<T>: HomogeneousTuple<T> {
/// Reduces all elements of a homogeneous tuple to its smallest value.
fn smallest(self) -> Self::Item;
fn smallest(v1: T, v2: T, args: Self) -> T;
}

/// Implements [`SmallestArgs`] for a provided tuple if applicable.
macro impl_smallest_args($($x:ident, $($($y:ident,)+)?)?) {
$(
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> SmallestArgs for (T, $($(${ignore($y)}T,)+)?)
$(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)?
{
#[inline]
fn smallest(self) -> Self::Item {
let ($x, $($($y,)+)?) = self;
$($(let $x = $x.min($y);)+)?
$x
}
macro impl_smallest_args($($x:ident,)*) {
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> SmallestArgs<T> for ($(${ignore($x)}T,)*)
where
T: [const] Destruct + [const] Ord,
{
#[inline(always)] // improves unoptimised codegen
fn smallest(v1: T, v2: T, ($($x,)*): Self) -> T {
v1.min(v2)$(.min($x))*

@scottmcm scottmcm Aug 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and having this be

Suggested change
v1.min(v2)$(.min($x))*
v $(.min($x))*

without needing the separate detupling looks really elegant.

View changes since the review

}
)?
}
}

impl_tuples! {
Expand All @@ -2056,7 +2049,6 @@ impl_tuples! {
/// #![feature(cmp_splat)]
/// use std::cmp;
///
/// assert_eq!(cmp::largest(1), 1);
/// assert_eq!(cmp::largest(1, 2), 2);
/// assert_eq!(cmp::largest(3, 2, 1), 3);
/// assert_eq!(cmp::largest(1, 2, 3, 4), 4);
Expand Down Expand Up @@ -2093,36 +2085,35 @@ impl_tuples! {
#[expect(private_bounds, reason = "`LargestArgs` is an internal implementation detail")]
#[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests
pub const fn largest<T: [const] Ord + [const] Destruct>(
#[rustc_splat] args: impl [const] LargestArgs<Item = T>,
v1: T,
v2: T,
#[rustc_splat] args: impl [const] LargestArgs<T>,
) -> T {
LargestArgs::largest(args)
LargestArgs::largest(v1, v2, args)
}

/// Implementation detail for [`largest`].
#[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `largest`")]
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const trait LargestArgs: HomogeneousTuple {
const trait LargestArgs<T>: HomogeneousTuple<T> {
/// Reduces all elements of a homogeneous tuple to its largest value.
fn largest(self) -> Self::Item;
fn largest(v1: T, v2: T, args: Self) -> T;
}

/// Implements [`LargestArgs`] for a provided tuple if applicable.
macro impl_largest_args($($x:ident, $($($y:ident,)+)?)?) {
$(
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> LargestArgs for (T, $($(${ignore($y)}T,)+)?)
$(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)?
{
#[inline]
fn largest(self) -> Self::Item {
let ($x, $($($y,)+)?) = self;
$($(let $x = $x.max($y);)+)?
$x
}
macro impl_largest_args($($x:ident,)*) {
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
const impl<T> LargestArgs<T> for ($(${ignore($x)}T,)*)
where
T: [const] Destruct + [const] Ord,
{
#[inline(always)] // improves unoptimised codegen
fn largest(v1: T, v2: T, ($($x,)*): Self) -> T {
v1.max(v2)$(.max($x))*
}
)?
}
}

impl_tuples! {
Expand Down
51 changes: 51 additions & 0 deletions tests/mir-opt/cmp_splat.min_direct.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// MIR for `min_direct` after built

fn min_direct(_1: T, _2: T) -> T {
debug v1 => _1;
debug v2 => _2;
let mut _0: T;
let mut _3: T;
let mut _4: T;

bb0: {
StorageLive(_3);
_3 = move _1;
StorageLive(_4);
_4 = move _2;
_0 = <T as Ord>::min(move _3, move _4) -> [return: bb1, unwind: bb4];
}

bb1: {
StorageDead(_4);
StorageDead(_3);
drop(_2) -> [return: bb2, unwind: bb7];
}

bb2: {
drop(_1) -> [return: bb3, unwind: bb8];
}

bb3: {
return;
}

bb4 (cleanup): {
drop(_4) -> [return: bb5, unwind terminate(cleanup)];
}

bb5 (cleanup): {
drop(_3) -> [return: bb6, unwind terminate(cleanup)];
}

bb6 (cleanup): {
drop(_2) -> [return: bb7, unwind terminate(cleanup)];
}

bb7 (cleanup): {
drop(_1) -> [return: bb8, unwind terminate(cleanup)];
}

bb8 (cleanup): {
resume;
}
}
27 changes: 27 additions & 0 deletions tests/mir-opt/cmp_splat.min_direct_u8.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// MIR for `min_direct_u8` after built

fn min_direct_u8(_1: u8, _2: u8) -> u8 {
debug x => _1;
debug y => _2;
let mut _0: u8;
let mut _3: u8;
let mut _4: u8;

bb0: {
StorageLive(_3);
_3 = copy _1;
StorageLive(_4);
_4 = copy _2;
_0 = min_direct::<u8>(move _3, move _4) -> [return: bb1, unwind: bb2];
}

bb1: {
StorageDead(_4);
StorageDead(_3);
return;
}

bb2 (cleanup): {
resume;
}
}
68 changes: 68 additions & 0 deletions tests/mir-opt/cmp_splat.min_splatted.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// MIR for `min_splatted` after built

fn min_splatted(_1: T, _2: T, _3: impl MinArgs<T>) -> T {
debug v1 => _1;
debug v2 => _2;
debug args => _3;
let mut _0: T;
let mut _4: T;
let mut _5: T;
let mut _6: impl MinArgs<T>;

bb0: {
StorageLive(_4);
_4 = move _1;
StorageLive(_5);
_5 = move _2;
StorageLive(_6);
_6 = move _3;
_0 = <impl MinArgs<T> as MinArgs<T>>::min_splatted_inner(move _4, move _5, move _6) -> [return: bb1, unwind: bb5];
}

bb1: {
StorageDead(_6);
StorageDead(_5);
StorageDead(_4);
drop(_3) -> [return: bb2, unwind: bb9];
}

bb2: {
drop(_2) -> [return: bb3, unwind: bb10];
}

bb3: {
drop(_1) -> [return: bb4, unwind: bb11];
}

bb4: {
return;
}

bb5 (cleanup): {
drop(_6) -> [return: bb6, unwind terminate(cleanup)];
}

bb6 (cleanup): {
drop(_5) -> [return: bb7, unwind terminate(cleanup)];
}

bb7 (cleanup): {
drop(_4) -> [return: bb8, unwind terminate(cleanup)];
}

bb8 (cleanup): {
drop(_3) -> [return: bb9, unwind terminate(cleanup)];
}

bb9 (cleanup): {
drop(_2) -> [return: bb10, unwind terminate(cleanup)];
}

bb10 (cleanup): {
drop(_1) -> [return: bb11, unwind terminate(cleanup)];
}

bb11 (cleanup): {
resume;
}
}
31 changes: 31 additions & 0 deletions tests/mir-opt/cmp_splat.min_splatted_u8.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// MIR for `min_splatted_u8` after built

fn min_splatted_u8(_1: u8, _2: u8) -> u8 {
debug x => _1;
debug y => _2;
let mut _0: u8;
let mut _3: u8;
let mut _4: u8;
let mut _5: ();

bb0: {
StorageLive(_3);
_3 = copy _1;
StorageLive(_4);
_4 = copy _2;
StorageLive(_5);
_5 = ();
_0 = min_splatted::<u8, ()>(move _3, move _4, move _5) -> [return: bb1, unwind: bb2];
}

bb1: {
StorageDead(_5);
StorageDead(_4);
StorageDead(_3);
return;
}

bb2 (cleanup): {
resume;
}
}
44 changes: 44 additions & 0 deletions tests/mir-opt/cmp_splat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//@ skip-filecheck

#![feature(splat)]
#![feature(tuple_trait)]

// Comparing the MIR of a splat-compatible function with it's direct counterpart.
// It's expected that there will be additional MIR generated due to the layer of
// indirection added by a trait (MinArgs). This test tracks that overhead.

use std::marker::Tuple;

// EMIT_MIR cmp_splat.min_direct_u8.built.after.mir
pub fn min_direct_u8(x: u8, y: u8) -> u8 {
min_direct(x, y)
}

// EMIT_MIR cmp_splat.min_splatted_u8.built.after.mir
pub fn min_splatted_u8(x: u8, y: u8) -> u8 {
min_splatted(x, y)
}

// EMIT_MIR cmp_splat.min_direct.built.after.mir
#[inline]
pub fn min_direct<T: Ord>(v1: T, v2: T) -> T {
v1.min(v2)
}

// EMIT_MIR cmp_splat.min_splatted.built.after.mir
#[inline]
pub fn min_splatted<T: Ord>(v1: T, v2: T, #[rustc_splat] args: impl MinArgs<T>) -> T {
MinArgs::min_splatted_inner(v1, v2, args)
}

trait MinArgs<T>: Tuple {
fn min_splatted_inner(v1: T, v2: T, args: Self) -> T;
}

// EMIT_MIR cmp_splat.{impl#0}-min_splatted_inner.built.after.mir
impl<T: Ord> MinArgs<T> for () {
#[inline(always)]
fn min_splatted_inner(v1: T, v2: T, (): Self) -> T {
v1.min(v2)
}
}
Loading
Loading