Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of ArgumentOutOfRangeException throw helpers when not inlined #88508

Merged
merged 1 commit into from
Jul 7, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,39 @@ public override string Message
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>
Expand All @@ -138,7 +138,7 @@ public static void ThrowIfZero<T>(T value, [CallerArgumentExpression(nameof(valu
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>
Expand All @@ -148,7 +148,7 @@ public static void ThrowIfNegative<T>(T value, [CallerArgumentExpression(nameof(
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>
Expand All @@ -158,7 +158,7 @@ public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(n
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>
Expand All @@ -168,7 +168,7 @@ public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(n
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>
Expand All @@ -178,7 +178,7 @@ public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(n
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>
Expand All @@ -189,7 +189,7 @@ public static void ThrowIfGreaterThan<T>(T value, T other, [CallerArgumentExpres
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>
Expand All @@ -200,7 +200,7 @@ public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [CallerArgumen
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>
Expand All @@ -211,7 +211,7 @@ public static void ThrowIfLessThan<T>(T value, T other, [CallerArgumentExpressio
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>
Expand All @@ -222,7 +222,7 @@ public static void ThrowIfLessThanOrEqual<T>(T value, T other, [CallerArgumentEx
where T : IComparable<T>
{
if (value.CompareTo(other) <= 0)
ThrowLessEqual(paramName, value, other);
ThrowLessEqual(value, other, paramName);
}
}
}