From 89ba543b7a2277e87a2571eee4e31b6d8fb4a46f Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 30 Jul 2021 08:50:30 -0700 Subject: [PATCH 1/8] Adding warnings to make generic and static ctor --- .../Emit/GenericTypeParameterBuilder.cs | 1 + .../src/System/Reflection/Emit/MethodBuilder.cs | 1 + .../Emit/MethodBuilderInstantiation.cs | 2 ++ .../src/System/Reflection/Emit/TypeBuilder.cs | 1 + .../Reflection/Emit/TypeBuilderInstantiation.cs | 2 ++ .../Emit/XXXOnTypeBuilderInstantiation.cs | 3 +++ .../src/System/Reflection/RuntimeMethodInfo.cs | 1 + .../CompilerServices/RuntimeHelpers.CoreCLR.cs | 1 + .../src/System/RuntimeType.CoreCLR.cs | 1 + .../src/System/Reflection/MethodInfo.cs | 3 +++ .../src/System/Reflection/SignatureType.cs | 2 ++ .../System.Private.CoreLib/src/System/Type.cs | 3 +++ .../Context/Delegation/DelegatingMethodInfo.cs | 2 ++ .../Context/Delegation/DelegatingType.cs | 1 + .../Context/Projection/ProjectingMethodInfo.cs | 1 + .../Context/Projection/ProjectingType.cs | 1 + .../Context/Virtual/VirtualMethodBase.cs | 2 ++ .../ref/System.Reflection.DispatchProxy.cs | 1 + .../src/System/Reflection/DispatchProxy.cs | 1 + .../System/Reflection/DispatchProxyGenerator.cs | 17 +++++++++++++---- .../ref/System.Reflection.Emit.cs | 3 +++ .../Reflection/TypeLoading/General/Sentinels.cs | 2 ++ .../Methods/RoConstructedGenericMethod.cs | 1 + .../TypeLoading/Methods/RoDefinitionMethod.cs | 1 + .../Reflection/TypeLoading/Methods/RoMethod.cs | 2 ++ .../TypeLoading/Methods/RoSyntheticMethod.cs | 1 + .../Types/RoConstructedGenericType.cs | 2 ++ .../TypeLoading/Types/RoDefinitionType.cs | 2 ++ .../TypeLoading/Types/RoGenericParameterType.cs | 2 ++ .../TypeLoading/Types/RoHasElementType.cs | 2 ++ .../Reflection/TypeLoading/Types/RoStubType.cs | 2 ++ .../Reflection/TypeLoading/Types/RoType.cs | 2 ++ .../System.Runtime/ref/System.Runtime.cs | 3 +++ .../Emit/GenericTypeParameterBuilder.cs | 1 + .../Reflection/Emit/MethodBuilder.Mono.cs | 1 + .../Reflection/Emit/MethodOnTypeBuilderInst.cs | 1 + .../System/Reflection/Emit/TypeBuilder.Mono.cs | 1 + .../src/System/Reflection/RuntimeMethodInfo.cs | 1 + .../src/System/RuntimeType.Mono.cs | 1 + 39 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs index f2d97999a4e3b..c96319b52cc5f 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs @@ -195,6 +195,7 @@ public override Type MakeArrayType(int rank) public override Type GetGenericTypeDefinition() { throw new InvalidOperationException(); } + [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 override Type MakeGenericType(params Type[] typeArguments) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } protected override bool IsValueTypeImpl() { return false; } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs index 6811eb598bf8c..8d13cb19fb9c7 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs @@ -541,6 +541,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override Type[] GetGenericArguments() => m_inst ?? Type.EmptyTypes; + [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 override MethodInfo MakeGenericMethod(params Type[] typeArguments) { return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs index a5c935ad9fb12..655f018c24f13 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Reflection.Emit @@ -77,6 +78,7 @@ public override bool ContainsGenericParameters } } + [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 override MethodInfo MakeGenericMethod(params Type[] arguments) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index ec93ac3e4dd0d..beef255824113 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -1195,6 +1195,7 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam return m_inst; } + [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 override Type MakeGenericType(params Type[] typeArguments) { AssemblyBuilder.CheckContext(typeArguments); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 81d5207d44553..3f2cadc852e6a 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -251,6 +251,8 @@ public override bool ContainsGenericParameters } public override MethodBase? DeclaringMethod => null; public override Type GetGenericTypeDefinition() { return m_type; } + + [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 override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs index a500e1ddf25a3..1841a8cfe0ff1 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.Emit { @@ -60,6 +61,8 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind public override MethodInfo GetGenericMethodDefinition() { return m_method; } public override bool IsGenericMethodDefinition => m_method.IsGenericMethodDefinition; public override bool ContainsGenericParameters => m_method.ContainsGenericParameters; + + [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 override MethodInfo MakeGenericMethod(params Type[] typeArgs) { if (!IsGenericMethodDefinition) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs index 6e3f0b0122a8f..03e08941b0ece 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs @@ -562,6 +562,7 @@ private Delegate CreateDelegateInternal(Type delegateType, object? firstArgument #endregion #region Generics + [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 override MethodInfo MakeGenericMethod(params Type[] methodInstantiation) { if (methodInstantiation == null) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index 558e2553f9957..6356d694ea20c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -45,6 +45,7 @@ public static partial class RuntimeHelpers [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] private static extern void RunClassConstructor(QCallTypeHandle type); + [RequiresUnreferencedCode("Trimmer can't guarantee existence of class constructor")] public static void RunClassConstructor(RuntimeTypeHandle type) { RuntimeType rt = type.GetRuntimeType(); diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs index b07971e0f5600..d41dd543ca583 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs @@ -3359,6 +3359,7 @@ public override Type[] GetGenericArguments() return types ?? Type.EmptyTypes; } + [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 override Type MakeGenericType(Type[] instantiation) { if (instantiation == null) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs index 532899524e497..8a0cbc11f5163 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace System.Reflection @@ -16,6 +17,8 @@ protected MethodInfo() { } public override Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } public virtual MethodInfo GetGenericMethodDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + + [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); } public abstract MethodInfo GetBaseDefinition(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureType.cs index 1d1f250b1b8ea..e592c24bbb584 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureType.cs @@ -46,6 +46,8 @@ public sealed override Type MakeArrayType(int rank) } public sealed override Type MakeByRefType() => new SignatureByRefType(this); public sealed override Type MakePointerType() => new SignaturePointerType(this); + + [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. // Dissectors diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index 3c1a1783a6e76..f722fe4fb1920 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -535,7 +535,10 @@ public virtual Array GetEnumValues() public virtual Type MakeArrayType() => throw new NotSupportedException(); public virtual Type MakeArrayType(int rank) => throw new NotSupportedException(); public virtual Type MakeByRefType() => throw new NotSupportedException(); + + [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); + public virtual Type MakePointerType() => throw new NotSupportedException(); public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) => new SignatureConstructedGenericType(genericTypeDefinition, typeArguments); diff --git a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingMethodInfo.cs b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingMethodInfo.cs index 1f4fd838c357c..e41233dd4e170 100644 --- a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingMethodInfo.cs +++ b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingMethodInfo.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Globalization; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.Context.Delegation { @@ -159,6 +160,7 @@ public override bool IsDefined(Type attributeType, bool inherit) return UnderlyingMethod.IsDefined(attributeType, inherit); } + [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 override MethodInfo MakeGenericMethod(params Type[] typeArguments) { return UnderlyingMethod.MakeGenericMethod(typeArguments); diff --git a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingType.cs b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingType.cs index 5b23ec0371483..d77592743ef5c 100644 --- a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingType.cs +++ b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Delegation/DelegatingType.cs @@ -455,6 +455,7 @@ public override Type MakePointerType() return _typeInfo.MakePointerType(); } + [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 override Type MakeGenericType(params Type[] typeArguments) { return _typeInfo.MakeGenericType(typeArguments); diff --git a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingMethodInfo.cs b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingMethodInfo.cs index 3d214a5eb5cd0..1c61fbef0f9ad 100644 --- a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingMethodInfo.cs +++ b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingMethodInfo.cs @@ -104,6 +104,7 @@ public override ParameterInfo[] GetParameters() return Projector.Project(base.GetParameters(), Projector.ProjectParameter); } + [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 override MethodInfo MakeGenericMethod(params Type[] typeArguments) { return Projector.ProjectMethod(base.MakeGenericMethod(Projector.Unproject(typeArguments))); diff --git a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingType.cs b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingType.cs index 019893177d448..a23c02cef220d 100644 --- a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingType.cs +++ b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Projection/ProjectingType.cs @@ -307,6 +307,7 @@ public override Type MakePointerType() return _projector.ProjectType(base.MakePointerType()); } + [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 override Type MakeGenericType(params Type[] typeArguments) { typeArguments = _projector.Unproject(typeArguments); diff --git a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Virtual/VirtualMethodBase.cs b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Virtual/VirtualMethodBase.cs index c61e099ff6b76..c8eba53c3435c 100644 --- a/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Virtual/VirtualMethodBase.cs +++ b/src/libraries/System.Reflection.Context/src/System/Reflection/Context/Virtual/VirtualMethodBase.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace System.Reflection.Context.Virtual @@ -87,6 +88,7 @@ public override ParameterInfo[] GetParameters() return CollectionServices.Empty(); } + [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 override sealed MethodInfo MakeGenericMethod(params Type[] typeArguments) { throw new InvalidOperationException(SR.Format(SR.InvalidOperation_NotGenericMethodDefinition, this)); diff --git a/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs b/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs index 82e762d95f67f..084f3142c36cf 100644 --- a/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs +++ b/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs @@ -9,6 +9,7 @@ namespace System.Reflection public abstract partial class DispatchProxy { protected DispatchProxy() { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 static T Create<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TProxy>() where TProxy : System.Reflection.DispatchProxy { throw null; } protected abstract object? Invoke(System.Reflection.MethodInfo? targetMethod, object?[]? args); } diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs index dbfea73eebfc2..520caaf08aff7 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs @@ -33,6 +33,7 @@ protected DispatchProxy() /// An object instance that implements . /// is a class, /// or is sealed or does not have a parameterless constructor + [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 static T Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TProxy>() where TProxy : DispatchProxy { diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index 1a2a90a2bf698..03531e2f9f35a 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -57,13 +57,20 @@ internal static class DispatchProxyGenerator // This approach is used to prevent regenerating identical proxy types for identical T/Proxy pairs, // which would ultimately be a more expensive leak. // Proxy instances are not cached. Their lifetime is entirely owned by the caller of DispatchProxy.Create. + [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.")] + [SuppressMessage("Performance", "CA1810", Justification = "Requires to suppress a trim warning")] + static DispatchProxyGenerator() + { + s_makeGenericMethodMethod = typeof(MethodInfo).GetMethod("MakeGenericMethod", new Type[] { typeof(Type[]) })!; + } private static readonly Dictionary> s_baseTypeAndInterfaceToGeneratedProxyType = new Dictionary>(); private static readonly ProxyAssembly s_proxyAssembly = new ProxyAssembly(); private static readonly MethodInfo s_dispatchProxyInvokeMethod = typeof(DispatchProxy).GetMethod("Invoke", BindingFlags.NonPublic | BindingFlags.Instance)!; private static readonly MethodInfo s_getTypeFromHandleMethod = typeof(Type).GetRuntimeMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) })!; - private static readonly MethodInfo s_makeGenericMethodMethod = typeof(MethodInfo).GetMethod("MakeGenericMethod", new Type[] { typeof(Type[]) })!; + private static readonly MethodInfo s_makeGenericMethodMethod; // Returns a new instance of a proxy the derives from 'baseType' and implements 'interfaceType' + [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.")] internal static object CreateProxyInstance( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -75,6 +82,7 @@ internal static object CreateProxyInstance( return Activator.CreateInstance(proxiedType.GeneratedType, new object[] { proxiedType.MethodInfos })!; } + [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.")] private static GeneratedTypeInfo GetProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -97,9 +105,8 @@ private static GeneratedTypeInfo GetProxyType( } } - // Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", - Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] + // Generates IL2062 incorrectly interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed + [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.")] private static GeneratedTypeInfo GenerateProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -293,6 +300,7 @@ internal GeneratedTypeInfo CreateType() return new GeneratedTypeInfo(_tb.CreateType()!, _methodInfos.ToArray()); } + [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.")] internal void AddInterfaceImpl([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type iface) { // If necessary, generate an attribute to permit visibility @@ -391,6 +399,7 @@ internal void AddInterfaceImpl([DynamicallyAccessedMembers(DynamicallyAccessedMe } } + [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.")] private MethodBuilder AddMethodImpl(MethodInfo mi, int methodInfoIndex) { ParameterInfo[] parameters = mi.GetParameters(); diff --git a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs index e823c6a25c3c0..1765ea4697a95 100644 --- a/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs +++ b/src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs @@ -285,6 +285,7 @@ internal GenericTypeParameterBuilder() { } public override System.Type MakeArrayType() { throw null; } public override System.Type MakeArrayType(int rank) { throw null; } public override System.Type MakeByRefType() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 override System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; } public override System.Type MakePointerType() { throw null; } public void SetBaseTypeConstraint([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type? baseTypeConstraint) { } @@ -330,6 +331,7 @@ internal MethodBuilder() { } public override System.Reflection.ParameterInfo[] GetParameters() { throw null; } public override object Invoke(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; } public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 override System.Reflection.MethodInfo MakeGenericMethod(params System.Type[] typeArguments) { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { } @@ -562,6 +564,7 @@ public void DefineMethodOverride(System.Reflection.MethodInfo methodInfoBody, Sy public override System.Type MakeArrayType() { throw null; } public override System.Type MakeArrayType(int rank) { throw null; } public override System.Type MakeByRefType() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 override System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; } public override System.Type MakePointerType() { throw null; } public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { } diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Sentinels.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Sentinels.cs index 8073838e419d5..5f874e61e6204 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Sentinels.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Sentinels.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.TypeLoading { @@ -36,6 +37,7 @@ internal SentinelMethod() : base(Sentinels.RoType) { } public sealed override MethodInfo GetGenericMethodDefinition() => throw null!; public sealed override int GetHashCode() => throw null!; public sealed override MethodBody GetMethodBody() => throw null!; + [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 MethodInfo MakeGenericMethod(params Type[] typeArguments) => throw null!; protected sealed override MethodAttributes ComputeAttributes() => throw null!; protected sealed override CallingConventions ComputeCallingConvention() => throw null!; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoConstructedGenericMethod.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoConstructedGenericMethod.cs index da6871ecda8b7..c0096557651b1 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoConstructedGenericMethod.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoConstructedGenericMethod.cs @@ -49,6 +49,7 @@ internal RoConstructedGenericMethod(RoDefinitionMethod genericMethodDefinition, public sealed override MethodInfo GetGenericMethodDefinition() => _genericMethodDefinition; + [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 MethodInfo MakeGenericMethod(params Type[] typeArguments) => throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); public sealed override bool Equals([NotNullWhen(true)] object? obj) diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoDefinitionMethod.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoDefinitionMethod.cs index 9906ff4ef3c7c..d8cbb51704561 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoDefinitionMethod.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoDefinitionMethod.cs @@ -99,6 +99,7 @@ public sealed override bool Equals([NotNullWhen(true)] object? obj) public sealed override bool IsGenericMethodDefinition => GetGenericTypeParametersNoCopy().Length != 0; public sealed override MethodInfo GetGenericMethodDefinition() => IsGenericMethodDefinition ? this : throw new InvalidOperationException(); // Very uninformative but compatible exception + [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 MethodInfo MakeGenericMethod(params Type[] typeArguments) { if (typeArguments == null) diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoMethod.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoMethod.cs index fcecdffd976ea..a38fdb82ab4ed 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoMethod.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoMethod.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Reflection.TypeLoading @@ -110,6 +111,7 @@ public sealed override bool ContainsGenericParameters internal abstract RoType[] GetGenericTypeParametersNoCopy(); internal abstract RoType[] GetGenericTypeArgumentsNoCopy(); + [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 abstract override MethodInfo MakeGenericMethod(params Type[] typeArguments); public sealed override string ToString() => Loader.GetDisposedString() ?? this.ToString(ComputeMethodSigStrings()); diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoSyntheticMethod.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoSyntheticMethod.cs index 02f35b6f7b249..efba722ec3454 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoSyntheticMethod.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Methods/RoSyntheticMethod.cs @@ -86,6 +86,7 @@ public sealed override bool Equals([NotNullWhen(true)] object? obj) public sealed override bool IsGenericMethodDefinition => false; public sealed override bool IsConstructedGenericMethod => false; public sealed override MethodInfo GetGenericMethodDefinition() => throw new InvalidOperationException(); + [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 MethodInfo MakeGenericMethod(params Type[] typeArguments) => throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); protected sealed override RoType[] ComputeGenericArgumentsOrParameters() => Array.Empty(); internal sealed override RoType[] GetGenericTypeArgumentsNoCopy() => Array.Empty(); diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoConstructedGenericType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoConstructedGenericType.cs index ee2ae736c191a..67cd0f3e451ad 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoConstructedGenericType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoConstructedGenericType.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Text; using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute; @@ -115,6 +116,7 @@ public sealed override string ToString() internal sealed override RoType[] GetGenericTypeParametersNoCopy() => Array.Empty(); internal sealed override RoType[] GetGenericTypeArgumentsNoCopy() => _genericTypeArguments; protected internal sealed override RoType[] GetGenericArgumentsNoCopy() => _genericTypeArguments; + [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 InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); public sealed override Guid GUID => _genericTypeDefinition.GUID; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs index 9fc4830329fb7..94837cbfa2476 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoDefinitionType.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; namespace System.Reflection.TypeLoading @@ -83,6 +84,7 @@ public sealed override IEnumerable CustomAttributes protected sealed override IEnumerable ComputeDirectlyImplementedInterfaces() => SpecializeInterfaces(Instantiation); internal abstract IEnumerable SpecializeInterfaces(RoType[] instantiation); + [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) { if (typeArguments == null) diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoGenericParameterType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoGenericParameterType.cs index a6e6190f57fb1..c362695ee8dd5 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoGenericParameterType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoGenericParameterType.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute; namespace System.Reflection.TypeLoading @@ -42,6 +43,7 @@ protected RoGenericParameterType() internal sealed override RoType[] GetGenericTypeParametersNoCopy() => Array.Empty(); internal sealed override RoType[] GetGenericTypeArgumentsNoCopy() => Array.Empty(); protected internal sealed override RoType[] GetGenericArgumentsNoCopy() => Array.Empty(); + [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 InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); public sealed override int GenericParameterPosition => (_lazyPosition == -1) ? (_lazyPosition = ComputePosition()) : _lazyPosition; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoHasElementType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoHasElementType.cs index 6ffd1722a94b8..6db947ef2def2 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoHasElementType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoHasElementType.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute; namespace System.Reflection.TypeLoading @@ -60,6 +61,7 @@ protected RoHasElementType(RoType elementType) internal sealed override RoType[] GetGenericTypeParametersNoCopy() => Array.Empty(); internal sealed override RoType[] GetGenericTypeArgumentsNoCopy() => Array.Empty(); protected internal sealed override RoType[] GetGenericArgumentsNoCopy() => _elementType.GetGenericArgumentsNoCopy(); + [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 InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); public sealed override GenericParameterAttributes GenericParameterAttributes => throw new InvalidOperationException(SR.Arg_NotGenericParameter); diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoStubType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoStubType.cs index c8f3fb6967a7d..60a93a6b1964c 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoStubType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoStubType.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute; namespace System.Reflection.TypeLoading @@ -54,6 +55,7 @@ protected RoStubType() : base() { } internal sealed override RoType[] GetGenericTypeParametersNoCopy() => throw null!; internal sealed override RoType[] GetGenericTypeArgumentsNoCopy() => throw null!; protected internal sealed override RoType[] GetGenericArgumentsNoCopy() => throw null!; + [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 null!; public sealed override GenericParameterAttributes GenericParameterAttributes => throw null!; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoType.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoType.cs index e7b847e19b7d9..1a9755c088235 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoType.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoType.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute; @@ -278,6 +279,7 @@ public sealed override Type MakeArrayType(int rank) public sealed override Type MakeByRefType() => this.GetUniqueByRefType(); public sealed override Type MakePointerType() => this.GetUniquePointerType(); + [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 abstract override Type MakeGenericType(params Type[] typeArguments); // Enum methods diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 9e1a56a42a65a..f14f8f867140c 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -6838,6 +6838,7 @@ protected Type() { } public virtual System.Type MakeByRefType() { throw null; } public static System.Type MakeGenericMethodParameter(int position) { throw null; } public static System.Type MakeGenericSignatureType(System.Type genericTypeDefinition, params System.Type[] typeArguments) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; } public virtual System.Type MakePointerType() { throw null; } public static bool operator ==(System.Type? left, System.Type? right) { throw null; } @@ -11850,6 +11851,7 @@ protected MethodInfo() { } public override System.Type[] GetGenericArguments() { throw null; } public virtual System.Reflection.MethodInfo GetGenericMethodDefinition() { throw null; } public override int GetHashCode() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 System.Reflection.MethodInfo MakeGenericMethod(params System.Type[] typeArguments) { throw null; } public static bool operator ==(System.Reflection.MethodInfo? left, System.Reflection.MethodInfo? right) { throw null; } public static bool operator !=(System.Reflection.MethodInfo? left, System.Reflection.MethodInfo? right) { throw null; } @@ -13047,6 +13049,7 @@ public static void PrepareMethod(System.RuntimeMethodHandle method) { } public static void PrepareMethod(System.RuntimeMethodHandle method, System.RuntimeTypeHandle[]? instantiation) { } [System.ObsoleteAttribute("The Constrained Execution Region (CER) feature is not supported.", DiagnosticId = "SYSLIB0004", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] public static void ProbeForSufficientStack() { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Trimmer can't guarantee existence of class constructor")] public static void RunClassConstructor(System.RuntimeTypeHandle type) { } public static void RunModuleConstructor(System.ModuleHandle module) { } public static bool TryEnsureSufficientExecutionStack() { throw null; } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs index dc887bf1d6beb..f431161b26447 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs @@ -494,6 +494,7 @@ public override Type MakeByRefType() return new ByRefType(this); } + [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 override Type MakeGenericType(params Type[] typeArguments) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs index be43137a10153..cf879ace71dd1 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.Mono.cs @@ -561,6 +561,7 @@ private static Exception NotSupported() return new NotSupportedException("The invoked member is not supported in a dynamic module."); } + [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 override MethodInfo MakeGenericMethod(params Type[] typeArguments) { if (!IsGenericMethodDefinition) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs index 98c09481bfbdc..b2d0614ad5835 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs @@ -247,6 +247,7 @@ public override CallingConventions CallingConvention } } + [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 override MethodInfo MakeGenericMethod(params Type[] methodInstantiation) { if (!base_method.IsGenericMethodDefinition || (method_arguments != null)) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs index 26bca9f69f234..08e5e899c8389 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs @@ -1394,6 +1394,7 @@ public override Type MakeByRefType() return new ByRefType(this); } + [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 override Type MakeGenericType(params Type[] typeArguments) { //return base.MakeGenericType (typeArguments); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs index fc5cf2509b889..a6568737354d4 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs @@ -663,6 +663,7 @@ private Attribute GetDllImportAttribute() namedArgs); } + [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 override MethodInfo MakeGenericMethod(Type[] methodInstantiation) { if (methodInstantiation == null) diff --git a/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs index 241fee87a4c43..2d0a4b925096e 100644 --- a/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs @@ -1306,6 +1306,7 @@ public override Type[] GetGenericArguments() return types; } + [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 override Type MakeGenericType(Type[] instantiation) { if (instantiation == null) From ef498157ec4a385006e6acf75f1a8c8e2b6eb5e4 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 30 Jul 2021 12:52:53 -0700 Subject: [PATCH 2/8] build break fixes --- .../src/System.Reflection.Context.csproj | 2 ++ .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj index 4d30b81b51d9d..0ae87677f4c09 100644 --- a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj +++ b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj @@ -67,6 +67,8 @@ + diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs index ca14c4a823f9e..0546a40ab41a2 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs @@ -50,6 +50,7 @@ public static int GetHashCode(object? o) [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern object? GetObjectValue(object? obj); + [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 static void RunClassConstructor(RuntimeTypeHandle type) { if (type.Value == IntPtr.Zero) From efc14fdfa733bbc82e367063302f9db3909de636 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 30 Jul 2021 13:27:21 -0700 Subject: [PATCH 3/8] build break2 --- .../src/System.Reflection.Context.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj index 0ae87677f4c09..a343f9b4b77bf 100644 --- a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj +++ b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj @@ -67,7 +67,7 @@ - From 3f54b6a4d1580039938d03464533129dd8f4df1c Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 30 Jul 2021 14:27:42 -0700 Subject: [PATCH 4/8] build break3 --- .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs index 0546a40ab41a2..01446a2ad9e4a 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs @@ -50,7 +50,7 @@ public static int GetHashCode(object? o) [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern object? GetObjectValue(object? obj); - [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.")] + [RequiresUnreferencedCode("Trimmer can't guarantee existence of class constructor")] public static void RunClassConstructor(RuntimeTypeHandle type) { if (type.Value == IntPtr.Zero) From fc74d1dee6b894ad497648f7f7d7e66456ea7c95 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 30 Jul 2021 17:40:07 -0700 Subject: [PATCH 5/8] Suppressing DispatchProxy warnings since they are safe --- .../ref/System.Reflection.DispatchProxy.cs | 1 - .../src/System/Reflection/DispatchProxy.cs | 1 - .../src/System/Reflection/DispatchProxyGenerator.cs | 11 ++++------- .../src/System.Reflection.MetadataLoadContext.csproj | 2 ++ 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs b/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs index 084f3142c36cf..82e762d95f67f 100644 --- a/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs +++ b/src/libraries/System.Reflection.DispatchProxy/ref/System.Reflection.DispatchProxy.cs @@ -9,7 +9,6 @@ namespace System.Reflection public abstract partial class DispatchProxy { protected DispatchProxy() { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 static T Create<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TProxy>() where TProxy : System.Reflection.DispatchProxy { throw null; } protected abstract object? Invoke(System.Reflection.MethodInfo? targetMethod, object?[]? args); } diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs index 520caaf08aff7..dbfea73eebfc2 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxy.cs @@ -33,7 +33,6 @@ protected DispatchProxy() /// An object instance that implements . /// is a class, /// or is sealed or does not have a parameterless constructor - [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 static T Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TProxy>() where TProxy : DispatchProxy { diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index 03531e2f9f35a..c32367ba1149f 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -57,7 +57,8 @@ internal static class DispatchProxyGenerator // This approach is used to prevent regenerating identical proxy types for identical T/Proxy pairs, // which would ultimately be a more expensive leak. // Proxy instances are not cached. Their lifetime is entirely owned by the caller of DispatchProxy.Create. - [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.")] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Use of the MakeGenericMethod is done in a trim-safe way in DispatchProxyGenerator.")] [SuppressMessage("Performance", "CA1810", Justification = "Requires to suppress a trim warning")] static DispatchProxyGenerator() { @@ -70,7 +71,6 @@ static DispatchProxyGenerator() private static readonly MethodInfo s_makeGenericMethodMethod; // Returns a new instance of a proxy the derives from 'baseType' and implements 'interfaceType' - [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.")] internal static object CreateProxyInstance( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -82,7 +82,6 @@ internal static object CreateProxyInstance( return Activator.CreateInstance(proxiedType.GeneratedType, new object[] { proxiedType.MethodInfos })!; } - [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.")] private static GeneratedTypeInfo GetProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -105,8 +104,8 @@ private static GeneratedTypeInfo GetProxyType( } } - // Generates IL2062 incorrectly interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed - [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.")] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", + Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] private static GeneratedTypeInfo GenerateProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -300,7 +299,6 @@ internal GeneratedTypeInfo CreateType() return new GeneratedTypeInfo(_tb.CreateType()!, _methodInfos.ToArray()); } - [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.")] internal void AddInterfaceImpl([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type iface) { // If necessary, generate an attribute to permit visibility @@ -399,7 +397,6 @@ internal void AddInterfaceImpl([DynamicallyAccessedMembers(DynamicallyAccessedMe } } - [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.")] private MethodBuilder AddMethodImpl(MethodInfo mi, int methodInfoIndex) { ParameterInfo[] parameters = mi.GetParameters(); diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj b/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj index 2052687cb978e..f47e4161a10b6 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj @@ -142,6 +142,8 @@ + From 5585959ffe2aeff08956bdc5a787040fb75f52ad Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 3 Aug 2021 08:18:57 -0700 Subject: [PATCH 6/8] FB --- .../src/System.Reflection.Context.csproj | 9 +++++---- .../Reflection/DispatchProxyGenerator.cs | 19 +++++++++++-------- ...stem.Reflection.MetadataLoadContext.csproj | 8 ++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj index a343f9b4b77bf..202283df44129 100644 --- a/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj +++ b/src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj @@ -65,10 +65,11 @@ - - + + + + + diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index c32367ba1149f..239e6b5c105aa 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -57,18 +57,18 @@ internal static class DispatchProxyGenerator // This approach is used to prevent regenerating identical proxy types for identical T/Proxy pairs, // which would ultimately be a more expensive leak. // Proxy instances are not cached. Their lifetime is entirely owned by the caller of DispatchProxy.Create. - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", - Justification = "Use of the MakeGenericMethod is done in a trim-safe way in DispatchProxyGenerator.")] - [SuppressMessage("Performance", "CA1810", Justification = "Requires to suppress a trim warning")] - static DispatchProxyGenerator() - { - s_makeGenericMethodMethod = typeof(MethodInfo).GetMethod("MakeGenericMethod", new Type[] { typeof(Type[]) })!; - } private static readonly Dictionary> s_baseTypeAndInterfaceToGeneratedProxyType = new Dictionary>(); private static readonly ProxyAssembly s_proxyAssembly = new ProxyAssembly(); private static readonly MethodInfo s_dispatchProxyInvokeMethod = typeof(DispatchProxy).GetMethod("Invoke", BindingFlags.NonPublic | BindingFlags.Instance)!; private static readonly MethodInfo s_getTypeFromHandleMethod = typeof(Type).GetRuntimeMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) })!; - private static readonly MethodInfo s_makeGenericMethodMethod; + private static readonly MethodInfo s_makeGenericMethodMethod = GetGenericMethodMethodInfo(); + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Use of the MakeGenericMethod is done in a trim-safe way in DispatchProxyGenerator.")] + private static MethodInfo GetGenericMethodMethodInfo() + { + return typeof(MethodInfo).GetMethod("MakeGenericMethod", new Type[] { typeof(Type[]) })!; + } // Returns a new instance of a proxy the derives from 'baseType' and implements 'interfaceType' internal static object CreateProxyInstance( @@ -82,6 +82,9 @@ internal static object CreateProxyInstance( return Activator.CreateInstance(proxiedType.GeneratedType, new object[] { proxiedType.MethodInfos })!; } + // Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", + Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] private static GeneratedTypeInfo GetProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj b/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj index f47e4161a10b6..b2e36f3626019 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System.Reflection.MetadataLoadContext.csproj @@ -140,10 +140,10 @@ - - + + + + From 6680ba1aba76d6217bd21e7ec58342a58a36c058 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Wed, 11 Aug 2021 11:26:03 -0700 Subject: [PATCH 7/8] FB --- .../src/System/Reflection/DispatchProxyGenerator.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index 239e6b5c105aa..9ae8101100005 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -64,7 +64,8 @@ internal static class DispatchProxyGenerator private static readonly MethodInfo s_makeGenericMethodMethod = GetGenericMethodMethodInfo(); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", - Justification = "Use of the MakeGenericMethod is done in a trim-safe way in DispatchProxyGenerator.")] + Justification = "MakeGenericMethod is safe here because the user code invoking the generic method will reference " + + "the GenericTypes being used, which will guarantee the requirements of the generic method.")] private static MethodInfo GetGenericMethodMethodInfo() { return typeof(MethodInfo).GetMethod("MakeGenericMethod", new Type[] { typeof(Type[]) })!; @@ -107,6 +108,7 @@ private static GeneratedTypeInfo GetProxyType( } } + // Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] private static GeneratedTypeInfo GenerateProxyType( From 867fe6f12ca977d1fcf4788ad27083b7216d2c81 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Wed, 11 Aug 2021 13:01:12 -0700 Subject: [PATCH 8/8] typos --- .../src/System/Reflection/DispatchProxyGenerator.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index 9ae8101100005..edf5ff293ab5e 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -83,9 +83,6 @@ internal static object CreateProxyInstance( return Activator.CreateInstance(proxiedType.GeneratedType, new object[] { proxiedType.MethodInfos })!; } - // Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", - Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] private static GeneratedTypeInfo GetProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) @@ -110,7 +107,7 @@ private static GeneratedTypeInfo GetProxyType( // Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern", - Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] + Justification = "interfaceType is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")] private static GeneratedTypeInfo GenerateProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType)