Skip to content

Commit

Permalink
Port annotations from runtimelab (#62562)
Browse files Browse the repository at this point in the history
Placing the attribute into ApiComplatExlude for now - there will be follow up work to do any necessary cleanups/propagation to non-shared parts of CoreLib before this can go into ref assemblies.
  • Loading branch information
MichalStrehovsky authored Dec 9, 2021
1 parent b579961 commit c9cd367
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions eng/ApiCompatExcludeAttributes.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
T:System.CLSCompliantAttribute
T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute
T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute
1 change: 1 addition & 0 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ ref MemoryMarshal.GetArrayDataReference(larray),
Debug.Assert(array != null);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public static Array CreateInstance(Type elementType, params long[] lengths)
{
if (lengths == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
===========================================================*/

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Collections
{
Expand Down Expand Up @@ -716,6 +717,7 @@ public static ArrayList Synchronized(ArrayList list)
// downcasting all elements. This copy may fail and is an O(n) operation.
// Internally, this implementation calls Array.Copy.
//
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public virtual Array ToArray(Type type)
{
if (type == null)
Expand Down Expand Up @@ -1099,6 +1101,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return array;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Array ToArray(Type type)
{
if (type == null)
Expand Down Expand Up @@ -1492,6 +1495,7 @@ public override void Sort(int index, int count, IComparer? comparer)
}
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Array ToArray(Type type)
{
lock (_root)
Expand Down Expand Up @@ -1874,6 +1878,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return _list.ToArray();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Array ToArray(Type type)
{
return _list.ToArray(type);
Expand Down Expand Up @@ -2125,6 +2130,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return _list.ToArray();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Array ToArray(Type type)
{
return _list.ToArray(type);
Expand Down Expand Up @@ -2578,6 +2584,7 @@ public override object? this[int index]
return array;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Array ToArray(Type type)
{
if (type == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Reflection;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using CultureInfo = System.Globalization.CultureInfo;

Expand All @@ -28,6 +29,8 @@ partial class DefaultBinder : Binder
//
// The most specific match will be selected.
//
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "AOT compiler ensures params arrays are created for reflection-invokable methods")]
public sealed override MethodBase BindToMethod(
BindingFlags bindingAttr, MethodBase[] match, ref object?[] args,
ParameterModifier[]? modifiers, CultureInfo? cultureInfo, string[]? names, out object? state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
#endif
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -201,6 +202,10 @@ private static Func<PropertyValue, PropertyValue> GetBoxedValueTypePropertyGette
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "Instantiation over a reference type. See comments above.")]
#endif
private static Func<PropertyValue, PropertyValue> GetReferenceTypePropertyGetter(PropertyInfo property)
{
var helper = (TypeHelper)Activator.CreateInstance(typeof(ReferenceTypeHelper<>).MakeGenericType(property.DeclaringType!))!;
Expand All @@ -216,6 +221,10 @@ abstract class TypeHelper
{
public abstract Func<PropertyValue, PropertyValue> GetPropertyGetter(PropertyInfo property);

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "Instantiation over a reference type. See comments above.")]
#endif
protected static Delegate GetGetMethod(PropertyInfo property, Type propertyType)
{
return property.GetMethod!.CreateDelegate(typeof(Func<,>).MakeGenericType(property.DeclaringType!, propertyType));
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Private.CoreLib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
=> (TEnum[])GetValues(typeof(TEnum));
#endif

[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload instead.")]
public static Array GetValues(Type enumType)
{
if (enumType is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected MethodInfo() { }
public override Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
public virtual MethodInfo GetGenericMethodDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ internal abstract class SignatureType : Type
public sealed override MemberTypes MemberType => MemberTypes.TypeInfo;

// Compositors
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public sealed override Type MakeArrayType() => new SignatureArrayType(this, rank: 1, isMultiDim: false);
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public sealed override Type MakeArrayType(int rank)
{
if (rank <= 0)
Expand All @@ -47,6 +49,7 @@ public sealed override Type MakeArrayType(int rank)
public sealed override Type MakeByRefType() => new SignatureByRefType(this);
public sealed override Type MakePointerType() => new SignaturePointerType(this);

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public sealed override Type MakeGenericType(params Type[] typeArguments) => throw new NotSupportedException(SR.NotSupported_SignatureType); // There is no SignatureType for type definition types so it would never be legal to call this.

Expand Down Expand Up @@ -96,6 +99,7 @@ public sealed override Type MakeArrayType(int rank)
public sealed override string GetEnumName(object value) => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override string[] GetEnumNames() => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override Type GetEnumUnderlyingType() => throw new NotSupportedException(SR.NotSupported_SignatureType);
[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use Enum.GetValues<TEnum> instead.")]
public sealed override Array GetEnumValues() => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override Guid GUID => throw new NotSupportedException(SR.NotSupported_SignatureType);
protected sealed override TypeCode GetTypeCodeImpl() => throw new NotSupportedException(SR.NotSupported_SignatureType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ internal static bool MatchesExactly(this SignatureType pattern, Type actual)

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Used to find matching method overloads. Only used for assignability checks.")]
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
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 Expand Up @@ -164,6 +166,8 @@ internal static bool MatchesExactly(this SignatureType pattern, Type actual)
}
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "Used to find matching method overloads. Only used for assignability checks.")]
private static Type? TryMakeArrayType(this Type type)
{
try
Expand All @@ -176,6 +180,8 @@ internal static bool MatchesExactly(this SignatureType pattern, Type actual)
}
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "Used to find matching method overloads. Only used for assignability checks.")]
private static Type? TryMakeArrayType(this Type type, int rank)
{
try
Expand Down Expand Up @@ -213,6 +219,7 @@ internal static bool MatchesExactly(this SignatureType pattern, Type actual)
}

[RequiresUnreferencedCode("Wrapper around MakeGenericType which itself has RequiresUnreferencedCode")]
[RequiresDynamicCode("Wrapper around MakeGenericType which itself has RequiresDynamicCode")]
private static Type? TryMakeGenericType(this Type type, Type[] instantiation)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static unsafe string PtrToStringUTF8(IntPtr ptr, int byteLen)
return string.CreateStringFromEncoding((byte*)ptr, byteLen, Encoding.UTF8);
}

[RequiresDynamicCode("Marshalling code for the object might not be available. Use the SizeOf<T> overload instead.")]
public static int SizeOf(object structure)
{
if (structure is null)
Expand All @@ -124,6 +125,7 @@ public static int SizeOf<T>(T structure)
return SizeOfHelper(structure.GetType(), throwIfNotMarshalable: true);
}

[RequiresDynamicCode("Marshalling code for the object might not be available. Use the SizeOf<T> overload instead.")]
public static int SizeOf(Type t)
{
if (t is null)
Expand Down Expand Up @@ -378,6 +380,7 @@ public static unsafe int ReadInt32(IntPtr ptr, int ofs)

public static int ReadInt32(IntPtr ptr) => ReadInt32(ptr, 0);

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static IntPtr ReadIntPtr(object ptr, int ofs)
{
#if TARGET_64BIT
Expand Down Expand Up @@ -464,6 +467,7 @@ public static unsafe void WriteInt16(IntPtr ptr, int ofs, short val)

public static void WriteInt16(IntPtr ptr, int ofs, char val) => WriteInt16(ptr, ofs, (short)val);

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt16([In, Out]object ptr, int ofs, char val) => WriteInt16(ptr, ofs, (short)val);

public static void WriteInt16(IntPtr ptr, char val) => WriteInt16(ptr, 0, (short)val);
Expand Down Expand Up @@ -501,6 +505,7 @@ public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val)
#endif
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteIntPtr(object ptr, int ofs, IntPtr val)
{
#if TARGET_64BIT
Expand Down Expand Up @@ -563,6 +568,8 @@ public static void PrelinkAll(Type c)
}
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "AOT compilers can see the T.")]
public static void StructureToPtr<T>([DisallowNull] T structure, IntPtr ptr, bool fDeleteOld)
{
StructureToPtr((object)structure!, ptr, fDeleteOld);
Expand All @@ -572,6 +579,7 @@ public static void StructureToPtr<T>([DisallowNull] T structure, IntPtr ptr, boo
/// Creates a new instance of "structuretype" and marshals data from a
/// native memory block to it.
/// </summary>
[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static object? PtrToStructure(IntPtr ptr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type structureType)
Expand Down Expand Up @@ -602,6 +610,7 @@ public static void StructureToPtr<T>([DisallowNull] T structure, IntPtr ptr, boo
/// <summary>
/// Marshals data from a native memory block to a preallocated structure class.
/// </summary>
[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void PtrToStructure(IntPtr ptr, object structure)
{
PtrToStructureHelper(ptr, structure, allowValueClasses: false);
Expand Down Expand Up @@ -633,6 +642,8 @@ public static void PtrToStructure<T>(IntPtr ptr, [DisallowNull] T structure)
return (T)structure;
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "AOT compilers can see the T.")]
public static void DestroyStructure<T>(IntPtr ptr) => DestroyStructure(ptr, typeof(T));

// CoreCLR has a different implementation for Windows only
Expand Down Expand Up @@ -1151,6 +1162,7 @@ public static Guid GenerateGuidForType(Type type)
return type.FullName;
}

[RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetDelegateForFunctionPointer<TDelegate> overload instead.")]
public static Delegate GetDelegateForFunctionPointer(IntPtr ptr, Type t)
{
if (ptr == IntPtr.Zero)
Expand Down Expand Up @@ -1205,6 +1217,7 @@ public static TDelegate GetDelegateForFunctionPointer<TDelegate>(IntPtr ptr)
return (TDelegate)(object)GetDelegateForFunctionPointerInternal(ptr, t);
}

[RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate<TDelegate> overload instead.")]
public static IntPtr GetFunctionPointerForDelegate(Delegate d)
{
if (d is null)
Expand All @@ -1215,6 +1228,8 @@ public static IntPtr GetFunctionPointerForDelegate(Delegate d)
return GetFunctionPointerForDelegateInternal(d);
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:AotUnfriendlyApi",
Justification = "AOT compilers can see the T.")]
public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where TDelegate : notnull
{
return GetFunctionPointerForDelegate((Delegate)(object)d);
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ public virtual Type GetEnumUnderlyingType()

return fields[0].FieldType;
}

[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use Enum.GetValues<TEnum> instead.")]
public virtual Array GetEnumValues()
{
if (!IsEnum)
Expand All @@ -532,10 +534,13 @@ public virtual Array GetEnumValues()
throw NotImplemented.ByDesign;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public virtual Type MakeArrayType() => throw new NotSupportedException();
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public virtual Type MakeArrayType(int rank) => throw new NotSupportedException();
public virtual Type MakeByRefType() => throw new NotSupportedException();

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public virtual Type MakeGenericType(params Type[] typeArguments) => throw new NotSupportedException(SR.NotSupported_SubclassOverride);

Expand Down

0 comments on commit c9cd367

Please sign in to comment.