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

Fix Activator.CreateInstance case #2146

Merged
merged 2 commits into from
Jul 16, 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
8 changes: 4 additions & 4 deletions src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,10 +1596,10 @@ public override bool HandleCall (MethodBody callingMethodBody, MethodReference c
var requiredMemberTypes = GetDynamicallyAccessedMemberTypesFromBindingFlagsForConstructors (bindingFlags);

// Special case the public parameterless constructor if we know that there are 0 args passed in
if (ctorParameterCount == 0 &&
requiredMemberTypes.HasFlag (DynamicallyAccessedMemberTypes.PublicConstructors) &&
!requiredMemberTypes.HasFlag (DynamicallyAccessedMemberTypes.NonPublicConstructors))
requiredMemberTypes = DynamicallyAccessedMemberTypes.PublicParameterlessConstructor;
if (ctorParameterCount == 0 && requiredMemberTypes.HasFlag (DynamicallyAccessedMemberTypes.PublicConstructors)) {
requiredMemberTypes &= ~DynamicallyAccessedMemberTypes.PublicConstructors;
requiredMemberTypes |= DynamicallyAccessedMemberTypes.PublicParameterlessConstructor;
}

RequireDynamicallyAccessedMembers (ref reflectionContext, requiredMemberTypes, value, calledMethod.Parameters[0]);
}
Expand Down
10 changes: 10 additions & 0 deletions test/Mono.Linker.Tests.Cases/Reflection/ActivatorCreateInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void Main ()
TestNullArgsOnKnownType ();
TestNullArgsOnAnnotatedType (typeof (TestType));
TestNullArgsNonPublicOnly (typeof (TestType));
TestNullArgsNonPublicWithNonPublicAnnotation (typeof (TestType));

CreateInstanceWithGetTypeFromHierarchy.Test ();
}
Expand Down Expand Up @@ -532,6 +533,15 @@ private static void TestNullArgsNonPublicOnly (
Activator.CreateInstance (type, BindingFlags.NonPublic | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture);
}

[Kept]
[ExpectedNoWarnings]
private static void TestNullArgsNonPublicWithNonPublicAnnotation (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors),
KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] Type type)
{
Activator.CreateInstance (type, nonPublic: true);
Copy link
Member

Choose a reason for hiding this comment

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

How does this test work? I assume we're trying to assert that the given type has its non-public constructors preserved, but how are we confirming that we're also not marking the other public constructors?

Wasn't the problem in the PR that we were marking extra things and getting additional warnings?

Copy link
Member

Choose a reason for hiding this comment

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

Not really - the problem is that PublicParameterlessConstructor | NonPublicConstructors was an annotation which was not considered sufficient for calling CreateInstance(type, true). Which it should be.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wasn't the problem in the PR that we were marking extra things and getting additional warnings?

I may have given you the wrong impression when we chatted - this additional warning was because we tried to require (not mark) extra things on the input (which was an abstract DAMT value). But the extra requirements also would have resulted in extra things being marked for concrete input types (that just wasn't the failure mode we happened to hit).

}

[Kept]
class CreateInstanceWithGetTypeFromHierarchy
{
Expand Down