From d8b94bd244f9239ef394817e2bd9a37ec1bae838 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Tue, 10 Aug 2021 10:10:20 -0700 Subject: Fix warnings for DAM.All (#2191) * Fix warnings for DAM.All Fixes https://github.com/mono/linker/issues/2159 * Add tests * PR feedback - Remove unnecessary helper * Avoid redundant DAM warnings for base members * PR feedback - Resolve -> TryResolve - Avoid nested yield returns - Testcases with base instantiated over self for generic parameter with requirements --- .../AnnotatedMembersAccessedViaReflection.cs | 97 ++++++++++++++++++++++ .../DataFlow/GenericParameterDataFlow.cs | 20 +++++ 2 files changed, 117 insertions(+) (limited to 'test/Mono.Linker.Tests.Cases/DataFlow') diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/AnnotatedMembersAccessedViaReflection.cs b/test/Mono.Linker.Tests.Cases/DataFlow/AnnotatedMembersAccessedViaReflection.cs index f94b9a458..4b626431f 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/AnnotatedMembersAccessedViaReflection.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/AnnotatedMembersAccessedViaReflection.cs @@ -84,6 +84,38 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeof (AnnotatedField).RequiresPublicFields (); } + class NestedType + { + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + public static Type _annotatedField; + } + + [ExpectedWarning ("IL2110", nameof (_annotatedField))] + [ExpectedWarning ("IL2026", "test")] + static void DynamicallyAccessedMembersAll1 () + { + typeof (AnnotatedField).RequiresAll (); + } + + [ExpectedWarning ("IL2110", nameof (_annotatedField))] + [ExpectedWarning ("IL2026", "test")] + static void DynamicallyAccessedMembersAll2 () + { + typeof (AnnotatedField).RequiresAll (); + } + + [ExpectedWarning ("IL2110", nameof (NestedType), nameof (NestedType._annotatedField))] + static void DynamicallyAccessedMembersNestedTypes1 () + { + typeof (AnnotatedField).RequiresNonPublicNestedTypes (); + } + + [ExpectedWarning ("IL2110", nameof (NestedType), nameof (NestedType._annotatedField))] + static void DynamicallyAccessedMembersNestedTypes2 () + { + typeof (AnnotatedField).RequiresNonPublicNestedTypes (); + } + [UnconditionalSuppressMessage ("test", "IL2026")] public static void Test () { @@ -95,6 +127,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow DynamicDependencyByName (); DynamicallyAccessedMembers (); DynamicallyAccessedMembersSuppressedByRUC (); + DynamicallyAccessedMembersAll1 (); + DynamicallyAccessedMembersAll2 (); + DynamicallyAccessedMembersNestedTypes1 (); + DynamicallyAccessedMembersNestedTypes2 (); } } @@ -177,6 +213,22 @@ namespace Mono.Linker.Tests.Cases.DataFlow var _ = new Action (instance.AnnotatedMethod); } + [ExpectedWarning ("IL2111", nameof (MethodWithSingleAnnotatedParameter))] + [ExpectedWarning ("IL2026", "test")] + [ExpectedWarning ("IL2111", nameof (IWithAnnotatedMethod.AnnotatedMethod))] + static void DynamicallyAccessedMembersAll1 () + { + typeof (AnnotatedMethodParameters).RequiresAll (); + } + + [ExpectedWarning ("IL2111", nameof (MethodWithSingleAnnotatedParameter))] + [ExpectedWarning ("IL2026", "test")] + [ExpectedWarning ("IL2111", nameof (IWithAnnotatedMethod.AnnotatedMethod))] + static void DynamicallyAccessedMembersAll2 () + { + typeof (AnnotatedMethodParameters).RequiresAll (); + } + [UnconditionalSuppressMessage ("test", "IL2026")] public static void Test () { @@ -189,6 +241,8 @@ namespace Mono.Linker.Tests.Cases.DataFlow DynamicallyAccessedMembersSuppressedByRUC (); Ldftn (); Ldvirtftn (); + DynamicallyAccessedMembersAll1 (); + DynamicallyAccessedMembersAll2 (); } } @@ -395,6 +449,24 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeof (AnnotatedProperty).RequiresPublicProperties (); } + [ExpectedWarning ("IL2111", nameof (PropertyWithAnnotation) + ".set")] + [ExpectedWarning ("IL2026", "test")] + [ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationGetterOnly) + ".get")] + [UnconditionalSuppressMessage ("Test", "IL2110", Justification = "Suppress warning about backing field of PropertyWithAnnotation")] + static void DynamicallyAccessedMembersAll1 () + { + typeof (AnnotatedProperty).RequiresAll (); + } + + [ExpectedWarning ("IL2111", nameof (PropertyWithAnnotation) + ".set")] + [ExpectedWarning ("IL2026", "test")] + [ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationGetterOnly) + ".get")] + [UnconditionalSuppressMessage ("Test", "IL2110", Justification = "Suppress warning about backing field of PropertyWithAnnotation")] + static void DynamicallyAccessedMembersAll2 () + { + typeof (AnnotatedProperty).RequiresAll (); + } + [UnconditionalSuppressMessage ("test", "IL2026")] public static void Test () { @@ -410,6 +482,8 @@ namespace Mono.Linker.Tests.Cases.DataFlow DynamicDependencySuppressedByRUC (); DynamicallyAccessedMembers (); DynamicallyAccessedMembersSuppressedByRUC (); + DynamicallyAccessedMembersAll1 (); + DynamicallyAccessedMembersAll2 (); } } @@ -446,12 +520,19 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeof (AnnotatedGenerics).GetMethod (nameof (GenericWithAnnotation)).MakeGenericMethod (type); } + // Like above, no warning expected + static void DynamicallyAccessedMembersAll () + { + typeof (AnnotatedGenerics).RequiresAll (); + } + public static void Test () { ReflectionOnly (); DynamicDependency (); DynamicallyAccessedMembers (); InstantiateGeneric (); + DynamicallyAccessedMembersAll (); } } @@ -499,6 +580,20 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeof (AnnotationOnGenerics).RequiresPublicMethods (); } + [ExpectedWarning ("IL2111", nameof (GenericMethodWithAnnotation))] + [ExpectedWarning ("IL2111", "GenericWithAnnotatedMethod", "AnnotatedMethod")] + static void DynamicallyAccessedMembersAll1 () + { + typeof (AnnotationOnGenerics).RequiresAll (); + } + + [ExpectedWarning ("IL2111", nameof (GenericMethodWithAnnotation))] + [ExpectedWarning ("IL2111", "GenericWithAnnotatedMethod", "AnnotatedMethod")] + static void DynamicallyAccessedMembersAll2 () + { + typeof (AnnotationOnGenerics).RequiresAll (); + } + public static void Test () { GenericTypeWithStaticMethodViaLdftn (); @@ -506,6 +601,8 @@ namespace Mono.Linker.Tests.Cases.DataFlow GenericMethodWithAnnotationDirectCall (); GenericMethodWithAnnotationViaLdftn (); GenericMethodDynamicallyAccessedMembers (); + DynamicallyAccessedMembersAll1 (); + DynamicallyAccessedMembersAll2 (); } } diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs index 09de0a015..1665c1abd 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs @@ -243,6 +243,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow static void TestBaseTypeGenericRequirements () { new DerivedTypeWithInstantiatedGenericOnBase (); + new DerivedTypeWithInstantiationOverSelfOnBase (); new DerivedTypeWithOpenGenericOnBase (); new DerivedTypeWithOpenGenericOnBaseWithRequirements (); } @@ -261,6 +262,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow { } + class GenericBaseTypeWithRequiresAll<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] T> + { + } + + [RecognizedReflectionAccessPattern] + class DerivedTypeWithInstantiationOverSelfOnBase : GenericBaseTypeWithRequirements + { + } + [UnrecognizedReflectionAccessPattern (typeof (GenericBaseTypeWithRequirements<>), "T", messageCode: "IL2091")] class DerivedTypeWithOpenGenericOnBase : GenericBaseTypeWithRequirements { @@ -277,6 +287,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow static void TestInterfaceTypeGenericRequirements () { IGenericInterfaceTypeWithRequirements instance = new InterfaceImplementationTypeWithInstantiatedGenericOnBase (); + new InterfaceImplementationTypeWithInstantiationOverSelfOnBase (); new InterfaceImplementationTypeWithOpenGenericOnBase (); new InterfaceImplementationTypeWithOpenGenericOnBaseWithRequirements (); } @@ -290,6 +301,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow { } + interface IGenericInterfaceTypeWithRequiresAll<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] T> + { + } + + [RecognizedReflectionAccessPattern] + class InterfaceImplementationTypeWithInstantiationOverSelfOnBase : IGenericInterfaceTypeWithRequiresAll + { + } + [UnrecognizedReflectionAccessPattern (typeof (IGenericInterfaceTypeWithRequirements<>), "T", messageCode: "IL2091")] class InterfaceImplementationTypeWithOpenGenericOnBase : IGenericInterfaceTypeWithRequirements { -- cgit v1.2.3