Reduce size of non-inlined ArgumentOutOfRangeException throw helpers (#88508)
authorStephen Toub <stoub@microsoft.com>
Fri, 7 Jul 2023 14:56:08 +0000 (10:56 -0400)
committerGitHub <noreply@github.com>
Fri, 7 Jul 2023 14:56:08 +0000 (10:56 -0400)
Keeping the arguments of the delegated throw method the same order as the callee avoids unnecessary spilling.

src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs

index 45ae63f..48c941f 100644 (file)
@@ -96,39 +96,39 @@ namespace System
         public virtual object? ActualValue => _actualValue;
 
         [DoesNotReturn]
-        private static void ThrowZero<T>(string? paramName, T value) =>
+        private static void ThrowZero<T>(T value, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowNegative<T>(string? paramName, T value) =>
+        private static void ThrowNegative<T>(T value, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowNegativeOrZero<T>(string? paramName, T value) =>
+        private static void ThrowNegativeOrZero<T>(T value, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowGreater<T>(string? paramName, T value, T other) =>
+        private static void ThrowGreater<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowGreaterEqual<T>(string? paramName, T value, T other) =>
+        private static void ThrowGreaterEqual<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowLess<T>(string? paramName, T value, T other) =>
+        private static void ThrowLess<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowLessEqual<T>(string? paramName, T value, T other) =>
+        private static void ThrowLessEqual<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowEqual<T>(string? paramName, T value, T other) =>
+        private static void ThrowEqual<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNotEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
 
         [DoesNotReturn]
-        private static void ThrowNotEqual<T>(string? paramName, T value, T other) =>
+        private static void ThrowNotEqual<T>(T value, T other, string? paramName) =>
             throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary>
@@ -138,7 +138,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsZero(value))
-                ThrowZero(paramName, value);
+                ThrowZero(value, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
@@ -148,7 +148,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsNegative(value))
-                ThrowNegative(paramName, value);
+                ThrowNegative(value, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative or zero.</summary>
@@ -158,7 +158,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsNegative(value) || T.IsZero(value))
-                ThrowNegativeOrZero(paramName, value);
+                ThrowNegativeOrZero(value, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is equal to <paramref name="other"/>.</summary>
@@ -168,7 +168,7 @@ namespace System
         public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
         {
             if (EqualityComparer<T>.Default.Equals(value, other))
-                ThrowEqual(paramName, value, other);
+                ThrowEqual(value, other, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not equal to <paramref name="other"/>.</summary>
@@ -178,7 +178,7 @@ namespace System
         public static void ThrowIfNotEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
         {
             if (!EqualityComparer<T>.Default.Equals(value, other))
-                ThrowNotEqual(paramName, value, other);
+                ThrowNotEqual(value, other, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary>
@@ -189,7 +189,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) > 0)
-                ThrowGreater(paramName, value, other);
+                ThrowGreater(value, other, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than or equal <paramref name="other"/>.</summary>
@@ -200,7 +200,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) >= 0)
-                ThrowGreaterEqual(paramName, value, other);
+                ThrowGreaterEqual(value, other, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than <paramref name="other"/>.</summary>
@@ -211,7 +211,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) < 0)
-                ThrowLess(paramName, value, other);
+                ThrowLess(value, other, paramName);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than or equal <paramref name="other"/>.</summary>
@@ -222,7 +222,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) <= 0)
-                ThrowLessEqual(paramName, value, other);
+                ThrowLessEqual(value, other, paramName);
         }
     }
 }