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 and adjust more of CoreLib for trimming #38865

Merged
merged 3 commits into from
Jul 18, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;

namespace System.Reflection
{
#if CORERT
Expand Down Expand Up @@ -109,6 +111,8 @@ internal static bool MatchesExactly(this SignatureType pattern, Type actual)
return signatureType.TryResolve(genericMethod.GetGenericArguments());
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Used to find matching method overloads. Only used for assignability checks.")]
private static Type? TryResolve(this SignatureType signatureType, Type[] genericMethodParameters)
{
if (signatureType.IsSZArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ protected TypeInfo() { }
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public virtual IEnumerable<MethodInfo> GetDeclaredMethods(string name)
{
foreach (MethodInfo method in GetMethods(TypeInfo.DeclaredOnlyLookup))
foreach (MethodInfo method in GetDeclaredOnlyMethods(this))
{
if (method.Name == name)
yield return method;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "The yield return state machine doesn't propagate annotations")]
static MethodInfo[] GetDeclaredOnlyMethods(
Type type) => type.GetMethods(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<ConstructorInfo> DeclaredConstructors
Expand Down Expand Up @@ -76,10 +81,15 @@ public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
get
{
foreach (Type t in GetNestedTypes(TypeInfo.DeclaredOnlyLookup))
foreach (Type t in GetDeclaredOnlyNestedTypes(this))
{
yield return t.GetTypeInfo();
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "The yield return state machine doesn't propagate annotations")]
static Type[] GetDeclaredOnlyNestedTypes(
Type type) => type.GetNestedTypes(TypeInfo.DeclaredOnlyLookup);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ public virtual void ReleaseAllResources()
}
}

public static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir, Type? usingResourceSet)
public static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type? usingResourceSet)
{
return new ResourceManager(baseName, resourceDir, usingResourceSet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;

Expand Down Expand Up @@ -115,10 +116,11 @@ public virtual Type GetDefaultReader()
// Returns the preferred IResourceWriter class for this kind of ResourceSet.
// Subclasses of ResourceSet using their own Readers &; should override
// GetDefaultReader and GetDefaultWriter.
// TODO: https://github.com/mono/linker/issues/943
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, "System.Resources.ResourceWriter", "System.Resources.Writer")]
public virtual Type GetDefaultWriter()
{
Assembly resourceWriterAssembly = Assembly.Load("System.Resources.Writer, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
return resourceWriterAssembly.GetType("System.Resources.ResourceWriter", throwOnError: true)!;
return Type.GetType("System.Resources.ResourceWriter, System.Resources.Writer", throwOnError: true)!;
Copy link
Member

Choose a reason for hiding this comment

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

@jkotas split this into the 2 calls in https://github.com/dotnet/coreclr/pull/11314/files#diff-37ee6a4a589ffae33ce341f9c1a28133R136. Are we sure this is safe to collapse it back into a single Type.GetType call?

Copy link
Member

Choose a reason for hiding this comment

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

The PR was reconciling differences between multiple forked copies of the same code. I do not think there was a strong reason why I have changed this to Assembly.Load + GetType that was used in the other fork.

It should be ok to change it back to Type.GetType.

}

public virtual IDictionaryEnumerator GetEnumerator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ internal static bool IsDefined(ICustomAttributeProvider obj, Type attributeType,
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool IsDefinedInternal(ICustomAttributeProvider obj, Type AttributeType);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker analyzes base properties and marks them")]
private static PropertyInfo? GetBasePropertyDefinition(RuntimePropertyInfo property)
{
MethodInfo? method = property.GetGetMethod(true);
Expand Down Expand Up @@ -620,6 +622,8 @@ internal static bool IsDefined(ICustomAttributeProvider obj, Type attributeType,

}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker analyzes base events and marks them")]
private static EventInfo? GetBaseEventDefinition(RuntimeEventInfo evt)
{
MethodInfo? method = evt.GetAddMethod(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ private bool has_ctor_method()

// We require emitted types to have all members on their bases to be accessible.
// This is basically an identity function for `this`.
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Reflection emitted types have all of their members")]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public
TypeInfo? CreateTypeInfo()
Expand Down Expand Up @@ -1819,6 +1821,8 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam
return generic_params;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")]
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
{
/*FIXME I would expect the same checks of GetMethod here*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ internal bool IsCreated
return InflateType(type, type_arguments, method_args);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Reflection emitted types have all of their members")]
internal static Type? InflateType(Type? type, Type[]? type_args, Type[]? method_args)
{
if (type == null)
Expand Down