Skip to content

Commit

Permalink
Do some cleanup on TimeSpan to ensure better inlining and clearer sem…
Browse files Browse the repository at this point in the history
…antics (#94226)

* Do some cleanup on TimeSpan to ensure better inlining and clearer semantics

* Ensure the overflow check in TimeSpan.operator + uses ==, not !=

* Update src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs

* Respond to PR feedback and ensure argument name is passed down for factor/divisor
  • Loading branch information
tannergooding authored Nov 1, 2023
1 parent f4ee321 commit c88b783
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
}
}

internal static void ThrowIfNull([NotNull] object? argument, ExceptionArgument paramName)
{
if (argument is null)
{
ThrowHelper.ThrowArgumentNullException(paramName);
}
}

/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The pointer argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
Expand Down
30 changes: 30 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ internal static void ThrowArgumentException_DestinationTooShort()
throw new ArgumentException(SR.Argument_DestinationTooShort, "destination");
}

[DoesNotReturn]
internal static void ThrowArgumentException_InvalidTimeSpanStyles()
{
throw new ArgumentException(SR.Argument_InvalidTimeSpanStyles, "styles");
}

[DoesNotReturn]
internal static void ThrowArgumentException_OverlapAlignmentMismatch()
{
Expand Down Expand Up @@ -224,18 +230,36 @@ internal static void ThrowOverflowException()
throw new OverflowException();
}

[DoesNotReturn]
internal static void ThrowOverflowException_NegateTwosCompNum()
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

[DoesNotReturn]
internal static void ThrowOverflowException_TimeSpanTooLong()
{
throw new OverflowException(SR.Overflow_TimeSpanTooLong);
}

[DoesNotReturn]
internal static void ThrowOverflowException_TimeSpanDuration()
{
throw new OverflowException(SR.Overflow_Duration);
}

[DoesNotReturn]
internal static void ThrowArgumentException_Arg_CannotBeNaN()
{
throw new ArgumentException(SR.Arg_CannotBeNaN);
}

[DoesNotReturn]
internal static void ThrowArgumentException_Arg_CannotBeNaN(ExceptionArgument argument)
{
throw new ArgumentException(SR.Arg_CannotBeNaN, GetArgumentName(argument));
}

[DoesNotReturn]
internal static void ThrowWrongKeyTypeArgumentException<T>(T key, Type targetType)
{
Expand Down Expand Up @@ -1000,6 +1024,10 @@ private static string GetArgumentName(ExceptionArgument argument)
return "overlapped";
case ExceptionArgument.minimumBytes:
return "minimumBytes";
case ExceptionArgument.divisor:
return "divisor";
case ExceptionArgument.factor:
return "factor";
default:
Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum.");
return "";
Expand Down Expand Up @@ -1289,6 +1317,8 @@ internal enum ExceptionArgument
anyOf,
overlapped,
minimumBytes,
divisor,
factor,
}

//
Expand Down
Loading

0 comments on commit c88b783

Please sign in to comment.