diff --git a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs index a9505dc646664..bd108893d6c22 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs @@ -1,9 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Reflection; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; namespace System { @@ -77,6 +78,9 @@ private static bool InternalIsDefined(PropertyInfo element, Type attributeType, return false; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "rtPropAccessor.DeclaringType is guaranteed to have the specified property because " + + "rtPropAccessor.GetParentDefinition() returned a non-null MethodInfo.")] private static PropertyInfo? GetParentDefinition(PropertyInfo property, Type[] propertyParameters) { Debug.Assert(property != null); @@ -142,6 +146,9 @@ private static Attribute[] InternalGetCustomAttributes(EventInfo element, Type t return attributes; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "rtAdd.DeclaringType is guaranteed to have the specified event because " + + "rtAdd.GetParentDefinition() returned a non-null MethodInfo.")] private static EventInfo? GetParentDefinition(EventInfo ev) { Debug.Assert(ev != null); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Associates.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Associates.cs index 1b24ecd5e0a63..6a39fad34063b 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Associates.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Associates.cs @@ -34,8 +34,8 @@ internal static bool IncludeAccessor(MethodInfo? associate, bool nonPublic) [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Module.ResolveMethod is marked as RequiresUnreferencedCode because it relies on tokens" + - "which are not guaranteed to be stable across trimming. So if somebody harcodes a token it could break." + - "The usage here is not like that as all these tokes come from existing metadata loaded from some IL" + + "which are not guaranteed to be stable across trimming. So if somebody hardcodes a token it could break." + + "The usage here is not like that as all these tokens come from existing metadata loaded from some IL" + "and so trimming has no effect (the tokens are read AFTER trimming occured).")] private static RuntimeMethodInfo? AssignAssociates( int tkMethod, 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 44774438e0c23..dde85effd391a 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 @@ -106,7 +106,7 @@ public override Type MakeArrayType(int rank) public override string? FullName => m_strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); public override string? Namespace => m_type.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPatter", + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + "Currently this is not supported by linker. Once it is supported the outercall (Type.MakeGenericType)" + "will validate that the types fullfill the necessary requirements of annotations on type parameters." + diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeExceptionHandlingClause.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeExceptionHandlingClause.cs index ff5ea23c21ef5..fe3f22e1d9485 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeExceptionHandlingClause.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeExceptionHandlingClause.cs @@ -41,8 +41,8 @@ public override Type? CatchType { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Module.ResolveType is marked as RequiresUnreferencedCode because it relies on tokens" + - "which are not guaranteed to be stable across trimming. So if somebody harcodes a token it could break." + - "The usage here is not like that as all these tokes come from existing metadata loaded from some IL" + + "which are not guaranteed to be stable across trimming. So if somebody hardcodes a token it could break." + + "The usage here is not like that as all these tokens come from existing metadata loaded from some IL" + "and so trimming has no effect (the tokens are read AFTER trimming occured).")] get { diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs index bb6114e34bac1..d5b56e11e99db 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -37,6 +37,7 @@ internal RuntimeType GetTypeChecked() [MethodImpl(MethodImplOptions.InternalCall)] internal static extern bool IsInstanceOfType(RuntimeType type, [NotNullWhen(true)] object? o); + [RequiresUnreferencedCode("MakeGenericType cannot be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.")] internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, IntPtr pModifiers, int cModifiers) { Type type = typeStart; 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 e08e9ac5f0b6e..4bd0b84ddc71c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs @@ -4029,6 +4029,7 @@ private extern object InvokeDispMethod( #endregion #if FEATURE_COMINTEROP + [RequiresUnreferencedCode("The member might be removed")] private object? ForwardCallToInvokeMember( string memberName, BindingFlags flags, diff --git a/src/coreclr/System.Private.CoreLib/src/System/TypeNameParser.cs b/src/coreclr/System.Private.CoreLib/src/System/TypeNameParser.cs index 27f2cfd1671d5..7a9dbf91e201c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/TypeNameParser.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/TypeNameParser.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; @@ -53,6 +54,7 @@ internal sealed class TypeNameParser : IDisposable #endregion #region Static Members + [RequiresUnreferencedCode("The type might be removed")] internal static Type? GetType( string typeName, Func? assemblyResolver, @@ -102,6 +104,7 @@ public void Dispose() #endregion #region private Members + [RequiresUnreferencedCode("The type might be removed")] private unsafe Type? ConstructType( Func? assemblyResolver, Func? typeResolver, @@ -216,6 +219,7 @@ public void Dispose() return assembly; } + [RequiresUnreferencedCode("The type might be removed")] private static Type? ResolveType(Assembly? assembly, string[] names, Func? typeResolver, bool throwOnError, bool ignoreCase, ref StackCrawlMark stackMark) { Debug.Assert(names != null && names.Length > 0); diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.NonWindows.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.NonWindows.xml deleted file mode 100644 index 160dc54731f65..0000000000000 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.NonWindows.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - ILLink - IL2037 - member - M:System.AppDomain.GetThreadPrincipal - - - diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml index 8ff2cca73c53b..9afad56f2df8c 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml @@ -31,12 +31,6 @@ member M:System.Delegate.#ctor(System.Type,System.String) - - ILLink - IL2026 - member - M:System.Reflection.Associates.AssignAssociates(System.Int32,System.RuntimeType,System.RuntimeType) - ILLink IL2026 @@ -55,12 +49,6 @@ member M:System.Reflection.Emit.ModuleBuilder.GetTypeToken(System.String) - - ILLink - IL2026 - member - M:System.Reflection.RuntimeExceptionHandlingClause.get_CatchType - ILLink IL2026 @@ -73,12 +61,6 @@ member M:System.StartupHookProvider.ProcessStartupHooks() - - ILLink - IL2026 - member - M:System.TypeNameParser.ResolveType(System.Reflection.Assembly,System.String[],System.Func{System.Reflection.Assembly,System.String,System.Boolean,System.Type},System.Boolean,System.Boolean,System.Threading.StackCrawlMark@) - ILLink IL2050 @@ -121,24 +103,6 @@ member M:System.Reflection.Emit.TypeBuilder.GetMethod(System.Type,System.Reflection.MethodInfo) - - ILLink - IL2055 - member - M:System.Reflection.Emit.TypeBuilderInstantiation.Substitute(System.Type[]) - - - ILLink - IL2055 - member - M:System.Reflection.SignatureTypeExtensions.TryMakeGenericType(System.Type,System.Type[]) - - - ILLink - IL2055 - member - M:System.RuntimeTypeHandle.GetTypeHelper(System.Type,System.Type[],System.IntPtr,System.Int32) - ILLink IL2057 @@ -211,12 +175,6 @@ member M:System.Reflection.RuntimeAssembly.AddPublicNestedTypes(System.Type,System.Collections.Generic.List{System.Type},System.Collections.Generic.List{System.Exception}) - - ILLink - IL2070 - member - M:System.RuntimeType.GetMethodBase(System.RuntimeType,System.RuntimeMethodHandleInternal) - ILLink IL2072 @@ -247,18 +205,6 @@ member M:Internal.Runtime.InteropServices.ComponentActivator.InternalGetFunctionPointer(System.Runtime.Loader.AssemblyLoadContext,System.String,System.String,System.IntPtr) - - ILLink - IL2075 - member - M:System.Attribute.GetParentDefinition(System.Reflection.EventInfo) - - - ILLink - IL2075 - member - M:System.Attribute.GetParentDefinition(System.Reflection.PropertyInfo,System.Type[]) - ILLink IL2075 @@ -295,12 +241,6 @@ member M:System.StartupHookProvider.CallStartupHook(System.StartupHookProvider.StartupHookNameOrPath) - - ILLink - IL2075 - member - M:System.TypeNameParser.ResolveType(System.Reflection.Assembly,System.String[],System.Func{System.Reflection.Assembly,System.String,System.Boolean,System.Type},System.Boolean,System.Boolean,System.Threading.StackCrawlMark@) - ILLink IL2077 @@ -337,11 +277,5 @@ member M:System.Reflection.Emit.TypeBuilder.DefinePInvokeMethodHelper(System.String,System.String,System.String,System.Reflection.MethodAttributes,System.Reflection.CallingConventions,System.Type,System.Type[],System.Type[],System.Type[],System.Type[][],System.Type[][],System.Runtime.InteropServices.CallingConvention,System.Runtime.InteropServices.CharSet) - - ILLink - IL2085 - member - M:System.RuntimeType.ForwardCallToInvokeMember(System.String,System.Reflection.BindingFlags,System.Object,System.Object[],System.Boolean[],System.Int32[],System.Type[],System.Type) - diff --git a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs index 5101f42b1a781..c069cab08d11f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs @@ -401,7 +401,9 @@ public void SetThreadPrincipal(IPrincipal principal) // TODO: Remove these DynamicDependencyAttributes when https://github.com/mono/linker/issues/943 is fixed. [DynamicDependency("GetDefaultInstance", "System.Security.Principal.GenericPrincipal", "System.Security.Claims")] +#if TARGET_WINDOWS [DynamicDependency("GetDefaultInstance", "System.Security.Principal.WindowsPrincipal", "System.Security.Principal.Windows")] +#endif internal IPrincipal? GetThreadPrincipal() { IPrincipal? principal = _defaultPrincipal; diff --git a/src/libraries/System.Runtime/tests/TrimmingTests/InheritedAttributeTests.cs b/src/libraries/System.Runtime/tests/TrimmingTests/InheritedAttributeTests.cs new file mode 100644 index 0000000000000..8e28e1588f664 --- /dev/null +++ b/src/libraries/System.Runtime/tests/TrimmingTests/InheritedAttributeTests.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +[AttributeUsage(AttributeTargets.Property, Inherited = true)] +class FooAttribute : Attribute{ } +[AttributeUsage(AttributeTargets.Property, Inherited = true)] +class BarAttribute : Attribute { } +[AttributeUsage(AttributeTargets.Property, Inherited = true)] +class BazAttribute : Attribute { } + +class Base +{ + [Foo] + public virtual int X { get; } +} + +class Mid : Base +{ + [Bar] + public override int X => base.X; +} + +class Derived : Mid +{ + [Baz] + public override int X => base.X; +} + +class Program +{ + static int Main(string[] args) + { + int numAttributes = Attribute.GetCustomAttributes(typeof(Derived).GetProperty("X"), inherit: true).Length; + if (numAttributes == 3) + { + return 100; + } + + return -1; + } +} diff --git a/src/libraries/System.Runtime/tests/TrimmingTests/System.Runtime.TrimmingTests.proj b/src/libraries/System.Runtime/tests/TrimmingTests/System.Runtime.TrimmingTests.proj index ce80b61eec67b..80a1996c9b37f 100644 --- a/src/libraries/System.Runtime/tests/TrimmingTests/System.Runtime.TrimmingTests.proj +++ b/src/libraries/System.Runtime/tests/TrimmingTests/System.Runtime.TrimmingTests.proj @@ -9,6 +9,7 @@ + <_SuppressionsXmls Include="$(CoreLibSharedILLinkSuppressionsFilesPrefix).xml" /> - <_SuppressionsXmls Condition="'$(TargetOS)' != 'windows'" Include="$(CoreLibSharedILLinkSuppressionsFilesPrefix).NonWindows.xml" />