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

Annotate CreateInstanceForAnotherGenericParameter as PublicParameterlessConstructor #50390

Merged
merged 2 commits into from
Mar 30, 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
15 changes: 13 additions & 2 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,13 @@ internal static bool HasElementType(RuntimeType type)
return outHandles;
}

internal static object CreateInstanceForAnotherGenericParameter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors)] RuntimeType type, RuntimeType genericParameter)
internal static object CreateInstanceForAnotherGenericParameter(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] RuntimeType type,
RuntimeType genericParameter)
{
Debug.Assert(type.GetConstructor(Type.EmptyTypes) is ConstructorInfo c && c.IsPublic,
$"CreateInstanceForAnotherGenericParameter requires {nameof(type)} to have a public parameterless constructor so it can be annotated for trimming without preserving private constructors.");

object? instantiatedObject = null;

IntPtr typeHandle = genericParameter.GetTypeHandleInternal().Value;
Expand All @@ -224,8 +229,14 @@ internal static object CreateInstanceForAnotherGenericParameter([DynamicallyAcce
return instantiatedObject!;
}

internal static object CreateInstanceForAnotherGenericParameter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors)] RuntimeType type, RuntimeType genericParameter1, RuntimeType genericParameter2)
internal static object CreateInstanceForAnotherGenericParameter(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] RuntimeType type,
RuntimeType genericParameter1,
RuntimeType genericParameter2)
{
Debug.Assert(type.GetConstructor(Type.EmptyTypes) is ConstructorInfo c && c.IsPublic,
$"CreateInstanceForAnotherGenericParameter requires {nameof(type)} to have a public parameterless constructor so it can be annotated for trimming without preserving private constructors.");

object? instantiatedObject = null;

IntPtr* pTypeHandles = stackalloc IntPtr[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public override int GetHashCode() =>
[Serializable]
internal sealed partial class EnumComparer<T> : Comparer<T>, ISerializable where T : struct, Enum
{
internal EnumComparer() { }
public EnumComparer() { }

// Used by the serialization engine.
private EnumComparer(SerializationInfo info, StreamingContext context) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public override int GetHashCode() =>
// Needs to be public to support binary serialization compatibility
public sealed partial class EnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct, Enum
{
internal EnumEqualityComparer() { }
public EnumEqualityComparer() { }
jkotas marked this conversation as resolved.
Show resolved Hide resolved

// This is used by the serialization engine.
private EnumEqualityComparer(SerializationInfo information, StreamingContext context) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1835,11 +1835,15 @@ public override Type[] GetGenericParameterConstraints()
return constraints ?? Type.EmptyTypes;
}

internal static object CreateInstanceForAnotherGenericParameter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type genericType, RuntimeType genericArgument)
internal static object CreateInstanceForAnotherGenericParameter(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type genericType,
RuntimeType genericArgument)
{
var gt = (RuntimeType)MakeGenericType(genericType, new Type[] { genericArgument });
RuntimeConstructorInfo? ctor = gt.GetDefaultConstructor();
if (ctor is null)

// CreateInstanceForAnotherGenericParameter requires type to have a public parameterless constructor so it can be annotated for trimming without preserving private constructors.
if (ctor is null || !ctor.IsPublic)
throw new MissingMethodException(SR.Format(SR.Arg_NoDefCTor, gt));

return ctor.InternalInvoke(null, null, wrapExceptions: true)!;
Expand Down