Skip to content

Commit f6ecbe5

Browse files
authored
Share MakeGeneric.. and Expression.Call (dotnet/linker#2758)
Moves the intrinsic handling of `MakeGenericType`, `MakeGenericMethod` and `Expression.Call` into the shared code. Other changes: * Some small refactorings on the shared type system and value system. * Expose generic parameters on type system proxies * Fix a bug in analyzer when we're recording patterns, the values must be cloned (as they can mutate during analysis after the record is taken) * Fix some problems in Nullable<T> handling in MakeGenericType - specifically if we don't know what the T is going to be, we now return just Nullable<> known type (but unknown T). * Add couple new tests - update existing ones (mainly due to limitation in the analyzer) Co-authored-by: Sven Boemer <[email protected]> Commit migrated from dotnet/linker@bf4d43b
1 parent ff13443 commit f6ecbe5

27 files changed

+440
-304
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ static ImmutableArray<DiagnosticDescriptor> GetSupportedDiagnostics ()
4242
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.DynamicallyAccessedMembersMismatchOnImplicitThisBetweenOverrides));
4343
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.DynamicallyAccessedMembersConflictsBetweenPropertyAndAccessor));
4444
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.PropertyAccessorParameterInLinqExpressionsCannotBeStaticallyDetermined));
45+
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.MakeGenericType));
46+
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.MakeGenericMethod));
4547
return diagDescriptorsArrayBuilder.ToImmutable ();
4648

4749
void AddRange (DiagnosticId first, DiagnosticId last)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ internal readonly partial struct GenericParameterProxy
99
{
1010
public GenericParameterProxy (ITypeParameterSymbol typeParameterSymbol) => TypeParameterSymbol = typeParameterSymbol;
1111

12+
internal partial bool HasDefaultConstructorConstraint () =>
13+
TypeParameterSymbol.HasConstructorConstraint |
14+
TypeParameterSymbol.HasValueTypeConstraint |
15+
TypeParameterSymbol.HasUnmanagedTypeConstraint;
16+
1217
public readonly ITypeParameterSymbol TypeParameterSymbol;
1318

1419
public override string ToString () => TypeParameterSymbol.ToString ();

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ partial record GenericParameterValue
1717
{
1818
public GenericParameterValue (ITypeParameterSymbol typeParameterSymbol) => GenericParameter = new (typeParameterSymbol);
1919

20-
public partial bool HasDefaultConstructorConstraint () =>
21-
GenericParameter.TypeParameterSymbol.HasConstructorConstraint |
22-
GenericParameter.TypeParameterSymbol.HasValueTypeConstraint |
23-
GenericParameter.TypeParameterSymbol.HasUnmanagedTypeConstraint;
24-
2520
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes => GenericParameter.TypeParameterSymbol.GetDynamicallyAccessedMemberTypes ();
2621

2722
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.Collections.Immutable;
45
using ILLink.RoslynAnalyzer;
56
using Microsoft.CodeAnalysis;
67

@@ -29,6 +30,19 @@ internal partial bool HasParameterOfType (int parameterIndex, string fullTypeNam
2930

3031
internal partial bool HasGenericParametersCount (int genericParameterCount) => Method.TypeParameters.Length == genericParameterCount;
3132

33+
internal partial ImmutableArray<GenericParameterProxy> GetGenericParameters ()
34+
{
35+
if (Method.TypeParameters.IsEmpty)
36+
return ImmutableArray<GenericParameterProxy>.Empty;
37+
38+
var builder = ImmutableArray.CreateBuilder<GenericParameterProxy> (Method.TypeParameters.Length);
39+
foreach (var typeParameter in Method.TypeParameters) {
40+
builder.Add (new GenericParameterProxy (typeParameter));
41+
}
42+
43+
return builder.ToImmutableArray ();
44+
}
45+
3246
internal partial bool IsStatic () => Method.IsStatic;
3347

3448
internal partial bool ReturnsVoid () => Method.ReturnType.SpecialType == SpecialType.System_Void;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212

1313
namespace ILLink.RoslynAnalyzer.TrimAnalysis
1414
{
15-
public readonly record struct TrimAnalysisAssignmentPattern (
16-
MultiValue Source,
17-
MultiValue Target,
18-
IOperation Operation)
15+
public readonly record struct TrimAnalysisAssignmentPattern
1916
{
17+
public MultiValue Source { init; get; }
18+
public MultiValue Target { init; get; }
19+
public IOperation Operation { init; get; }
20+
21+
public TrimAnalysisAssignmentPattern (MultiValue source, MultiValue target, IOperation operation)
22+
{
23+
Source = source.Clone ();
24+
Target = target.Clone ();
25+
Operation = operation;
26+
}
27+
2028
public TrimAnalysisAssignmentPattern Merge (ValueSetLattice<SingleValue> lattice, TrimAnalysisAssignmentPattern other)
2129
{
2230
Debug.Assert (Operation == other.Operation);

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,36 @@
1313

1414
namespace ILLink.RoslynAnalyzer.TrimAnalysis
1515
{
16-
public readonly record struct TrimAnalysisMethodCallPattern (
17-
IMethodSymbol CalledMethod,
18-
MultiValue Instance,
19-
ImmutableArray<MultiValue> Arguments,
20-
IOperation Operation,
21-
ISymbol OwningSymbol)
16+
public readonly record struct TrimAnalysisMethodCallPattern
2217
{
18+
public IMethodSymbol CalledMethod { init; get; }
19+
public MultiValue Instance { init; get; }
20+
public ImmutableArray<MultiValue> Arguments { init; get; }
21+
public IOperation Operation { init; get; }
22+
public ISymbol OwningSymbol { init; get; }
23+
24+
public TrimAnalysisMethodCallPattern (
25+
IMethodSymbol calledMethod,
26+
MultiValue instance,
27+
ImmutableArray<MultiValue> arguments,
28+
IOperation operation,
29+
ISymbol owningSymbol)
30+
{
31+
CalledMethod = calledMethod;
32+
Instance = instance.Clone ();
33+
if (arguments.IsEmpty) {
34+
Arguments = ImmutableArray<MultiValue>.Empty;
35+
} else {
36+
var builder = ImmutableArray.CreateBuilder<MultiValue> ();
37+
foreach (var argument in arguments) {
38+
builder.Add (argument.Clone ());
39+
}
40+
Arguments = builder.ToImmutableArray ();
41+
}
42+
Operation = operation;
43+
OwningSymbol = owningSymbol;
44+
}
45+
2346
public TrimAnalysisMethodCallPattern Merge (ValueSetLattice<SingleValue> lattice, TrimAnalysisMethodCallPattern other)
2447
{
2548
Debug.Assert (Operation == other.Operation);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public override void HandleAssignment (MultiValue source, MultiValue target, IOp
191191
// TODO: consider not tracking patterns unless the target is something
192192
// annotated with DAMT.
193193
TrimAnalysisPatterns.Add (
194+
// This will copy the values if necessary
194195
new TrimAnalysisAssignmentPattern (source, target, operation),
195196
isReturnValue: false
196197
);
@@ -264,6 +265,7 @@ public override MultiValue HandleMethodCall (IMethodSymbol calledMethod, MultiVa
264265
}
265266
}
266267

268+
// This will copy the values if necessary
267269
TrimAnalysisPatterns.Add (new TrimAnalysisMethodCallPattern (
268270
calledMethod,
269271
instance,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.Collections.Immutable;
45
using ILLink.RoslynAnalyzer;
56
using Microsoft.CodeAnalysis;
67

@@ -10,6 +11,20 @@ internal readonly partial struct TypeProxy
1011
{
1112
public TypeProxy (ITypeSymbol type) => Type = type;
1213

14+
internal partial ImmutableArray<GenericParameterProxy> GetGenericParameters ()
15+
{
16+
if (Type is not INamedTypeSymbol namedType ||
17+
namedType.TypeParameters.IsEmpty)
18+
return ImmutableArray<GenericParameterProxy>.Empty;
19+
20+
var builder = ImmutableArray.CreateBuilder<GenericParameterProxy> (namedType.TypeParameters.Length);
21+
foreach (var typeParameter in namedType.TypeParameters) {
22+
builder.Add (new GenericParameterProxy (typeParameter));
23+
}
24+
25+
return builder.ToImmutableArray ();
26+
}
27+
1328
public readonly ITypeSymbol Type;
1429

1530
public string Name { get => Type.MetadataName; }

src/tools/illink/src/ILLink.Shared/TrimAnalysis/GenericParameterValue.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ namespace ILLink.Shared.TrimAnalysis
1212
sealed partial record GenericParameterValue : ValueWithDynamicallyAccessedMembers
1313
{
1414
public readonly GenericParameterProxy GenericParameter;
15-
16-
public partial bool HasDefaultConstructorConstraint ();
1715
}
1816
}

0 commit comments

Comments
 (0)