diff options
Diffstat (limited to 'src/mscorlib/src/System/ThrowHelper.cs')
-rw-r--r-- | src/mscorlib/src/System/ThrowHelper.cs | 211 |
1 files changed, 169 insertions, 42 deletions
diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index 3105d56f7c..a534dec818 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -10,7 +10,7 @@ namespace System { // The old way to throw an exception generates quite a lot IL code and assembly code. // Following is an example: // C# source - // throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); + // throw new ArgumentNullException(nameof(key), Environment.GetResourceString("ArgumentNull_Key")); // IL code: // IL_0003: ldstr "key" // IL_0008: ldstr "ArgumentNull_Key" @@ -39,112 +39,206 @@ namespace System { using Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.Serialization; + using System.Diagnostics; using System.Diagnostics.Contracts; [Pure] internal static class ThrowHelper { +#if FEATURE_SPAN_OF_T + internal static void ThrowArrayTypeMismatchException() { + throw new ArrayTypeMismatchException(); + } + + internal static void ThrowInvalidTypeWithPointersNotSupported(Type targetType) { + throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeWithPointersNotSupported", targetType)); + } + + internal static void ThrowIndexOutOfRangeException() { + throw new IndexOutOfRangeException(); + } + + internal static void ThrowArgumentOutOfRangeException() { + throw new ArgumentOutOfRangeException(); + } + + internal static void ThrowArgumentException_DestinationTooShort() { + throw new ArgumentException(Environment.GetResourceString("Argument_DestinationTooShort")); + } + + internal static void ThrowNotSupportedException_CannotCallEqualsOnSpan() { + throw new NotSupportedException(Environment.GetResourceString("NotSupported_CannotCallEqualsOnSpan")); + } + + internal static void ThrowNotSupportedException_CannotCallGetHashCodeOnSpan() { + throw new NotSupportedException(Environment.GetResourceString("NotSupported_CannotCallGetHashCodeOnSpan")); + } +#endif + internal static void ThrowArgumentOutOfRange_IndexException() { - throw new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.index), - Environment.GetResourceString(GetResourceName(ExceptionResource.ArgumentOutOfRange_Index))); + throw GetArgumentOutOfRangeException(ExceptionArgument.index, + ExceptionResource.ArgumentOutOfRange_Index); } internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException() { - throw new ArgumentOutOfRangeException( - GetArgumentName(ExceptionArgument.index), - Environment.GetResourceString(GetResourceName(ExceptionResource.ArgumentOutOfRange_NeedNonNegNum))); + throw GetArgumentOutOfRangeException(ExceptionArgument.index, + ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + } + + internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum() { + throw GetArgumentOutOfRangeException(ExceptionArgument.length, + ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + } + + internal static void ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index() { + throw GetArgumentOutOfRangeException(ExceptionArgument.startIndex, + ExceptionResource.ArgumentOutOfRange_Index); + } + + internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count() { + throw GetArgumentOutOfRangeException(ExceptionArgument.count, + ExceptionResource.ArgumentOutOfRange_Count); } internal static void ThrowWrongKeyTypeArgumentException(object key, Type targetType) { - throw new ArgumentException(Environment.GetResourceString("Arg_WrongType", key, targetType), "key"); + throw GetWrongKeyTypeArgumentException(key, targetType); } internal static void ThrowWrongValueTypeArgumentException(object value, Type targetType) { - throw new ArgumentException(Environment.GetResourceString("Arg_WrongType", value, targetType), "value"); + throw GetWrongValueTypeArgumentException(value, targetType); + } + + private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object key) { + return new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicateWithKey", key)); } -#if FEATURE_CORECLR internal static void ThrowAddingDuplicateWithKeyArgumentException(object key) { - throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicateWithKey", key)); + throw GetAddingDuplicateWithKeyArgumentException(key); } -#endif internal static void ThrowKeyNotFoundException() { throw new System.Collections.Generic.KeyNotFoundException(); } internal static void ThrowArgumentException(ExceptionResource resource) { - throw new ArgumentException(Environment.GetResourceString(GetResourceName(resource))); + throw GetArgumentException(resource); } internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument) { - throw new ArgumentException(Environment.GetResourceString(GetResourceName(resource)), GetArgumentName(argument)); + throw GetArgumentException(resource, argument); } internal static void ThrowArgumentNullException(ExceptionArgument argument) { throw new ArgumentNullException(GetArgumentName(argument)); } + internal static void ThrowArgumentNullException(ExceptionResource resource) { + throw new ArgumentNullException(GetResourceString(resource)); + } + internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) { throw new ArgumentOutOfRangeException(GetArgumentName(argument)); } internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) { - throw new ArgumentOutOfRangeException(GetArgumentName(argument), - Environment.GetResourceString(GetResourceName(resource))); + throw GetArgumentOutOfRangeException(argument, resource); } internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource) { - throw new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", - Environment.GetResourceString(GetResourceName(resource))); + throw GetArgumentOutOfRangeException(argument, paramNumber, resource); } internal static void ThrowInvalidOperationException(ExceptionResource resource) { - throw new InvalidOperationException(Environment.GetResourceString(GetResourceName(resource))); + throw GetInvalidOperationException(resource); } internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e) { - throw new InvalidOperationException(Environment.GetResourceString(GetResourceName(resource)), e); + throw new InvalidOperationException(GetResourceString(resource), e); } internal static void ThrowSerializationException(ExceptionResource resource) { - throw new SerializationException(Environment.GetResourceString(GetResourceName(resource))); + throw new SerializationException(GetResourceString(resource)); } internal static void ThrowSecurityException(ExceptionResource resource) { - throw new System.Security.SecurityException(Environment.GetResourceString(GetResourceName(resource))); + throw new System.Security.SecurityException(GetResourceString(resource)); } internal static void ThrowRankException(ExceptionResource resource) { - throw new RankException(Environment.GetResourceString(GetResourceName(resource))); + throw new RankException(GetResourceString(resource)); } internal static void ThrowNotSupportedException(ExceptionResource resource) { - throw new NotSupportedException(Environment.GetResourceString(GetResourceName(resource))); + throw new NotSupportedException(GetResourceString(resource)); } internal static void ThrowUnauthorizedAccessException(ExceptionResource resource) { - throw new UnauthorizedAccessException(Environment.GetResourceString(GetResourceName(resource))); + throw new UnauthorizedAccessException(GetResourceString(resource)); } internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource) { - throw new ObjectDisposedException(objectName, Environment.GetResourceString(GetResourceName(resource))); + throw new ObjectDisposedException(objectName, GetResourceString(resource)); } - internal static void ThrowObjectDisposedException(ExceptionResource resource) - { - throw new ObjectDisposedException(null, Environment.GetResourceString(GetResourceName(resource))); + internal static void ThrowObjectDisposedException(ExceptionResource resource) { + throw new ObjectDisposedException(null, GetResourceString(resource)); } - internal static void ThrowNotSupportedException() - { + internal static void ThrowNotSupportedException() { throw new NotSupportedException(); } - internal static void ThrowAggregateException(List<Exception> exceptions) - { + internal static void ThrowAggregateException(List<Exception> exceptions) { throw new AggregateException(exceptions); } + internal static void ThrowArgumentException_Argument_InvalidArrayType() { + throw GetArgumentException(ExceptionResource.Argument_InvalidArrayType); + } + + internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted() { + throw GetInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted); + } + + internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded() { + throw GetInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded); + } + + internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion() { + throw GetInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + } + + internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen() { + throw GetInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + } + + private static ArgumentException GetArgumentException(ExceptionResource resource) { + return new ArgumentException(GetResourceString(resource)); + } + + private static InvalidOperationException GetInvalidOperationException(ExceptionResource resource) { + return new InvalidOperationException(GetResourceString(resource)); + } + + private static ArgumentException GetWrongKeyTypeArgumentException(object key, Type targetType) { + return new ArgumentException(Environment.GetResourceString("Arg_WrongType", key, targetType), nameof(key)); + } + + private static ArgumentException GetWrongValueTypeArgumentException(object value, Type targetType) { + return new ArgumentException(Environment.GetResourceString("Arg_WrongType", value, targetType), nameof(value)); + } + + private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) { + return new ArgumentOutOfRangeException(GetArgumentName(argument), GetResourceString(resource)); + } + + private static ArgumentException GetArgumentException(ExceptionResource resource, ExceptionArgument argument) { + return new ArgumentException(GetResourceString(resource), GetArgumentName(argument)); + } + + private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource) { + return new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", GetResourceString(resource)); + } + // Allow nulls for reference types and Nullable<U>, but not for value types. // Aggressively inline so the jit evaluates the if in place and either drops the call altogether // Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException @@ -155,26 +249,43 @@ namespace System { ThrowHelper.ThrowArgumentNullException(argName); } - // // This function will convert an ExceptionArgument enum value to the argument name string. - // - internal static string GetArgumentName(ExceptionArgument argument) { - Contract.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), + private static string GetArgumentName(ExceptionArgument argument) { + // This is indirected through a second NoInlining function it has a special meaning + // in System.Private.CoreLib of indicatating it takes a StackMark which cause + // the caller to also be not inlined; so we can't mark it directly. + // So is the effect of marking this function as non-inlining in a regular situation. + return GetArgumentNameInner(argument); + } + + // This function will convert an ExceptionArgument enum value to the argument name string. + // Second function in chain so as to not propergate the non-inlining to outside caller + [MethodImpl(MethodImplOptions.NoInlining)] + private static string GetArgumentNameInner(ExceptionArgument argument) { + Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), "The enum value is not defined, please check the ExceptionArgument Enum."); return argument.ToString(); } - // // This function will convert an ExceptionResource enum value to the resource string. - // - internal static string GetResourceName(ExceptionResource resource) { - Contract.Assert(Enum.IsDefined(typeof(ExceptionResource), resource), + private static string GetResourceString(ExceptionResource resource) { + // This is indirected through a second NoInlining function it has a special meaning + // in System.Private.CoreLib of indicatating it takes a StackMark which cause + // the caller to also be not inlined; so we can't mark it directly. + // So is the effect of marking this function as non-inlining in a regular situation. + return GetResourceStringInner(resource); + } + + // This function will convert an ExceptionResource enum value to the resource string. + // Second function in chain so as to not propergate the non-inlining to outside caller + [MethodImpl(MethodImplOptions.NoInlining)] + private static string GetResourceStringInner(ExceptionResource resource) { + Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource), "The enum value is not defined, please check the ExceptionResource Enum."); - return resource.ToString(); + return Environment.GetResourceString(resource.ToString()); } - } // @@ -247,6 +358,11 @@ namespace System { beginMethod, continuationOptions, continuationAction, + valueFactory, + addValueFactory, + updateValueFactory, + concurrencyLevel, + text, } @@ -341,6 +457,17 @@ namespace System { TaskCompletionSourceT_TrySetException_NullException, TaskCompletionSourceT_TrySetException_NoExceptions, InvalidOperation_WrongAsyncResultOrEndCalledMultiple, + ConcurrentDictionary_ConcurrencyLevelMustBePositive, + ConcurrentDictionary_CapacityMustNotBeNegative, + ConcurrentDictionary_TypeOfValueIncorrect, + ConcurrentDictionary_TypeOfKeyIncorrect, + ConcurrentDictionary_SourceContainsDuplicateKeys, + ConcurrentDictionary_KeyAlreadyExisted, + ConcurrentDictionary_ItemKeyIsNull, + ConcurrentDictionary_IndexIsNegative, + ConcurrentDictionary_ArrayNotLargeEnough, + ConcurrentDictionary_ArrayIncorrectType, + ConcurrentCollection_SyncRoot_NotSupported, } } |