Skip to content

Commit df04484

Browse files
committed
Revert "Implement type name resolution for ILLink analyzer (dotnet#106209)"
This reverts commit e5b1d02.
1 parent 01e4518 commit df04484

22 files changed

+80
-393
lines changed

src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public static ImmutableArray<DiagnosticDescriptor> GetSupportedDiagnostics ()
5252
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.UnrecognizedTypeNameInTypeGetType));
5353
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.UnrecognizedParameterInMethodCreateInstance));
5454
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.ParametersOfAssemblyCreateInstanceCannotBeAnalyzed));
55-
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.TypeNameIsNotAssemblyQualified));
5655
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.ReturnValueDoesNotMatchFeatureGuards));
5756
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.InvalidFeatureGuard));
5857

@@ -118,12 +117,11 @@ public override void Initialize (AnalysisContext context)
118117

119118
var location = GetPrimaryLocation (type.Locations);
120119

121-
var typeNameResolver = new TypeNameResolver (context.Compilation);
122120
if (type.BaseType is INamedTypeSymbol baseType)
123-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (typeNameResolver, location, baseType, context.ReportDiagnostic);
121+
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (location, baseType, context.ReportDiagnostic);
124122

125123
foreach (var interfaceType in type.Interfaces)
126-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (typeNameResolver, location, interfaceType, context.ReportDiagnostic);
124+
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (location, interfaceType, context.ReportDiagnostic);
127125
}, SymbolKind.NamedType);
128126
context.RegisterSymbolAction (context => {
129127
VerifyMemberOnlyApplyToTypesOrStrings (context, context.Symbol);

src/tools/illink/src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
optimize the analyzer even in Debug builds. Note: we still use the
1616
Debug configuration to get Debug asserts. -->
1717
<Optimize>true</Optimize>
18-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1918
</PropertyGroup>
2019

2120
<ItemGroup>
2221
<Compile Include="$(CoreLibSharedDir)System/Index.cs" />
23-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Reflection\Metadata\TypeNameHelpers.cs" />
24-
<Compile Include="$(LibrariesProjectRoot)\Common\src\System\Text\ValueStringBuilder.cs" />
2522
</ItemGroup>
2623

2724
<ItemGroup>
@@ -31,7 +28,6 @@
3128
<PrivateAssets>all</PrivateAssets>
3229
<ExcludeAssets>contentfiles</ExcludeAssets> <!-- We include our own copy of the ClosedAttribute to work in source build -->
3330
</PackageReference>
34-
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
3531
</ItemGroup>
3632

3733
<Import Project="..\ILLink.Shared\ILLink.Shared.projitems" Label="Shared" />

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/GenericArgumentDataFlow.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,28 @@ namespace ILLink.RoslynAnalyzer.TrimAnalysis
1212
{
1313
internal static class GenericArgumentDataFlow
1414
{
15-
public static void ProcessGenericArgumentDataFlow (TypeNameResolver typeNameResolver, Location location, INamedTypeSymbol type, Action<Diagnostic>? reportDiagnostic)
15+
public static void ProcessGenericArgumentDataFlow (Location location, INamedTypeSymbol type, Action<Diagnostic> reportDiagnostic)
1616
{
17-
ProcessGenericArgumentDataFlow (typeNameResolver, location, type.TypeArguments, type.TypeParameters, reportDiagnostic);
17+
ProcessGenericArgumentDataFlow (location, type.TypeArguments, type.TypeParameters, reportDiagnostic);
1818
}
1919

20-
public static void ProcessGenericArgumentDataFlow (TypeNameResolver typeNameResolver, Location location, IMethodSymbol method, Action<Diagnostic>? reportDiagnostic)
20+
public static void ProcessGenericArgumentDataFlow (Location location, IMethodSymbol method, Action<Diagnostic> reportDiagnostic)
2121
{
22-
ProcessGenericArgumentDataFlow (typeNameResolver, location, method.TypeArguments, method.TypeParameters, reportDiagnostic);
22+
ProcessGenericArgumentDataFlow (location, method.TypeArguments, method.TypeParameters, reportDiagnostic);
2323

24-
ProcessGenericArgumentDataFlow (typeNameResolver, location, method.ContainingType, reportDiagnostic);
24+
ProcessGenericArgumentDataFlow (location, method.ContainingType, reportDiagnostic);
2525
}
2626

27-
public static void ProcessGenericArgumentDataFlow (TypeNameResolver typeNameResolver, Location location, IFieldSymbol field, Action<Diagnostic>? reportDiagnostic)
27+
public static void ProcessGenericArgumentDataFlow (Location location, IFieldSymbol field, Action<Diagnostic> reportDiagnostic)
2828
{
29-
ProcessGenericArgumentDataFlow (typeNameResolver, location, field.ContainingType, reportDiagnostic);
29+
ProcessGenericArgumentDataFlow (location, field.ContainingType, reportDiagnostic);
3030
}
3131

3232
static void ProcessGenericArgumentDataFlow (
33-
TypeNameResolver typeNameResolver,
3433
Location location,
3534
ImmutableArray<ITypeSymbol> typeArguments,
3635
ImmutableArray<ITypeParameterSymbol> typeParameters,
37-
Action<Diagnostic>? reportDiagnostic)
36+
Action<Diagnostic> reportDiagnostic)
3837
{
3938
var diagnosticContext = new DiagnosticContext (location, reportDiagnostic);
4039
for (int i = 0; i < typeArguments.Length; i++) {
@@ -43,14 +42,14 @@ static void ProcessGenericArgumentDataFlow (
4342
var genericParameterValue = new GenericParameterValue (typeParameters[i]);
4443
if (genericParameterValue.DynamicallyAccessedMemberTypes != DynamicallyAccessedMemberTypes.None) {
4544
SingleValue genericArgumentValue = SingleValueExtensions.FromTypeSymbol (typeArgument)!;
46-
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic, typeNameResolver);
47-
var requireDynamicallyAccessedMembersAction = new RequireDynamicallyAccessedMembersAction (typeNameResolver, location, reportDiagnostic, reflectionAccessAnalyzer);
45+
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic);
46+
var requireDynamicallyAccessedMembersAction = new RequireDynamicallyAccessedMembersAction (diagnosticContext, reflectionAccessAnalyzer);
4847
requireDynamicallyAccessedMembersAction.Invoke (genericArgumentValue, genericParameterValue);
4948
}
5049

5150
// Recursively process generic argument data flow on the generic argument if it itself is generic
5251
if (typeArgument is INamedTypeSymbol namedTypeArgument && namedTypeArgument.IsGenericType)
53-
ProcessGenericArgumentDataFlow (typeNameResolver, location, namedTypeArgument, reportDiagnostic);
52+
ProcessGenericArgumentDataFlow (location, namedTypeArgument, reportDiagnostic);
5453
}
5554
}
5655

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/HandleCallAction.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ internal partial struct HandleCallAction
2828
ValueSetLattice<SingleValue> _multiValueLattice;
2929

3030
public HandleCallAction (
31-
TypeNameResolver typeNameResolver,
3231
Location location,
3332
ISymbol owningSymbol,
3433
IOperation operation,
@@ -40,8 +39,8 @@ public HandleCallAction (
4039
_isNewObj = operation.Kind == OperationKind.ObjectCreation;
4140
_diagnosticContext = new DiagnosticContext (location, reportDiagnostic);
4241
_annotations = FlowAnnotations.Instance;
43-
_reflectionAccessAnalyzer = new (reportDiagnostic, typeNameResolver);
44-
_requireDynamicallyAccessedMembersAction = new (typeNameResolver, location, reportDiagnostic, _reflectionAccessAnalyzer);
42+
_reflectionAccessAnalyzer = new (reportDiagnostic);
43+
_requireDynamicallyAccessedMembersAction = new (_diagnosticContext, _reflectionAccessAnalyzer);
4544
_multiValueLattice = multiValueLattice;
4645
}
4746

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/ReflectionAccessAnalyzer.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@ namespace ILLink.RoslynAnalyzer.TrimAnalysis
1515
readonly struct ReflectionAccessAnalyzer
1616
{
1717
readonly Action<Diagnostic>? _reportDiagnostic;
18-
readonly TypeNameResolver _typeNameResolver;
1918

20-
public ReflectionAccessAnalyzer (
21-
Action<Diagnostic>? reportDiagnostic,
22-
TypeNameResolver typeNameResolver)
23-
{
24-
_reportDiagnostic = reportDiagnostic;
25-
_typeNameResolver = typeNameResolver;
26-
}
19+
public ReflectionAccessAnalyzer (Action<Diagnostic>? reportDiagnostic) => _reportDiagnostic = reportDiagnostic;
2720

2821
#pragma warning disable CA1822 // Mark members as static - the other partial implementations might need to be instance methods
2922
internal void GetReflectionAccessDiagnostics (Location location, ITypeSymbol typeSymbol, DynamicallyAccessedMemberTypes requiredMemberTypes, bool declaredOnly = false)
@@ -145,10 +138,5 @@ void GetDiagnosticsForField (Location location, IFieldSymbol fieldSymbol)
145138
diagnosticContext.AddDiagnostic (DiagnosticId.DynamicallyAccessedMembersFieldAccessedViaReflection, fieldSymbol.GetDisplayName ());
146139
}
147140
}
148-
149-
internal bool TryResolveTypeNameAndMark (string typeName, in DiagnosticContext diagnosticContext, bool needsAssemblyName, [NotNullWhen (true)] out ITypeSymbol? type)
150-
{
151-
return _typeNameResolver.TryResolveTypeName (typeName, diagnosticContext, out type, needsAssemblyName);
152-
}
153141
}
154142
}
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System;
5-
using System.Diagnostics;
64
using System.Diagnostics.CodeAnalysis;
7-
using System.Reflection.Metadata;
8-
using Microsoft.CodeAnalysis;
95
using ILLink.RoslynAnalyzer.TrimAnalysis;
106
using ILLink.Shared.TypeSystemProxy;
11-
using System.Collections.Immutable;
127

138
namespace ILLink.Shared.TrimAnalysis
149
{
1510
internal partial struct RequireDynamicallyAccessedMembersAction
1611
{
17-
readonly Location _location;
18-
readonly Action<Diagnostic>? _reportDiagnostic;
1912
readonly ReflectionAccessAnalyzer _reflectionAccessAnalyzer;
20-
readonly TypeNameResolver _typeNameResolver;
2113
#pragma warning disable CA1822 // Mark members as static - the other partial implementations might need to be instance methods
2214
#pragma warning disable IDE0060 // Unused parameters - should be removed once methods are actually implemented
2315

2416
public RequireDynamicallyAccessedMembersAction (
25-
TypeNameResolver typeNameResolver,
26-
Location location,
27-
Action<Diagnostic>? reportDiagnostic,
17+
DiagnosticContext diagnosticContext,
2818
ReflectionAccessAnalyzer reflectionAccessAnalyzer)
2919
{
30-
_typeNameResolver = typeNameResolver;
31-
_location = location;
32-
_reportDiagnostic = reportDiagnostic;
20+
_diagnosticContext = diagnosticContext;
3321
_reflectionAccessAnalyzer = reflectionAccessAnalyzer;
34-
_diagnosticContext = new (location, reportDiagnostic);
3522
}
3623

3724
public partial bool TryResolveTypeNameAndMark (string typeName, bool needsAssemblyName, out TypeProxy type)
3825
{
39-
var diagnosticContext = new DiagnosticContext (_location, _reportDiagnostic);
40-
if (_reflectionAccessAnalyzer.TryResolveTypeNameAndMark (typeName, diagnosticContext, needsAssemblyName, out ITypeSymbol? foundType)) {
41-
if (foundType is INamedTypeSymbol namedType && namedType.IsGenericType)
42-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (_typeNameResolver, _location, namedType, _reportDiagnostic);
26+
// TODO: Implement type name resolution to type symbol
27+
// https://github.com/dotnet/runtime/issues/95118
4328

44-
type = new TypeProxy (foundType);
45-
return true;
46-
}
29+
// Important corner cases:
30+
// IL2105 (see it's occurences in the tests) - non-assembly qualified type name which doesn't resolve warns
31+
// - will need to figure out what analyzer should do around this.
4732

4833
type = default;
4934
return false;
5035
}
5136

5237
private partial void MarkTypeForDynamicallyAccessedMembers (in TypeProxy type, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes) =>
53-
_reflectionAccessAnalyzer.GetReflectionAccessDiagnostics (_location, type.Type, dynamicallyAccessedMemberTypes);
38+
_reflectionAccessAnalyzer.GetReflectionAccessDiagnostics (_diagnosticContext.Location, type.Type, dynamicallyAccessedMemberTypes);
5439
}
5540
}

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisAssignmentPattern.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TrimAnalysisAssignmentPattern Merge (
5555

5656
public void ReportDiagnostics (DataFlowAnalyzerContext context, Action<Diagnostic> reportDiagnostic)
5757
{
58-
var location = Operation.Syntax.GetLocation ();
58+
var diagnosticContext = new DiagnosticContext (Operation.Syntax.GetLocation (), reportDiagnostic);
5959
if (context.EnableTrimAnalyzer &&
6060
!OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope (out _) &&
6161
!FeatureContext.IsEnabled (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) {
@@ -66,9 +66,8 @@ public void ReportDiagnostics (DataFlowAnalyzerContext context, Action<Diagnosti
6666
if (targetValue is not ValueWithDynamicallyAccessedMembers targetWithDynamicallyAccessedMembers)
6767
throw new NotImplementedException ();
6868

69-
var typeNameResolver = new TypeNameResolver (context.Compilation);
70-
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic, typeNameResolver);
71-
var requireDynamicallyAccessedMembersAction = new RequireDynamicallyAccessedMembersAction (typeNameResolver, location, reportDiagnostic, reflectionAccessAnalyzer);
69+
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic);
70+
var requireDynamicallyAccessedMembersAction = new RequireDynamicallyAccessedMembersAction (diagnosticContext, reflectionAccessAnalyzer);
7271
requireDynamicallyAccessedMembersAction.Invoke (sourceValue, targetWithDynamicallyAccessedMembers);
7372
}
7473
}

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisGenericInstantiationPattern.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,17 @@ public void ReportDiagnostics (DataFlowAnalyzerContext context, Action<Diagnosti
4949
!OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope (out _) &&
5050
!FeatureContext.IsEnabled (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) {
5151
var location = Operation.Syntax.GetLocation ();
52-
var typeNameResolver = new TypeNameResolver (context.Compilation);
5352
switch (GenericInstantiation) {
5453
case INamedTypeSymbol type:
55-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (typeNameResolver, location, type, reportDiagnostic);
54+
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (location, type, reportDiagnostic);
5655
break;
5756

5857
case IMethodSymbol method:
59-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (typeNameResolver, location, method, reportDiagnostic);
58+
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (location, method, reportDiagnostic);
6059
break;
6160

6261
case IFieldSymbol field:
63-
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (typeNameResolver, location, field, reportDiagnostic);
62+
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow (location, field, reportDiagnostic);
6463
break;
6564
}
6665
}

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisMethodCallPattern.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ public void ReportDiagnostics (DataFlowAnalyzerContext context, Action<Diagnosti
7373
{
7474
var location = Operation.Syntax.GetLocation ();
7575
if (context.EnableTrimAnalyzer &&
76-
!OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope (out _) &&
76+
!OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope(out _) &&
7777
!FeatureContext.IsEnabled (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute))
7878
{
79-
var typeNameResolver = new TypeNameResolver (context.Compilation);
80-
TrimAnalysisVisitor.HandleCall (typeNameResolver, Operation, OwningSymbol, CalledMethod, Instance, Arguments, location, reportDiagnostic, default, out var _);
79+
TrimAnalysisVisitor.HandleCall(Operation, OwningSymbol, CalledMethod, Instance, Arguments, location, reportDiagnostic, default, out var _);
8180
}
8281

8382
var diagnosticContext = new DiagnosticContext (location, reportDiagnostic);

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisReflectionAccessPattern.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public TrimAnalysisReflectionAccessPattern Merge (
4646
public void ReportDiagnostics (DataFlowAnalyzerContext context, Action<Diagnostic> reportDiagnostic)
4747
{
4848
var location = Operation.Syntax.GetLocation ();
49-
var typeNameResolver = new TypeNameResolver (context.Compilation);
50-
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic, typeNameResolver);
49+
var reflectionAccessAnalyzer = new ReflectionAccessAnalyzer (reportDiagnostic);
5150
if (context.EnableTrimAnalyzer &&
5251
!OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope (out _) &&
5352
!FeatureContext.IsEnabled (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) {

0 commit comments

Comments
 (0)