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 more of CoreLib for trimming #38265

Merged
merged 5 commits into from
Jun 24, 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 @@ -100,6 +100,8 @@ public static partial class Activator

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Implementation detail of Activator that linker intrinsically recognizes")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Implementation detail of Activator that linker intrinsically recognizes")]
private static ObjectHandle? CreateInstanceInternal(string assemblyString,
string typeName,
bool ignoreCase,
Expand Down Expand Up @@ -143,7 +145,7 @@ public static partial class Activator
}

[System.Runtime.CompilerServices.Intrinsic]
public static T CreateInstance<T>()
public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>()
{
return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true)!;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Lazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ internal static LazyHelper Create(LazyThreadSafetyMode mode, bool useDefaultCons
}
}

internal static T CreateViaDefaultConstructor<T>()
internal static T CreateViaDefaultConstructor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>()
{
try
{
Expand Down Expand Up @@ -184,7 +184,7 @@ internal static LazyThreadSafetyMode GetModeFromIsThreadSafe(bool isThreadSafe)
/// </remarks>
[DebuggerTypeProxy(typeof(LazyDebugView<>))]
[DebuggerDisplay("ThreadSafetyMode={Mode}, IsValueCreated={IsValueCreated}, IsValueFaulted={IsValueFaulted}, Value={ValueForDebugDisplay}")]
public class Lazy<T>
public class Lazy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>
{
private static T CreateViaDefaultConstructor() => LazyHelper.CreateViaDefaultConstructor<T>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// 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;
using System.Threading;

namespace System
{
public class Lazy<T, TMetadata> : Lazy<T>
public class Lazy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T, TMetadata> : Lazy<T>
{
private readonly TMetadata _metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected Assembly() { }

public virtual IEnumerable<TypeInfo> DefinedTypes
{
[RequiresUnreferencedCode("Types might be removed")]
get
{
Type[] types = GetTypes();
Expand All @@ -41,6 +42,7 @@ public virtual IEnumerable<TypeInfo> DefinedTypes
}
}

[RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetTypes()
{
Module[] m = GetModules(false);
Expand Down Expand Up @@ -70,8 +72,14 @@ public virtual Type[] GetTypes()
return ret;
}

public virtual IEnumerable<Type> ExportedTypes => GetExportedTypes();
public virtual IEnumerable<Type> ExportedTypes
{
[RequiresUnreferencedCode("Types might be removed")]
get => GetExportedTypes();
}
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetExportedTypes() { throw NotImplemented.ByDesign; }
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetForwardedTypes() { throw NotImplemented.ByDesign; }

public virtual string? CodeBase => throw NotImplemented.ByDesign;
Expand All @@ -93,8 +101,11 @@ public virtual Type[] GetTypes()
public virtual AssemblyName GetName() => GetName(copiedName: false);
public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; }

[RequiresUnreferencedCode("Types might be removed")]
public virtual Type? GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false);
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type? GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false);
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }

public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
Expand Down Expand Up @@ -135,6 +146,7 @@ public virtual Type[] GetTypes()
public Module[] GetLoadedModules() => GetLoadedModules(getResourceModules: false);
public virtual Module[] GetLoadedModules(bool getResourceModules) { throw NotImplemented.ByDesign; }

[RequiresUnreferencedCode("Assembly references might be removed")]
public virtual AssemblyName[] GetReferencedAssemblies() { throw NotImplemented.ByDesign; }

public virtual Assembly GetSatelliteAssembly(CultureInfo culture) { throw NotImplemented.ByDesign; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Reflection;
using System.Threading;
Expand Down Expand Up @@ -92,6 +93,8 @@ internal static bool TrackAsyncMethodCompletion
/// <summary>Gets a description of the state of the state machine object, suitable for debug purposes.</summary>
/// <param name="stateMachine">The state machine object.</param>
/// <returns>A description of the state machine.</returns>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Copy link
Member

Choose a reason for hiding this comment

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

Can we close #32921 with this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup

Justification = "It's okay if unused fields disappear from debug views")]
internal static string GetAsyncStateMachineDescription(IAsyncStateMachine stateMachine)
{
Debug.Assert(stateMachine != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace System.Runtime.CompilerServices
{
public sealed class ConditionalWeakTable<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
public sealed class ConditionalWeakTable<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : class
where TValue : class?
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public static void PtrToStructure<T>(IntPtr ptr, [DisallowNull] T structure)
}

[return: MaybeNull]
public static T PtrToStructure<T>(IntPtr ptr) => (T)PtrToStructure(ptr, typeof(T))!;
public static T PtrToStructure<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>(IntPtr ptr) => (T)PtrToStructure(ptr, typeof(T))!;

public static void DestroyStructure<T>(IntPtr ptr) => DestroyStructure(ptr, typeof(T));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ private static void OnAssemblyLoad(RuntimeAssembly assembly)
}
#endif // !CORERT

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Satellite assemblies have no code in them and loading is not a problem")]
private Assembly? ResolveSatelliteAssembly(AssemblyName assemblyName)
{
// Called by native runtime when CultureName is not empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public override IList<CustomAttributeData> GetCustomAttributesData()

// GetDefaultMembers
// This will return a MemberInfo that has been marked with the [DefaultMemberAttribute]
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicEvents
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public override MemberInfo[] GetDefaultMembers()
{
// See if we have cached the default member name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class LazyInitializer
/// if an object was not used and to then dispose of the object appropriately.
/// </para>
/// </remarks>
public static T EnsureInitialized<T>([NotNull] ref T? target) where T : class =>
public static T EnsureInitialized<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([NotNull] ref T? target) where T : class =>
Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target);

/// <summary>
Expand All @@ -57,7 +57,7 @@ public static T EnsureInitialized<T>([NotNull] ref T? target) where T : class =>
/// <typeparam name="T">The reference type of the reference to be initialized.</typeparam>
/// <param name="target">The variable that need to be initialized</param>
/// <returns>The initialized variable</returns>
private static T EnsureInitializedCore<T>([NotNull] ref T? target) where T : class
private static T EnsureInitializedCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([NotNull] ref T? target) where T : class
{
try
{
Expand Down Expand Up @@ -136,7 +136,7 @@ private static T EnsureInitializedCore<T>([NotNull] ref T? target, Func<T> value
/// <paramref name="target"/>. If <paramref name="syncLock"/> is null, and if the target hasn't already
/// been initialized, a new object will be instantiated.</param>
/// <returns>The initialized value of type <typeparamref name="T"/>.</returns>
public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock)
public static T EnsureInitialized<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock)
{
// Fast path.
if (Volatile.Read(ref initialized))
Expand All @@ -158,7 +158,7 @@ public static T EnsureInitialized<T>([AllowNull] ref T target, ref bool initiali
/// a new object will be instantiated.
/// </param>
/// <returns>The initialized object.</returns>
private static T EnsureInitializedCore<T>([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock)
private static T EnsureInitializedCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock)
{
// Lazily initialize the lock if necessary and then double check if initialization is still required.
lock (EnsureLockInitialized(ref syncLock))
Expand Down
31 changes: 23 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetConstructors(BindingFlags.Public) but this is what the body is doing")]
public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Expand All @@ -164,7 +164,7 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetEvents(BindingFlags.Public) but this is what the body is doing")]
public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
Expand All @@ -178,13 +178,21 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetFields(BindingFlags.Public) but this is what the body is doing")]
public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicEvents
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recognize GetMember(BindingFlags.Public) but this is what the body is doing")]
public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
Expand Down Expand Up @@ -268,7 +276,7 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetMethods(BindingFlags.Public) but this is what the body is doing")]
public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
Expand All @@ -282,7 +290,7 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")]
public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
Expand All @@ -301,7 +309,7 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")]
public PropertyInfo? GetProperty(string name, Type? returnType)
{
if (name == null)
Expand Down Expand Up @@ -333,12 +341,19 @@ public ConstructorInfo? TypeInitializer

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is what the body is doing")]
Justification = "Linker doesn't recognize GetProperties(BindingFlags.Public) but this is what the body is doing")]
public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicEvents
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public virtual MemberInfo[] GetDefaultMembers() => throw NotImplemented.ByDesign;

public virtual RuntimeTypeHandle TypeHandle => throw new NotSupportedException();
Expand Down
Loading