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

Enhance NestedTypes in DynamicallyAccessedMemberTypes #2133

Merged
merged 1 commit into from
Jul 8, 2021
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
3 changes: 2 additions & 1 deletion src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,8 @@ void MarkTypeForDynamicallyAccessedMembers (ref ReflectionPatternContext reflect
MarkField (ref reflectionContext, field, DependencyKind.DynamicallyAccessedMember);
break;
case TypeDefinition nestedType:
MarkType (ref reflectionContext, nestedType, DependencyKind.DynamicallyAccessedMember);
DependencyInfo nestedDependencyInfo = new DependencyInfo (DependencyKind.DynamicallyAccessedMember, reflectionContext.Source);
reflectionContext.RecordRecognizedPattern (nestedType, () => _markStep.MarkEntireType (nestedType, includeBaseAndInterfaceTypes: true, nestedDependencyInfo));
break;
case PropertyDefinition property:
MarkProperty (ref reflectionContext, property, DependencyKind.DynamicallyAccessedMember);
Expand Down
1 change: 1 addition & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/IReflectDataflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class C2
public C2 () { }

[Kept]
[KeptMember (".ctor()")]
public class Nested { }
}

Expand Down
16 changes: 16 additions & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/MemberTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,19 @@ public class HideBaseNestedType { }
class PublicNestedTypesType : PublicNestedTypesBaseType
{
[Kept]
[KeptMember (".ctor()")]
public class PublicNestedType { }
protected class ProtectedNestedType { }
private class PrivateNestedType { }
[Kept]
[KeptMember (".ctor()")]
public class HideNestedType { }

[Kept]
[KeptBaseType (typeof (MulticastDelegate))]
[KeptMember (".ctor(System.Object,System.IntPtr)")]
[KeptMember ("BeginInvoke(System.AsyncCallback,System.Object)")]
[KeptMember ("EndInvoke(System.IAsyncResult)")]
[KeptMember ("Invoke()")]
public delegate int PublicDelegate ();

Expand Down Expand Up @@ -1085,8 +1089,10 @@ class NonPublicNestedTypesType : NonPublicNestedTypesBaseType
{
public class PublicNestedType { }
[Kept]
[KeptMember (".ctor()")]
protected class ProtectedNestedType { }
[Kept]
[KeptMember (".ctor()")]
private class PrivateNestedType { }
public class HideNestedType { }

Expand All @@ -1095,6 +1101,8 @@ public class HideNestedType { }
[Kept]
[KeptBaseType (typeof (MulticastDelegate))]
[KeptMember (".ctor(System.Object,System.IntPtr)")]
[KeptMember ("BeginInvoke(System.AsyncCallback,System.Object)")]
[KeptMember ("EndInvoke(System.IAsyncResult)")]
[KeptMember ("Invoke()")]
private delegate int PrivateDelegate ();
}
Expand Down Expand Up @@ -1125,23 +1133,31 @@ public class HideBaseNestedType { }
class AllNestedTypesType : AllNestedTypesBaseType
{
[Kept]
[KeptMember (".ctor()")]
public class PublicNestedType { }
[Kept]
[KeptMember (".ctor()")]
protected class ProtectedNestedType { }
[Kept]
[KeptMember (".ctor()")]
private class PrivateNestedType { }
[Kept]
[KeptMember (".ctor()")]
public class HideNestedType { }

[Kept]
[KeptBaseType (typeof (MulticastDelegate))]
[KeptMember (".ctor(System.Object,System.IntPtr)")]
[KeptMember ("BeginInvoke(System.AsyncCallback,System.Object)")]
[KeptMember ("EndInvoke(System.IAsyncResult)")]
[KeptMember ("Invoke()")]
public delegate int PublicDelegate ();

[Kept]
[KeptBaseType (typeof (MulticastDelegate))]
[KeptMember (".ctor(System.Object,System.IntPtr)")]
[KeptMember ("BeginInvoke(System.AsyncCallback,System.Object)")]
[KeptMember ("EndInvoke(System.IAsyncResult)")]
[KeptMember ("Invoke()")]
private delegate int PrivateDelegate ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ private int PrivateProperty {
[Kept]
public static class PublicNestedType
{
[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down Expand Up @@ -193,7 +195,9 @@ private int PrivateProperty {
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down Expand Up @@ -258,7 +262,9 @@ private int PrivateProperty {
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down Expand Up @@ -313,7 +319,9 @@ private int PrivateProperty {
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should delete these comments in our testing given that now the link will mark more than the declaration

[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down Expand Up @@ -368,7 +376,9 @@ private int PrivateProperty {
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down Expand Up @@ -423,7 +433,9 @@ private int PrivateProperty {
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
[Kept]
public static int _nestedPublicField;
[Kept]
public static void NestedPublicMethod ()
{ }
}
Expand Down
6 changes: 6 additions & 0 deletions test/Mono.Linker.Tests.Cases/Reflection/ObjectGetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,25 +1102,31 @@ interface IUnusedInterface
class Derived : AnnotatedBase
{
[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (AnnotatedBase))]
public class NestedDerived : AnnotatedBase
{
[Kept]
[KeptBaseType (typeof (NestedDerived))]
[KeptMember (".ctor()")]
public class DeepNestedDerived : NestedDerived
{
[Kept] // Marked due to the annotation
[KeptMember (".ctor()")]
public class DeepNestedChild
{
}

[Kept] // Marked due to the annotation
[KeptMember (".ctor()")]
private class DeepNestedPrivateChild
{
}
}

[Kept] // Marked due to the annotation
[KeptInterface (typeof (IUnannotatedInterface))]
[KeptMember (".ctor()")]
public class NestedChild : IUnannotatedInterface
{
}
Expand Down