Skip to content

Commit

Permalink
Add tests for RUC warnings on override methods (#86015)
Browse files Browse the repository at this point in the history
These are tests for bug #86008.

I also restructured the test file into nested classes some more as it's easier to navigate this way.

Also adds a test for #86032.
  • Loading branch information
vitek-karas authored May 12, 2023
1 parent e131899 commit 5fabb48
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void Main ()
{
TestRequiresOnlyThroughReflection ();
AccessedThroughReflectionOnGenericType<TestType>.Test ();
AccessedThroughGenericParameterAnnotation.Test ();
AccessThroughSpecialAttribute.Test ();
AccessThroughPInvoke.Test ();
AccessThroughNewConstraint.Test<TestType> ();
Expand Down Expand Up @@ -73,6 +74,59 @@ public static void Test ()
}
}

class AccessedThroughGenericParameterAnnotation
{
class TypeWithRequiresMethod
{
[RequiresUnreferencedCode("--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--")]
[RequiresDynamicCode ("--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--")]
[RequiresAssemblyFiles ("--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--")]
public static void MethodWhichRequires () { }
}

class TypeWithPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T>
{
public TypeWithPublicMethods () { }
}

[ExpectedWarning ("IL2026", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--")]
[ExpectedWarning ("IL3002", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
static void TestAccessOnGenericType ()
{
new TypeWithPublicMethods<TypeWithRequiresMethod> ();
}

static void MethodWithPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> () { }

[ExpectedWarning ("IL2026", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--")]
[ExpectedWarning ("IL3002", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
static void TestAccessOnGenericMethod ()
{
MethodWithPublicMethods<TypeWithRequiresMethod> ();
}

static void MethodWithPublicMethodsInference<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> (T instance) { }

// https://github.com/dotnet/runtime/issues/86032
// IL2026 should be produced by the analyzer as well, but it has a bug around inferred generic arguments
[ExpectedWarning ("IL2026", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.Trimmer | Tool.NativeAot)]
[ExpectedWarning ("IL3002", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--AccessedThroughGenericParameterAnnotation.TypeWithRequiresMethod.MethodWhichRequires--", ProducedBy = Tool.NativeAot)]
static void TestAccessOnGenericMethodWithInferenceOnMethod ()
{
MethodWithPublicMethodsInference (new TypeWithRequiresMethod ());
}

public static void Test ()
{
TestAccessOnGenericType ();
TestAccessOnGenericMethod ();
TestAccessOnGenericMethodWithInferenceOnMethod ();
}
}

class AccessThroughSpecialAttribute
{
// https://github.com/dotnet/linker/issues/1873
Expand Down
Loading

0 comments on commit 5fabb48

Please sign in to comment.