Skip to content

Commit 996b012

Browse files
authored
Reduce size of non-inlined ArgumentOutOfRangeException throw helpers (#88508)
Keeping the arguments of the delegated throw method the same order as the callee avoids unnecessary spilling.
1 parent bcd678f commit 996b012

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -96,39 +96,39 @@ public override string Message
9696
public virtual object? ActualValue => _actualValue;
9797

9898
[DoesNotReturn]
99-
private static void ThrowZero<T>(string? paramName, T value) =>
99+
private static void ThrowZero<T>(T value, string? paramName) =>
100100
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName, value));
101101

102102
[DoesNotReturn]
103-
private static void ThrowNegative<T>(string? paramName, T value) =>
103+
private static void ThrowNegative<T>(T value, string? paramName) =>
104104
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName, value));
105105

106106
[DoesNotReturn]
107-
private static void ThrowNegativeOrZero<T>(string? paramName, T value) =>
107+
private static void ThrowNegativeOrZero<T>(T value, string? paramName) =>
108108
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName, value));
109109

110110
[DoesNotReturn]
111-
private static void ThrowGreater<T>(string? paramName, T value, T other) =>
111+
private static void ThrowGreater<T>(T value, T other, string? paramName) =>
112112
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, value, other));
113113

114114
[DoesNotReturn]
115-
private static void ThrowGreaterEqual<T>(string? paramName, T value, T other) =>
115+
private static void ThrowGreaterEqual<T>(T value, T other, string? paramName) =>
116116
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, value, other));
117117

118118
[DoesNotReturn]
119-
private static void ThrowLess<T>(string? paramName, T value, T other) =>
119+
private static void ThrowLess<T>(T value, T other, string? paramName) =>
120120
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, value, other));
121121

122122
[DoesNotReturn]
123-
private static void ThrowLessEqual<T>(string? paramName, T value, T other) =>
123+
private static void ThrowLessEqual<T>(T value, T other, string? paramName) =>
124124
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, value, other));
125125

126126
[DoesNotReturn]
127-
private static void ThrowEqual<T>(string? paramName, T value, T other) =>
127+
private static void ThrowEqual<T>(T value, T other, string? paramName) =>
128128
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNotEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
129129

130130
[DoesNotReturn]
131-
private static void ThrowNotEqual<T>(string? paramName, T value, T other) =>
131+
private static void ThrowNotEqual<T>(T value, T other, string? paramName) =>
132132
throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
133133

134134
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary>
@@ -138,7 +138,7 @@ public static void ThrowIfZero<T>(T value, [CallerArgumentExpression(nameof(valu
138138
where T : INumberBase<T>
139139
{
140140
if (T.IsZero(value))
141-
ThrowZero(paramName, value);
141+
ThrowZero(value, paramName);
142142
}
143143

144144
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
@@ -148,7 +148,7 @@ public static void ThrowIfNegative<T>(T value, [CallerArgumentExpression(nameof(
148148
where T : INumberBase<T>
149149
{
150150
if (T.IsNegative(value))
151-
ThrowNegative(paramName, value);
151+
ThrowNegative(value, paramName);
152152
}
153153

154154
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative or zero.</summary>
@@ -158,7 +158,7 @@ public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(n
158158
where T : INumberBase<T>
159159
{
160160
if (T.IsNegative(value) || T.IsZero(value))
161-
ThrowNegativeOrZero(paramName, value);
161+
ThrowNegativeOrZero(value, paramName);
162162
}
163163

164164
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is equal to <paramref name="other"/>.</summary>
@@ -168,7 +168,7 @@ public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(n
168168
public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
169169
{
170170
if (EqualityComparer<T>.Default.Equals(value, other))
171-
ThrowEqual(paramName, value, other);
171+
ThrowEqual(value, other, paramName);
172172
}
173173

174174
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not equal to <paramref name="other"/>.</summary>
@@ -178,7 +178,7 @@ public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(n
178178
public static void ThrowIfNotEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
179179
{
180180
if (!EqualityComparer<T>.Default.Equals(value, other))
181-
ThrowNotEqual(paramName, value, other);
181+
ThrowNotEqual(value, other, paramName);
182182
}
183183

184184
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary>
@@ -189,7 +189,7 @@ public static void ThrowIfGreaterThan<T>(T value, T other, [CallerArgumentExpres
189189
where T : IComparable<T>
190190
{
191191
if (value.CompareTo(other) > 0)
192-
ThrowGreater(paramName, value, other);
192+
ThrowGreater(value, other, paramName);
193193
}
194194

195195
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than or equal <paramref name="other"/>.</summary>
@@ -200,7 +200,7 @@ public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [CallerArgumen
200200
where T : IComparable<T>
201201
{
202202
if (value.CompareTo(other) >= 0)
203-
ThrowGreaterEqual(paramName, value, other);
203+
ThrowGreaterEqual(value, other, paramName);
204204
}
205205

206206
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than <paramref name="other"/>.</summary>
@@ -211,7 +211,7 @@ public static void ThrowIfLessThan<T>(T value, T other, [CallerArgumentExpressio
211211
where T : IComparable<T>
212212
{
213213
if (value.CompareTo(other) < 0)
214-
ThrowLess(paramName, value, other);
214+
ThrowLess(value, other, paramName);
215215
}
216216

217217
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than or equal <paramref name="other"/>.</summary>
@@ -222,7 +222,7 @@ public static void ThrowIfLessThanOrEqual<T>(T value, T other, [CallerArgumentEx
222222
where T : IComparable<T>
223223
{
224224
if (value.CompareTo(other) <= 0)
225-
ThrowLessEqual(paramName, value, other);
225+
ThrowLessEqual(value, other, paramName);
226226
}
227227
}
228228
}

0 commit comments

Comments
 (0)