Skip to content

Commit 5c6a76d

Browse files
committed
Change S.R.M.TypeName to be consumed from System.Reflection.Metadata package
Contributes to #101541 Fixes #101628 Better unreachable code elimination in CoreLib
1 parent 6313ff8 commit 5c6a76d

File tree

13 files changed

+95
-120
lines changed

13 files changed

+95
-120
lines changed

eng/Versions.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<SystemDrawingCommonVersion>8.0.0</SystemDrawingCommonVersion>
120120
<SystemIOFileSystemAccessControlVersion>5.0.0</SystemIOFileSystemAccessControlVersion>
121121
<SystemMemoryVersion>4.5.5</SystemMemoryVersion>
122-
<SystemReflectionMetadataVersion>9.0.0-preview.4.24219.3</SystemReflectionMetadataVersion>
122+
<SystemReflectionMetadataVersion>9.0.0-preview.4.24227.6</SystemReflectionMetadataVersion>
123123
<SystemReflectionMetadataLoadContextVersion>9.0.0-preview.4.24219.3</SystemReflectionMetadataLoadContextVersion>
124124
<SystemSecurityAccessControlVersion>6.0.0</SystemSecurityAccessControlVersion>
125125
<SystemSecurityCryptographyCngVersion>5.0.0</SystemSecurityCryptographyCngVersion>
@@ -210,7 +210,7 @@
210210
<!-- Mono Cecil -->
211211
<MicrosoftDotNetCecilVersion>0.11.4-alpha.24222.1</MicrosoftDotNetCecilVersion>
212212
<!-- ILCompiler -->
213-
<MicrosoftDotNetILCompilerVersion>9.0.0-preview.4.24219.3</MicrosoftDotNetILCompilerVersion>
213+
<MicrosoftDotNetILCompilerVersion>9.0.0-preview.4.24227.6</MicrosoftDotNetILCompilerVersion>
214214
<!-- ICU -->
215215
<MicrosoftNETCoreRuntimeICUTransportVersion>9.0.0-preview.5.24223.2</MicrosoftNETCoreRuntimeICUTransportVersion>
216216
<!-- MsQuic -->

src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ namespace System.Reflection
2929
{
3030
internal partial struct TypeNameParser
3131
{
32+
private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = Int32.MaxValue };
33+
3234
private ModuleDesc _module;
3335
private bool _throwIfNotFound;
3436
private Func<ModuleDesc, string, MetadataType> _canonResolver;
3537

3638
public static TypeDesc ResolveType(ModuleDesc module, string name, bool throwIfNotFound,
3739
Func<ModuleDesc, string, MetadataType> canonResolver)
3840
{
39-
if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed))
41+
if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed, s_typeNameParseOptions))
4042
{
4143
ThrowHelper.ThrowTypeLoadException(name, module);
4244
}

src/coreclr/tools/ILVerification/ILVerification.projitems

-24
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,6 @@
6666
<Compile Include="$(ToolsCommonPath)TypeSystem\Common\Utilities\CustomAttributeTypeNameParser.cs">
6767
<Link>Utilities\CustomAttributeTypeNameParser.cs</Link>
6868
</Compile>
69-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\HexConverter.cs">
70-
<Link>Utilities\HexConverter.cs</Link>
71-
</Compile>
72-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\AssemblyNameFormatter.cs">
73-
<Link>Utilities\AssemblyNameFormatter.cs</Link>
74-
</Compile>
75-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\AssemblyNameParser.cs">
76-
<Link>Utilities\AssemblyNameParser.cs</Link>
77-
</Compile>
78-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\AssemblyNameInfo.cs">
79-
<Link>Utilities\Metadata\AssemblyNameInfo.cs</Link>
80-
</Compile>
81-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeName.cs">
82-
<Link>Utilities\TypeName.cs</Link>
83-
</Compile>
84-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParserOptions.cs">
85-
<Link>Utilities\TypeNameParserOptions.cs</Link>
86-
</Compile>
87-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParser.cs">
88-
<Link>Utilities\TypeNameParser.cs</Link>
89-
</Compile>
90-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParserHelpers.cs">
91-
<Link>Utilities\TypeNameParserHelpers.cs</Link>
92-
</Compile>
9369
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
9470
<Link>System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs</Link>
9571
</Compile>

src/coreclr/tools/ILVerify/ILVerify.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
2021
<PackageReference Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
2122
</ItemGroup>
2223
</Project>

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace System.Reflection
99
{
1010
internal partial struct TypeNameParser
1111
{
12+
private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = Int32.MaxValue };
13+
1214
private TypeSystemContext _context;
1315
private ModuleDesc _callingModule;
1416
private List<ModuleDesc> _referencedModules;
@@ -17,7 +19,7 @@ internal partial struct TypeNameParser
1719
public static TypeDesc ResolveType(string name, ModuleDesc callingModule,
1820
TypeSystemContext context, List<ModuleDesc> referencedModules, out bool typeWasNotFoundInAssemblyNorBaseLibrary)
1921
{
20-
if (!TypeName.TryParse(name, out TypeName parsed))
22+
if (!TypeName.TryParse(name, out TypeName parsed, s_typeNameParseOptions))
2123
{
2224
typeWasNotFoundInAssemblyNorBaseLibrary = false;
2325
return null;

src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj

+4-27
Original file line numberDiff line numberDiff line change
@@ -198,39 +198,12 @@
198198
<Compile Include="..\..\Common\TypeSystem\Common\Utilities\CustomAttributeTypeNameParser.cs">
199199
<Link>Utilities\CustomAttributeTypeNameParser.cs</Link>
200200
</Compile>
201-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\HexConverter.cs">
202-
<Link>Utilities\HexConverter.cs</Link>
203-
</Compile>
204-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\AssemblyNameFormatter.cs">
205-
<Link>Utilities\AssemblyNameFormatter.cs</Link>
206-
</Compile>
207-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\AssemblyNameParser.cs">
208-
<Link>Utilities\AssemblyNameParser.cs</Link>
209-
</Compile>
210-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\AssemblyNameInfo.cs">
211-
<Link>Utilities\AssemblyNameInfo.cs</Link>
212-
</Compile>
213-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeName.cs">
214-
<Link>Utilities\TypeName.cs</Link>
215-
</Compile>
216-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParser.cs">
217-
<Link>Utilities\TypeNameParser.cs</Link>
218-
</Compile>
219-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParserHelpers.cs">
220-
<Link>Utilities\TypeNameParserHelpers.cs</Link>
221-
</Compile>
222-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameParserOptions.cs">
223-
<Link>Utilities\TypeNameParserOptions.cs</Link>
224-
</Compile>
225201
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\TypeNameParser.Helpers.cs">
226202
<Link>Utilities\CustomAttributeTypeNameParser.Helpers</Link>
227203
</Compile>
228204
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Text\ValueStringBuilder.cs">
229205
<Link>Utilities\ValueStringBuilder.cs</Link>
230206
</Compile>
231-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Text\ValueStringBuilder.AppendSpanFormattable.cs">
232-
<Link>Utilities\ValueStringBuilder.AppendSpanFormattable.cs</Link>
233-
</Compile>
234207
<Compile Include="..\..\Common\TypeSystem\Common\Utilities\GCPointerMap.Algorithm.cs">
235208
<Link>Utilities\GCPointerMap.Algorithm.cs</Link>
236209
</Compile>
@@ -741,6 +714,10 @@
741714
<AdditionalFiles Include="BannedSymbols.txt" />
742715
</ItemGroup>
743716

717+
<ItemGroup>
718+
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
719+
</ItemGroup>
720+
744721
<ItemGroup>
745722
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="$(MicrosoftCodeAnalysisBannedApiAnalyzersVersion)" Condition="'$(DotNetBuildSourceOnly)' != 'true'">
746723
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ namespace System.Reflection.Metadata
2121
/// It's a more lightweight, immutable version of <seealso cref="AssemblyName"/> that does not pre-allocate <seealso cref="System.Globalization.CultureInfo"/> instances.
2222
/// </remarks>
2323
[DebuggerDisplay("{FullName}")]
24-
#if SYSTEM_PRIVATE_CORELIB
25-
internal
26-
#else
24+
#if SYSTEM_REFLECTION_METADATA
2725
public
26+
#else
27+
internal
2828
#endif
2929
sealed class AssemblyNameInfo
3030
{
@@ -182,11 +182,7 @@ public AssemblyName ToAssemblyName()
182182
public static AssemblyNameInfo Parse(ReadOnlySpan<char> assemblyName)
183183
=> TryParse(assemblyName, out AssemblyNameInfo? result)
184184
? result!
185-
#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB
186185
: throw new ArgumentException(SR.InvalidAssemblyName, nameof(assemblyName));
187-
#else // tools that reference this file as a link
188-
: throw new ArgumentException("The given assembly name was invalid.", nameof(assemblyName));
189-
#endif
190186

191187
/// <summary>
192188
/// Tries to parse a span of characters into an assembly name.

src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
namespace System.Reflection.Metadata
1717
{
1818
[DebuggerDisplay("{AssemblyQualifiedName}")]
19-
#if SYSTEM_PRIVATE_CORELIB
20-
internal
21-
#else
19+
#if SYSTEM_REFLECTION_METADATA
2220
public
21+
#else
22+
internal
2323
#endif
2424
sealed class TypeName
2525
{

src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace System.Reflection.Metadata
1717
[DebuggerDisplay("{_inputString}")]
1818
internal ref struct TypeNameParser
1919
{
20-
private static readonly TypeNameParseOptions _defaults = new();
20+
private static readonly TypeNameParseOptions s_defaults = new();
21+
2122
private readonly bool _throwOnError;
2223
private readonly TypeNameParseOptions _parseOptions;
2324
private ReadOnlySpan<char> _inputString;
@@ -26,7 +27,7 @@ private TypeNameParser(ReadOnlySpan<char> name, bool throwOnError, TypeNameParse
2627
{
2728
_inputString = name;
2829
_throwOnError = throwOnError;
29-
_parseOptions = options ?? _defaults;
30+
_parseOptions = options ?? s_defaults;
3031
}
3132

3233
internal static TypeName? Parse(ReadOnlySpan<char> typeName, bool throwOnError, TypeNameParseOptions? options = default)
@@ -50,7 +51,7 @@ private TypeNameParser(ReadOnlySpan<char> name, bool throwOnError, TypeNameParse
5051
{
5152
if (throwOnError)
5253
{
53-
if (recursiveDepth >= parser._parseOptions.MaxNodes)
54+
if (parser._parseOptions.IsMaxDepthExceeded(recursiveDepth))
5455
{
5556
ThrowInvalidOperation_MaxNodesExceeded(parser._parseOptions.MaxNodes);
5657
}
@@ -249,7 +250,7 @@ private bool TryParseAssemblyName(ref AssemblyNameInfo? assemblyName)
249250

250251
private bool TryDive(ref int depth)
251252
{
252-
if (depth >= _parseOptions.MaxNodes)
253+
if (_parseOptions.IsMaxDepthExceeded(depth))
253254
{
254255
return false;
255256
}

src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs

+42-31
Original file line numberDiff line numberDiff line change
@@ -331,53 +331,64 @@ internal static bool TryStripFirstCharAndTrailingSpaces(ref ReadOnlySpan<char> s
331331
}
332332

333333
[DoesNotReturn]
334-
internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit) => throw
335-
#if SYSTEM_REFLECTION_METADATA
336-
new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit));
337-
#else // corelib and tools that reference this file as a link
338-
new InvalidOperationException();
339-
#endif
334+
internal static void ThrowArgumentException_InvalidTypeName(int errorIndex)
335+
{
336+
throw new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}");
337+
}
340338

341339
[DoesNotReturn]
342-
internal static void ThrowArgumentException_InvalidTypeName(int errorIndex) => throw
343-
#if SYSTEM_PRIVATE_CORELIB
344-
new ArgumentException(SR.Arg_ArgumentException, $"typeName@{errorIndex}");
345-
#elif SYSTEM_REFLECTION_METADATA
346-
new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}");
347-
#else // tools that reference this file as a link
348-
new ArgumentException();
340+
internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit)
341+
{
342+
#if SYSTEM_REFLECTION_METADATA
343+
throw new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit));
344+
#else
345+
Debug.Fail("Expected to be unreachable");
346+
throw new InvalidOperationException();
349347
#endif
348+
}
350349

351350
[DoesNotReturn]
352-
internal static void ThrowInvalidOperation_NotGenericType() => throw
353-
#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB
354-
new InvalidOperationException(SR.InvalidOperation_NotGenericType);
355-
#else // tools that reference this file as a link
356-
new InvalidOperationException();
351+
internal static void ThrowInvalidOperation_NotGenericType()
352+
{
353+
#if SYSTEM_REFLECTION_METADATA
354+
throw new InvalidOperationException(SR.InvalidOperation_NotGenericType);
355+
#else
356+
Debug.Fail("Expected to be unreachable");
357+
throw new InvalidOperationException();
357358
#endif
359+
}
358360

359361
[DoesNotReturn]
360-
internal static void ThrowInvalidOperation_NotNestedType() => throw
362+
internal static void ThrowInvalidOperation_NotNestedType()
363+
{
361364
#if SYSTEM_REFLECTION_METADATA
362-
new InvalidOperationException(SR.InvalidOperation_NotNestedType);
363-
#else // corelib and tools that reference this file as a link
364-
new InvalidOperationException();
365+
throw new InvalidOperationException(SR.InvalidOperation_NotNestedType);
366+
#else
367+
Debug.Fail("Expected to be unreachable");
368+
throw new InvalidOperationException();
365369
#endif
370+
}
366371

367372
[DoesNotReturn]
368-
internal static void ThrowInvalidOperation_NoElement() => throw
373+
internal static void ThrowInvalidOperation_NoElement()
374+
{
369375
#if SYSTEM_REFLECTION_METADATA
370-
new InvalidOperationException(SR.InvalidOperation_NoElement);
371-
#else // corelib and tools that reference this file as a link
372-
new InvalidOperationException();
376+
throw new InvalidOperationException(SR.InvalidOperation_NoElement);
377+
#else
378+
Debug.Fail("Expected to be unreachable");
379+
throw new InvalidOperationException();
373380
#endif
381+
}
374382

375383
[DoesNotReturn]
376-
internal static void ThrowInvalidOperation_HasToBeArrayClass() => throw
377-
#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB
378-
new InvalidOperationException(SR.Argument_HasToBeArrayClass);
379-
#else // tools that reference this file as a link
380-
new InvalidOperationException();
384+
internal static void ThrowInvalidOperation_HasToBeArrayClass()
385+
{
386+
#if SYSTEM_REFLECTION_METADATA
387+
throw new InvalidOperationException(SR.Argument_HasToBeArrayClass);
388+
#else
389+
Debug.Fail("Expected to be unreachable");
390+
throw new InvalidOperationException();
381391
#endif
392+
}
382393
}
383394
}

src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,9 @@
33

44
namespace System.Reflection.Metadata
55
{
6-
#if SYSTEM_PRIVATE_CORELIB
7-
internal
8-
#else
9-
public
10-
#endif
11-
sealed class TypeNameParseOptions
6+
public sealed class TypeNameParseOptions
127
{
13-
private int _maxNodes =
14-
#if SYSTEM_PRIVATE_CORELIB
15-
int.MaxValue; // CoreLib has never introduced any limits
16-
#else
17-
20;
18-
#endif
8+
private int _maxNodes = 20;
199

2010
/// <summary>
2111
/// Limits the maximum value of <seealso cref="TypeName.GetNodeCount">node count</seealso> that parser can handle.
@@ -37,5 +27,7 @@ public int MaxNodes
3727
_maxNodes = value;
3828
}
3929
}
30+
31+
internal bool IsMaxDepthExceeded(int depth) => depth >= _maxNodes;
4032
}
4133
}

src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88

99
#nullable enable
1010

11+
#if SYSTEM_PRIVATE_CORELIB
12+
namespace System.Reflection.Metadata
13+
{
14+
internal struct TypeNameParseOptions
15+
{
16+
public TypeNameParseOptions() { }
17+
#pragma warning disable CA1822 // Mark members as static
18+
// CoreLib does not enforce any limits
19+
public bool IsMaxDepthExceeded(int _) => false;
20+
public int MaxNodes
21+
{
22+
get
23+
{
24+
Debug.Fail("Expected to be unreachable");
25+
return 0;
26+
}
27+
}
28+
#pragma warning restore CA1822
29+
}
30+
}
31+
#endif
32+
1133
namespace System.Reflection
1234
{
1335
internal partial struct TypeNameParser

0 commit comments

Comments
 (0)