From 3fadbd50b099b873b8631bc39158ce83d0845768 Mon Sep 17 00:00:00 2001 From: Dan54 Date: Thu, 16 Oct 2025 01:33:22 +0100 Subject: [PATCH 1/3] Add bounds to clamp error message --- library/core/src/cmp.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 7f369d19c3d12..e50984bb781dc 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1997,6 +1997,19 @@ mod impls { fn cmp(&self, other: &Self) -> Ordering { crate::intrinsics::three_way_compare(*self, *other) } + + #[inline] + fn clamp(self, min: Self, max: Self) -> Self + { + assert!(min <= max, "min > max. min = {min}, max = {max}"); + if self < min { + min + } else if self > max { + max + } else { + self + } + } } )*) } From f7d7a35daf9cb25b0b5ed0c76d176df66eb2a4e4 Mon Sep 17 00:00:00 2001 From: Dan54 Date: Thu, 16 Oct 2025 02:03:39 +0100 Subject: [PATCH 2/3] track_caller for clamp --- library/core/src/cmp.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index e50984bb781dc..3bc4c28a9d815 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1999,6 +1999,7 @@ mod impls { } #[inline] + #[track_caller] fn clamp(self, min: Self, max: Self) -> Self { assert!(min <= max, "min > max. min = {min}, max = {max}"); From 6dbea1f0a257862d1fcc1582f80fbd701dc5f18e Mon Sep 17 00:00:00 2001 From: Dan54 Date: Thu, 16 Oct 2025 03:04:51 +0100 Subject: [PATCH 3/3] use const_assert! --- library/core/src/cmp.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 3bc4c28a9d815..feb9c43196044 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1855,6 +1855,7 @@ mod impls { use crate::hint::unreachable_unchecked; use crate::marker::PointeeSized; use crate::ops::ControlFlow::{self, Break, Continue}; + use crate::panic::const_assert; macro_rules! partial_eq_impl { ($($t:ty)*) => ($( @@ -2002,7 +2003,13 @@ mod impls { #[track_caller] fn clamp(self, min: Self, max: Self) -> Self { - assert!(min <= max, "min > max. min = {min}, max = {max}"); + const_assert!( + min <= max, + "min > max", + "min > max. min = {min:?}, max = {max:?}", + min: $t, + max: $t, + ); if self < min { min } else if self > max {