From d5ccb7a12f1acceedff4520c744c820dfc27f5c2 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sun, 28 Apr 2024 09:15:30 -0700 Subject: [PATCH] Change S.R.M.TypeName to be visible from System.Reflection.Metadata only Fixes #101628 --- eng/Versions.props | 2 +- .../CustomAttributeTypeNameParser.cs | 4 +- .../ILVerification/ILVerification.projitems | 24 ------ src/coreclr/tools/ILVerify/ILVerify.csproj | 1 + .../Dataflow/TypeNameParser.Dataflow.cs | 4 +- .../ILCompiler.TypeSystem.csproj | 31 +------- .../Reflection/Metadata/AssemblyNameInfo.cs | 10 +-- .../System/Reflection/Metadata/TypeName.cs | 6 +- .../Reflection/Metadata/TypeNameParser.cs | 5 +- .../Metadata/TypeNameParserHelpers.cs | 73 +++++++++++-------- .../Metadata/TypeNameParserOptions.cs | 14 +--- .../Reflection/TypeNameParser.Helpers.cs | 10 +++ .../System.Private.CoreLib.Shared.projitems | 11 +-- 13 files changed, 78 insertions(+), 117 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 0ff857849146a..b8f3306ddecf7 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -119,7 +119,7 @@ 8.0.0 5.0.0 4.5.5 - 9.0.0-preview.4.24219.3 + 9.0.0-preview.4.24227.6 9.0.0-preview.4.24219.3 6.0.0 5.0.0 diff --git a/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs b/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs index 72779375f4697..157011df53322 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs @@ -29,6 +29,8 @@ namespace System.Reflection { internal partial struct TypeNameParser { + private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = Int32.MaxValue }; + private ModuleDesc _module; private bool _throwIfNotFound; private Func _canonResolver; @@ -36,7 +38,7 @@ internal partial struct TypeNameParser public static TypeDesc ResolveType(ModuleDesc module, string name, bool throwIfNotFound, Func canonResolver) { - if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed)) + if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed, s_typeNameParseOptions)) { ThrowHelper.ThrowTypeLoadException(name, module); } diff --git a/src/coreclr/tools/ILVerification/ILVerification.projitems b/src/coreclr/tools/ILVerification/ILVerification.projitems index 3b2c30ddef1f1..c0f3644b1ab34 100644 --- a/src/coreclr/tools/ILVerification/ILVerification.projitems +++ b/src/coreclr/tools/ILVerification/ILVerification.projitems @@ -66,30 +66,6 @@ Utilities\CustomAttributeTypeNameParser.cs - - Utilities\HexConverter.cs - - - Utilities\AssemblyNameFormatter.cs - - - Utilities\AssemblyNameParser.cs - - - Utilities\Metadata\AssemblyNameInfo.cs - - - Utilities\TypeName.cs - - - Utilities\TypeNameParserOptions.cs - - - Utilities\TypeNameParser.cs - - - Utilities\TypeNameParserHelpers.cs - System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs diff --git a/src/coreclr/tools/ILVerify/ILVerify.csproj b/src/coreclr/tools/ILVerify/ILVerify.csproj index e1915c65d6f2d..804f32ddc7bc3 100644 --- a/src/coreclr/tools/ILVerify/ILVerify.csproj +++ b/src/coreclr/tools/ILVerify/ILVerify.csproj @@ -17,6 +17,7 @@ + diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs index 5b0cfdb12b444..135693f4f60a4 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs @@ -9,6 +9,8 @@ namespace System.Reflection { internal partial struct TypeNameParser { + private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = Int32.MaxValue }; + private TypeSystemContext _context; private ModuleDesc _callingModule; private List _referencedModules; @@ -17,7 +19,7 @@ internal partial struct TypeNameParser public static TypeDesc ResolveType(string name, ModuleDesc callingModule, TypeSystemContext context, List referencedModules, out bool typeWasNotFoundInAssemblyNorBaseLibrary) { - if (!TypeName.TryParse(name, out TypeName parsed)) + if (!TypeName.TryParse(name, out TypeName parsed, s_typeNameParseOptions)) { typeWasNotFoundInAssemblyNorBaseLibrary = false; return null; diff --git a/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj b/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj index 23afa3780b1ab..1a5db3b44445d 100644 --- a/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj +++ b/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj @@ -198,39 +198,12 @@ Utilities\CustomAttributeTypeNameParser.cs - - Utilities\HexConverter.cs - - - Utilities\AssemblyNameFormatter.cs - - - Utilities\AssemblyNameParser.cs - - - Utilities\AssemblyNameInfo.cs - - - Utilities\TypeName.cs - - - Utilities\TypeNameParser.cs - - - Utilities\TypeNameParserHelpers.cs - - - Utilities\TypeNameParserOptions.cs - Utilities\CustomAttributeTypeNameParser.Helpers Utilities\ValueStringBuilder.cs - - Utilities\ValueStringBuilder.AppendSpanFormattable.cs - Utilities\GCPointerMap.Algorithm.cs @@ -741,6 +714,10 @@ + + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs b/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs index ee586e3b57a8c..cb4de9467f660 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs @@ -21,10 +21,10 @@ namespace System.Reflection.Metadata /// It's a more lightweight, immutable version of that does not pre-allocate instances. /// [DebuggerDisplay("{FullName}")] -#if SYSTEM_PRIVATE_CORELIB - internal -#else +#if SYSTEM_REFLECTION_METADATA public +#else + internal #endif sealed class AssemblyNameInfo { @@ -182,11 +182,7 @@ public AssemblyName ToAssemblyName() public static AssemblyNameInfo Parse(ReadOnlySpan assemblyName) => TryParse(assemblyName, out AssemblyNameInfo? result) ? result! -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB : throw new ArgumentException(SR.InvalidAssemblyName, nameof(assemblyName)); -#else // tools that reference this file as a link - : throw new ArgumentException("The given assembly name was invalid.", nameof(assemblyName)); -#endif /// /// Tries to parse a span of characters into an assembly name. diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs index 2fac2f8ffd74d..1464f0248280b 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs @@ -16,10 +16,10 @@ namespace System.Reflection.Metadata { [DebuggerDisplay("{AssemblyQualifiedName}")] -#if SYSTEM_PRIVATE_CORELIB - internal -#else +#if SYSTEM_REFLECTION_METADATA public +#else + internal #endif sealed class TypeName { diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs index 7c75f3ee844d6..178aa79f3d0e4 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs @@ -17,7 +17,8 @@ namespace System.Reflection.Metadata [DebuggerDisplay("{_inputString}")] internal ref struct TypeNameParser { - private static readonly TypeNameParseOptions _defaults = new(); + private static readonly TypeNameParseOptions s_defaults = new(); + private readonly bool _throwOnError; private readonly TypeNameParseOptions _parseOptions; private ReadOnlySpan _inputString; @@ -26,7 +27,7 @@ private TypeNameParser(ReadOnlySpan name, bool throwOnError, TypeNameParse { _inputString = name; _throwOnError = throwOnError; - _parseOptions = options ?? _defaults; + _parseOptions = options ?? s_defaults; } internal static TypeName? Parse(ReadOnlySpan typeName, bool throwOnError, TypeNameParseOptions? options = default) diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs index 3783f56c73d4b..1cdc5e230ad96 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs @@ -331,53 +331,64 @@ internal static bool TryStripFirstCharAndTrailingSpaces(ref ReadOnlySpan s } [DoesNotReturn] - internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit) => throw -#if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit)); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); -#endif + internal static void ThrowArgumentException_InvalidTypeName(int errorIndex) + { + throw new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}"); + } [DoesNotReturn] - internal static void ThrowArgumentException_InvalidTypeName(int errorIndex) => throw -#if SYSTEM_PRIVATE_CORELIB - new ArgumentException(SR.Arg_ArgumentException, $"typeName@{errorIndex}"); -#elif SYSTEM_REFLECTION_METADATA - new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}"); -#else // tools that reference this file as a link - new ArgumentException(); + internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit) + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit)); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NotGenericType() => throw -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB - new InvalidOperationException(SR.InvalidOperation_NotGenericType); -#else // tools that reference this file as a link - new InvalidOperationException(); + internal static void ThrowInvalidOperation_NotGenericType() + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.InvalidOperation_NotGenericType); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NotNestedType() => throw + internal static void ThrowInvalidOperation_NotNestedType() + { #if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.InvalidOperation_NotNestedType); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); + throw new InvalidOperationException(SR.InvalidOperation_NotNestedType); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NoElement() => throw + internal static void ThrowInvalidOperation_NoElement() + { #if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.InvalidOperation_NoElement); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); + throw new InvalidOperationException(SR.InvalidOperation_NoElement); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_HasToBeArrayClass() => throw -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB - new InvalidOperationException(SR.Argument_HasToBeArrayClass); -#else // tools that reference this file as a link - new InvalidOperationException(); + internal static void ThrowInvalidOperation_HasToBeArrayClass() + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.Argument_HasToBeArrayClass); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } } } diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs index 66350c9a44cac..b7420c40aa9ea 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs @@ -3,19 +3,9 @@ namespace System.Reflection.Metadata { -#if SYSTEM_PRIVATE_CORELIB - internal -#else - public -#endif - sealed class TypeNameParseOptions + public sealed class TypeNameParseOptions { - private int _maxNodes = -#if SYSTEM_PRIVATE_CORELIB - int.MaxValue; // CoreLib has never introduced any limits -#else - 20; -#endif + private int _maxNodes = 20; /// /// Limits the maximum value of node count that parser can handle. diff --git a/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs b/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs index bcb2125d3f4ea..a7f969d3f0065 100644 --- a/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs +++ b/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs @@ -8,6 +8,16 @@ #nullable enable +#if SYSTEM_PRIVATE_CORELIB +namespace System.Reflection.Metadata +{ + internal struct TypeNameParseOptions + { + public int MaxNodes => int.MaxValue; // CoreLib does not enforce any limits + } +} +#endif + namespace System.Reflection { internal partial struct TypeNameParser diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index fccffa63fa159..f1b61a1ea67c4 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1424,6 +1424,9 @@ Common\System\LocalAppContextSwitches.Common.cs + + Common\System\HexConverter.cs + Common\System\HResults.cs @@ -1493,9 +1496,6 @@ Common\System\Reflection\Metadata\TypeNameParserHelpers.cs - - Common\System\Reflection\Metadata\TypeNameParserOptions.cs - Common\System\Runtime\Versioning\NonVersionableAttribute.cs @@ -1599,11 +1599,6 @@ - - - Common\System\HexConverter.cs - - Common\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs