diff options
author | Tlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com> | 2021-07-17 03:00:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-17 03:00:13 +0300 |
commit | 460dd6ddb329a5588d9e4399f4257ce28dfadaca (patch) | |
tree | bddc2f3ff127a4a6ea7c0b2bf3c085e153c24ac3 /test/Mono.Linker.Tests.Cases | |
parent | 2174dc1bf8217ab8f5b77b0fcc0ab0cbf1973a85 (diff) |
Ruc on classes (#2143)
Add support for RequiresUnreferencedCode on classes
Added a method to retrieve the effective RequiresUnreferencedCode attribute, which means it will look for the attribute on the type or any of its declaring types
Added new warning IL2109 for when a type derives from a base type with effective RequiresUnreferencedCode, and the derived doesn't have RequiresUnreferencedCode on its effective type
Added logic to handle suppressions
Adds tests
Diffstat (limited to 'test/Mono.Linker.Tests.Cases')
-rw-r--r-- | test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs index 7d6c358c2..45facb1d6 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs @@ -79,6 +79,7 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability OnEventMethod.Test (); AccessThroughNewConstraint.Test (); AccessThroughLdToken.Test (); + RequiresOnClass.Test (); } [ExpectedWarning ("IL2026", "Message for --RequiresWithMessageOnly--.")] @@ -783,5 +784,192 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability Expression<Func<bool>> getter = () => PropertyWithLdToken; } } + + class RequiresOnClass + { + [RequiresUnreferencedCode ("Message for --ClassWithRequiresUnreferencedCode--")] + class ClassWithRequiresUnreferencedCode + { + public static object Instance; + + public ClassWithRequiresUnreferencedCode () { } + + public static void StaticMethod () { } + + public void NonStaticMethod () { } + + public class NestedClass + { + public static void NestedStaticMethod () { } + // RequiresOnMethod.MethodWithRUC generates a warning that gets suppressed because the declaring type has RUC + public static void CallRUCMethod () => RequiresOnMethod.MethodWithRUC (); + } + + // RequiresUnfereferencedCode on the type will suppress IL2072 + static ClassWithRequiresUnreferencedCode () + { + Instance = Activator.CreateInstance (Type.GetType ("SomeText")); + } + + public static void TestSuppressions (Type[] types) + { + // StaticMethod is a static method on a RUC annotated type, so it should warn. But RequiresUnreferencedCode in the + // class suppresses other RequiresUnreferencedCode messages + StaticMethod (); + + var nested = new NestedClass (); + + // RequiresUnreferencedCode in the class suppresses DynamicallyAccessedMembers messages + types[1].GetMethods (); + + void LocalFunction (int a) { } + LocalFunction (2); + } + } + + class RequiresOnMethod + { + [RequiresUnreferencedCode ("MethodWithRUC")] + public static void MethodWithRUC () { } + } + + [ExpectedWarning ("IL2109", "RequiresOnClass/DerivedWithoutRequires", "RequiresOnClass.ClassWithRequiresUnreferencedCode", "--ClassWithRequiresUnreferencedCode--")] + private class DerivedWithoutRequires : ClassWithRequiresUnreferencedCode + { + public static void StaticMethodInInheritedClass () { } + + public class DerivedNestedClass + { + public static void NestedStaticMethod () { } + } + + public static void ShouldntWarn (object objectToCast) + { + _ = typeof (ClassWithRequiresUnreferencedCode); + var type = (ClassWithRequiresUnreferencedCode) objectToCast; + } + } + + [ExpectedWarning ("IL2109", "RequiresOnClass/DerivedWithoutRequires2", "RequiresOnClass.ClassWithRequiresUnreferencedCode.NestedClass", "--ClassWithRequiresUnreferencedCode--")] + private class DerivedWithoutRequires2 : ClassWithRequiresUnreferencedCode.NestedClass + { + public static void StaticMethod () { } + } + + [UnconditionalSuppressMessage ("trim", "IL2109")] + class TestUnconditionalSuppressMessage : ClassWithRequiresUnreferencedCode + { + public static void StaticMethodInTestSuppressionClass () { } + } + + class ClassWithoutRequiresUnreferencedCode + { + public ClassWithoutRequiresUnreferencedCode () { } + + public static void StaticMethod () { } + + public void NonStaticMethod () { } + + public class NestedClass + { + public static void NestedStaticMethod () { } + } + } + + [RequiresUnreferencedCode ("Message for --DerivedWithRequires--")] + private class DerivedWithRequires : ClassWithoutRequiresUnreferencedCode + { + public static void StaticMethodInInheritedClass () { } + + public class DerivedNestedClass + { + public static void NestedStaticMethod () { } + } + } + + [RequiresUnreferencedCode ("Message for --DerivedWithRequires2--")] + private class DerivedWithRequires2 : ClassWithRequiresUnreferencedCode + { + public static void StaticMethodInInheritedClass () { } + + // The requires attribute in the declaring type suppresses IL2109 here + public class DerivedNestedClass : ClassWithRequiresUnreferencedCode + { + public static void NestedStaticMethod () { } + } + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.StaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + static void TestRequiresInClassAccessedByStaticMethod () + { + ClassWithRequiresUnreferencedCode.StaticMethod (); + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + static void TestRequiresInClassAccessedByCctor () + { + var classObject = new ClassWithRequiresUnreferencedCode (); + } + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.NestedClass.NestedStaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + static void TestRequiresInParentClassAccesedByStaticMethod () + { + ClassWithRequiresUnreferencedCode.NestedClass.NestedStaticMethod (); + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.StaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.NestedClass.NestedStaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.NestedClass.CallRUCMethod()", "Message for --ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + static void TestRequiresOnBaseButNotOnDerived () + { + DerivedWithoutRequires.StaticMethodInInheritedClass (); + DerivedWithoutRequires.StaticMethod (); + DerivedWithoutRequires.DerivedNestedClass.NestedStaticMethod (); + DerivedWithoutRequires.NestedClass.NestedStaticMethod (); + DerivedWithoutRequires.NestedClass.CallRUCMethod (); + DerivedWithoutRequires.ShouldntWarn (null); + DerivedWithoutRequires.Instance.ToString (); + DerivedWithoutRequires2.StaticMethod (); + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.DerivedWithRequires.StaticMethodInInheritedClass()", "--DerivedWithRequires--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.DerivedWithRequires.DerivedNestedClass.NestedStaticMethod()", "--DerivedWithRequires--", GlobalAnalysisOnly = true)] + static void TestRequiresOnDerivedButNotOnBase () + { + DerivedWithRequires.StaticMethodInInheritedClass (); + DerivedWithRequires.StaticMethod (); + DerivedWithRequires.DerivedNestedClass.NestedStaticMethod (); + DerivedWithRequires.NestedClass.NestedStaticMethod (); + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.DerivedWithRequires2.StaticMethodInInheritedClass()", "--DerivedWithRequires2--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.DerivedWithRequires2.DerivedNestedClass.NestedStaticMethod()", "--DerivedWithRequires2--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.StaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.NestedClass.NestedStaticMethod()", "--ClassWithRequiresUnreferencedCode--", GlobalAnalysisOnly = true)] + static void TestRequiresOnBaseAndDerived () + { + DerivedWithRequires2.StaticMethodInInheritedClass (); + DerivedWithRequires2.StaticMethod (); + DerivedWithRequires2.DerivedNestedClass.NestedStaticMethod (); + DerivedWithRequires2.NestedClass.NestedStaticMethod (); + } + + [ExpectedWarning ("IL2026", "RequiresOnClass.ClassWithRequiresUnreferencedCode.TestSuppressions(Type[])", GlobalAnalysisOnly = true)] + static void TestSuppressionsOnClass () + { + ClassWithRequiresUnreferencedCode.TestSuppressions (new[] { typeof (ClassWithRequiresUnreferencedCode) }); + TestUnconditionalSuppressMessage.StaticMethodInTestSuppressionClass (); + } + + public static void Test () + { + TestRequiresInClassAccessedByStaticMethod (); + TestRequiresInParentClassAccesedByStaticMethod (); + TestRequiresInClassAccessedByCctor (); + TestRequiresOnBaseButNotOnDerived (); + TestRequiresOnDerivedButNotOnBase (); + TestRequiresOnBaseAndDerived (); + TestSuppressionsOnClass (); + } + } } }
\ No newline at end of file |