Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning for MakeGenericType annotation mismatch #104921

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
Expand Down Expand Up @@ -958,6 +958,11 @@ internal partial MethodReturnValue GetMethodReturnValue(MethodProxy method, bool
internal partial MethodReturnValue GetMethodReturnValue(MethodProxy method, bool isNewObj)
=> GetMethodReturnValue(method, isNewObj, GetReturnParameterAnnotation(method.Method));

#pragma warning disable CA1822 // Other partial implementations are not in the ilc project
internal partial GenericParameterValue GetGenericParameterValue(GenericParameterProxy genericParameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
#pragma warning restore CA1822 // Mark members as static
=> new GenericParameterValue(genericParameter.GenericParameter, dynamicallyAccessedMemberTypes);

internal partial GenericParameterValue GetGenericParameterValue(GenericParameterProxy genericParameter)
=> new GenericParameterValue(genericParameter.GenericParameter, GetGenericParameterAnnotation(genericParameter.GenericParameter));

Expand All @@ -970,18 +975,14 @@ internal partial MethodParameterValue GetMethodParameterValue(ParameterProxy par
=> GetMethodParameterValue(param, GetParameterAnnotation(param));

#pragma warning disable CA1822 // Mark members as static - Should be an instance method for consistency
// overrideIsThis is needed for backwards compatibility with MakeGenericType/Method https://github.com/dotnet/linker/issues/2428
internal MethodParameterValue GetMethodThisParameterValue(MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
internal partial MethodParameterValue GetMethodThisParameterValue(MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
if (!method.HasImplicitThis() && !overrideIsThis)
if (!method.HasImplicitThis())
throw new InvalidOperationException($"Cannot get 'this' parameter of method {method.GetDisplayName()} with no 'this' parameter.");
return new MethodParameterValue(new ParameterProxy(method, (ParameterIndex)0), dynamicallyAccessedMemberTypes, overrideIsThis);
return new MethodParameterValue(new ParameterProxy(method, (ParameterIndex)0), dynamicallyAccessedMemberTypes);
}
#pragma warning restore CA1822 // Mark members as static

internal partial MethodParameterValue GetMethodThisParameterValue(MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
=> GetMethodThisParameterValue(method, dynamicallyAccessedMemberTypes, false);

internal partial MethodParameterValue GetMethodThisParameterValue(MethodProxy method)
{
if (!method.HasImplicitThis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ namespace ILLink.Shared.TrimAnalysis
/// </summary>
internal partial record MethodParameterValue
{
public MethodParameterValue(ParameterProxy param, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
public MethodParameterValue(ParameterProxy param, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
StaticType = param.ParameterType;
Parameter = param;
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
_overrideIsThis = overrideIsThis;
}

public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static class UnboxT<T>
}
return NonNullableField;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2090:MakeGenericMethod",
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:MakeGenericMethod",
Justification = "'NullableField<TElem> where TElem : struct' implies 'TElem : new()'. Nullable does not make use of new() so it is safe." +
"The warning is only issued when IsDynamicCodeSupported is true.")]
static Func<object, T?> CreateWhenDynamicCodeSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static JsonConverter<T> Create<T>(EnumConverterOptions converterOptions,
}

[RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2071:UnrecognizedReflectionPattern",
Justification = "'EnumConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because enumType's constructors are not annotated. " +
"But EnumConverter doesn't call new T(), so this is safe.")]
public static JsonConverter Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy? namingPolicy, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static JsonConverter CreateValueConverter(Type valueTypeToConvert, JsonCo
culture: null)!;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2071:UnrecognizedReflectionPattern",
Justification = "'NullableConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because valueTypeToConvert's constructors are not annotated. " +
"But NullableConverter doesn't call new T(), so this is safe.")]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ internal partial MethodReturnValue GetMethodReturnValue (MethodProxy method, boo
internal partial MethodReturnValue GetMethodReturnValue (MethodProxy method, bool isNewObj)
=> GetMethodReturnValue (method, isNewObj, GetMethodReturnValueAnnotation (method.Method));

internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
=> new GenericParameterValue (genericParameter.TypeParameterSymbol, dynamicallyAccessedMemberTypes);

internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter)
=> new GenericParameterValue (genericParameter.TypeParameterSymbol);

Expand All @@ -119,14 +122,6 @@ internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy m
return GetMethodParameterValue (new ParameterProxy (method, (ParameterIndex) 0), dynamicallyAccessedMemberTypes);
}

// overrideIsThis is needed for backwards compatibility with MakeGenericType/Method https://github.com/dotnet/linker/issues/2428
internal MethodParameterValue GetMethodThisParameterValue (MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
{
if (!method.HasImplicitThis () && !overrideIsThis)
throw new InvalidOperationException ($"Cannot get 'this' parameter of method {method.GetDisplayName ()} with no 'this' parameter.");
return new MethodParameterValue (new ParameterProxy (method, (ParameterIndex) 0), dynamicallyAccessedMemberTypes, overrideIsThis);
}

internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method)
{
if (!method.HasImplicitThis ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ namespace ILLink.Shared.TrimAnalysis
/// </summary>
internal partial record GenericParameterValue
{
public GenericParameterValue (ITypeParameterSymbol typeParameterSymbol) => GenericParameter = new (typeParameterSymbol);
public GenericParameterValue (ITypeParameterSymbol typeParameterSymbol, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
GenericParameter = new (typeParameterSymbol);
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
}

public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes => GenericParameter.TypeParameterSymbol.GetDynamicallyAccessedMemberTypes ();
public GenericParameterValue (ITypeParameterSymbol typeParameterSymbol)
: this (typeParameterSymbol, typeParameterSymbol.GetDynamicallyAccessedMemberTypes ())
{
}

public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }

public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
=> new string[] { GenericParameter.TypeParameterSymbol.Name, GenericParameter.TypeParameterSymbol.ContainingSymbol.GetDisplayName () };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public MethodParameterValue (IMethodSymbol methodSymbol, ParameterIndex paramete
public MethodParameterValue (ParameterProxy parameter)
: this (parameter, FlowAnnotations.GetMethodParameterAnnotation (parameter)) { }

public MethodParameterValue (ParameterProxy parameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
public MethodParameterValue (ParameterProxy parameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
Parameter = parameter;
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
StaticType = parameter.ParameterType;
_overrideIsThis = overrideIsThis;
}

public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ partial class FlowAnnotations

internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter);

internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes);

internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes);

internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers
} else {
// Any other type - perform generic parameter validation
var genericParameterValues = GetGenericParameterValues (typeValue.RepresentedType.GetGenericParameters ());
if (!AnalyzeGenericInstantiationTypeArray (argumentValues[0], calledMethod, genericParameterValues)) {
if (!AnalyzeGenericInstantiationTypeArray (argumentValues[0], genericParameterValues)) {
_diagnosticContext.AddDiagnostic (DiagnosticId.MakeGenericType, calledMethod.GetDisplayName ());
}
}
Expand Down Expand Up @@ -1242,7 +1242,7 @@ private IEnumerable<MultiValue> ProcessGetMethodByName (TypeProxy type, string m
yield return NullValue.Instance;
}

private bool AnalyzeGenericInstantiationTypeArray (in MultiValue arrayParam, in MethodProxy calledMethod, ImmutableArray<GenericParameterValue> genericParameters)
private bool AnalyzeGenericInstantiationTypeArray (in MultiValue arrayParam, ImmutableArray<GenericParameterValue> genericParameters)
{
bool hasRequirements = false;
foreach (var genericParameter in genericParameters) {
Expand Down Expand Up @@ -1280,10 +1280,7 @@ private bool AnalyzeGenericInstantiationTypeArray (in MultiValue arrayParam, in

for (int i = 0; i < size.Value; i++) {
if (array.TryGetValueByIndex (i, out MultiValue value)) {
// https://github.com/dotnet/linker/issues/2428
// We need to report the target as "this" - as that was the previous behavior
// but with the annotation from the generic parameter.
var targetValue = _annotations.GetMethodThisParameterValue (calledMethod, GetGenericParameterEffectiveMemberTypes (genericParameters[i]), overrideIsThis: true);
var targetValue = _annotations.GetGenericParameterValue (genericParameters[i].GenericParameter, GetGenericParameterEffectiveMemberTypes (genericParameters[i]));
_requireDynamicallyAccessedMembersAction.Invoke (value, targetValue);
}
}
Expand Down Expand Up @@ -1315,7 +1312,7 @@ private void ValidateGenericMethodInstantiation (
}

var genericParameterValues = GetGenericParameterValues (genericMethod.GetGenericParameters ());
if (!AnalyzeGenericInstantiationTypeArray (genericParametersArray, reflectionMethod, genericParameterValues)) {
if (!AnalyzeGenericInstantiationTypeArray (genericParametersArray, genericParameterValues)) {
_diagnosticContext.AddDiagnostic (DiagnosticId.MakeGenericMethod, reflectionMethod.GetDisplayName ());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespace ILLink.Shared.TrimAnalysis
{
internal sealed partial record MethodParameterValue : ValueWithDynamicallyAccessedMembers, IValueWithStaticType
{
// _overrideIsThis is needed for backwards compatibility with MakeGenericType/Method https://github.com/dotnet/linker/issues/2428
private readonly bool _overrideIsThis;

public TypeProxy? StaticType { get; }

public ParameterProxy Parameter { get; }
Expand All @@ -26,7 +23,7 @@ public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch
public override string ToString ()
=> this.ValueToString (Parameter.Method.Method, Parameter.Index, DynamicallyAccessedMemberTypes);

public bool IsThisParameter () => _overrideIsThis || Parameter.IsImplicitThis;
public bool IsThisParameter () => Parameter.IsImplicitThis;

public override SingleValue DeepCopy () => this; // This value is immutable

Expand Down
15 changes: 8 additions & 7 deletions src/tools/illink/src/linker/Linker.Dataflow/FlowAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ internal partial MethodReturnValue GetMethodReturnValue (MethodProxy method, boo
internal partial MethodReturnValue GetMethodReturnValue (MethodProxy method, bool isNewObj)
=> GetMethodReturnValue (method, isNewObj, GetReturnParameterAnnotation (method.Method));

#pragma warning disable CA1822 // Mark members as static - Should be an instance method for consistency
internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
=> new GenericParameterValue (genericParameter.GenericParameter, dynamicallyAccessedMemberTypes);
#pragma warning restore CA1822 // Mark members as static

internal partial GenericParameterValue GetGenericParameterValue (GenericParameterProxy genericParameter)
=> new GenericParameterValue (genericParameter.GenericParameter, GetGenericParameterAnnotation (genericParameter.GenericParameter));

Expand All @@ -703,18 +708,14 @@ internal partial MethodParameterValue GetMethodParameterValue (ParameterProxy pa
=> GetMethodParameterValue (param, GetParameterAnnotation (param));

#pragma warning disable CA1822 // Mark members as static - Should be an instance method for consistency
// overrideIsThis is needed for backwards compatibility with MakeGenericType/Method https://github.com/dotnet/linker/issues/2428
internal MethodParameterValue GetMethodThisParameterValue (MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
if (!method.HasImplicitThis () && !overrideIsThis)
if (!method.HasImplicitThis ())
throw new InvalidOperationException ($"Cannot get 'this' parameter of method {method.GetDisplayName ()} with no 'this' parameter.");
return new MethodParameterValue (method.Method.DeclaringType, new ParameterProxy (method, (ParameterIndex) 0), dynamicallyAccessedMemberTypes, overrideIsThis);
return new MethodParameterValue (method.Method.DeclaringType, new ParameterProxy (method, (ParameterIndex) 0), dynamicallyAccessedMemberTypes);
}
#pragma warning restore CA1822 // Mark members as static

internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
=> GetMethodThisParameterValue (method, dynamicallyAccessedMemberTypes, false);

internal partial MethodParameterValue GetMethodThisParameterValue (MethodProxy method)
{
if (!method.HasImplicitThis ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ namespace ILLink.Shared.TrimAnalysis
/// </summary>
internal partial record MethodParameterValue
{
public MethodParameterValue (TypeDefinition? staticType, ParameterProxy param, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes, bool overrideIsThis = false)
public MethodParameterValue (TypeDefinition? staticType, ParameterProxy param, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
{
StaticType = staticType == null ? null : new (staticType);
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
Parameter = param;
_overrideIsThis = overrideIsThis;
}

public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ static void DynamicallyAccessedMembers ()
typeof (AnnotatedGenerics).RequiresPublicMethods ();
}

// This should produce IL2071 https://github.com/dotnet/linker/issues/2144
[ExpectedWarning ("IL2070", "MakeGenericMethod")]
[ExpectedWarning ("IL2071", "'T'")]
static void InstantiateGeneric (Type type = null)
{
// This should warn due to MakeGenericMethod - in this case the generic parameter is unannotated type
Expand Down
Loading
Loading