Skip to content

Add tests for assigning dataflow values to generic parameters through MakeGenericType #106832

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,13 +1,17 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
[UnconditionalSuppressMessage ("AOT", "IL3050", Justification = "These tests are not targetted at AOT scenarios")]
[SandboxDependency ("Dependencies/TestSystemTypeBase.cs")]
public class MakeGenericDataFlow
{
public static void Main ()
Expand Down Expand Up @@ -41,6 +45,9 @@ public static void Test ()

TestWithMultipleArgumentsWithRequirements ();

MakeGenericWithThis.Test();
MakeGenericWithFields.Test();
MakeGenericWithObjectGetType.Test();
NewConstraint.Test ();
StructConstraint.Test ();
UnmanagedConstraint.Test ();
Expand Down Expand Up @@ -174,6 +181,189 @@ class GenericWithMultipleArgumentsWithRequirements<
{
}

class MakeGenericWithThis
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
static TypeDerived fieldAnnotated;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
static TypeDerived fieldMisannotated;
static TypeDerived fieldUnannotated;

[ExpectedWarning("IL2065", "MakeGenericFromThis_Annotated()")]
[ExpectedWarning("IL2065", "MakeGenericFromThis_Misannotated()")]
public static void Test()
{
new TypeDerived().MakeGenericFromThis_Annotated();
new TypeDerived().MakeGenericFromThis_Unannotated();
new TypeDerived().MakeGenericFromThis_Misannotated();
}

class TypeDerived : TestSystemTypeBase
{
[ExpectedWarning("IL2086", "GenericWithPublicFieldsArgument", "'this'")]
public void MakeGenericFromThis_Unannotated ()
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (this);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
public void MakeGenericFromThis_Annotated ()
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (this);
}

[ExpectedWarning("IL2086", "GenericWithPublicFieldsArgument", "'this'")]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public void MakeGenericFromThis_Misannotated ()
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (this);
}
}
}

class MakeGenericWithFields
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
static Type AnnotatedField;
static Type UnannotatedField;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
static Type MisannotatedField;

public static void Test()
{
TestAnnotated();
TestUnannotated();
TestMisannotated();
}

public static void TestAnnotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(AnnotatedField);
}

[ExpectedWarning("IL2081", "GenericWithPublicFieldsArgument", nameof(UnannotatedField))]
public static void TestUnannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(UnannotatedField);
}

[ExpectedWarning("IL2081", "GenericWithPublicFieldsArgument", nameof(MisannotatedField))]
public static void TestMisannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(MisannotatedField);
}
}

class MakeGenericWithObjectGetType
{
static AnnotatedType s_annotatedType = new();
static UnannotatedType s_unannotatedType = new();
static MisannotatedType s_misannotatedType = new();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
class AnnotatedType
{
public void MakeGenericFromThis()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(this.GetType());
}
}

class UnannotatedType
{
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
public void MakeGenericFromThis()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(this.GetType());
}
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
class MisannotatedType
{
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
public void MakeGenericWithThis()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(this.GetType());
}
}


public static void Test()
{
MakeGenericFromNewObject_Annotated();
MakeGenericFromNewObject_Unannotated();
MakeGenericFromNewObject_Misannotated();
MakeGenericFromParameters_Annotated(new AnnotatedType());
MakeGenericFromParameters_Unannotated(new UnannotatedType());
MakeGenericFromParameters_Misannotated(new MisannotatedType());
MakeGenericFromField_Annotated();
MakeGenericFromField_Unannotated();
MakeGenericFromField_Misannotated();
MakeGenericFromThis_Annotated();
MakeGenericFromThis_Unannotated();
MakeGenericFromThis_Misannotated();
}

static void MakeGenericFromThis_Annotated ()
{
new AnnotatedType().MakeGenericFromThis();
}
static void MakeGenericFromThis_Unannotated ()
{
new UnannotatedType().MakeGenericFromThis();
}
static void MakeGenericFromThis_Misannotated ()
{
new MisannotatedType().MakeGenericWithThis();
}

static void MakeGenericFromNewObject_Annotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(new AnnotatedType().GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromNewObject_Unannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(new UnannotatedType().GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromNewObject_Misannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(new MisannotatedType().GetType());
}

static void MakeGenericFromParameters_Annotated(AnnotatedType annotatedType)
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(annotatedType.GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromParameters_Unannotated(UnannotatedType unannotatedType)
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(unannotatedType.GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromParameters_Misannotated(MisannotatedType misannotatedType)
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(misannotatedType.GetType());
}

static void MakeGenericFromField_Annotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(s_annotatedType.GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromField_Unannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(s_unannotatedType.GetType());
}
[ExpectedWarning("IL2076", "GenericWithPublicFieldsArgument", "System.Object.GetType()")]
static void MakeGenericFromField_Misannotated()
{
typeof(GenericWithPublicFieldsArgument<>).MakeGenericType(s_misannotatedType.GetType());
}
}

class NewConstraint
{
class GenericWithNewConstraint<T> where T : new()
Expand Down
Loading