From d7eff5ded1fb83436e232709ff4af177c8b51279 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 25 Oct 2019 14:05:19 -0400 Subject: [PATCH] Remove ILLinkTrim.xml from System.Text.Json (dotnet/corefx#42118) * Remove ILLinkTrim.xml from System.Text.Json Instead use PreserveDependencyAttribute. * Address PR feedback Commit migrated from https://github.com/dotnet/corefx/commit/567241af05d1e43142d04572dee84870b78761aa --- .../PreserveDependencyAttribute.cs | 62 +++++++++++++++++++ .../System.Text.Json/src/ILLinkTrim.xml | 32 ---------- .../src/System.Text.Json.csproj | 3 + .../Converters/JsonConverterEnum.cs | 5 ++ .../Converters/JsonKeyValuePairConverter.cs | 2 + .../JsonClassInfo.AddProperty.cs | 3 + .../Serialization/JsonStringEnumConverter.cs | 5 ++ .../ReflectionEmitMemberAccessor.cs | 4 +- .../Serialization/ReflectionMemberAccessor.cs | 3 +- 9 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 src/libraries/Common/src/System/Runtime/CompilerServices/PreserveDependencyAttribute.cs delete mode 100644 src/libraries/System.Text.Json/src/ILLinkTrim.xml diff --git a/src/libraries/Common/src/System/Runtime/CompilerServices/PreserveDependencyAttribute.cs b/src/libraries/Common/src/System/Runtime/CompilerServices/PreserveDependencyAttribute.cs new file mode 100644 index 00000000000..de14ff35e04 --- /dev/null +++ b/src/libraries/Common/src/System/Runtime/CompilerServices/PreserveDependencyAttribute.cs @@ -0,0 +1,62 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#nullable enable + +// TODO https://github.com/dotnet/corefx/issues/41201: Design and expose this publicly. + +namespace System.Runtime.CompilerServices +{ + /// States a dependency that one member has on another. + /// + /// This can be used to inform tooling of a dependency that is otherwise not evident purely from + /// metadata and IL, for example a member relied on via reflection. + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Field /* AttributeTargets.Class | AttributeTargets.Struct */, // TODO: https://github.com/mono/linker/issues/797 + AllowMultiple = true, Inherited = false)] + internal sealed class PreserveDependencyAttribute : Attribute + { + /// Initializes the attribute. + /// The signature of the member depended. + public PreserveDependencyAttribute(string memberSignature) + { + MemberSignature = memberSignature; + } + + /// Initializes the attribute. + /// The signature of the member depended on. + /// The full name of the type containing . + public PreserveDependencyAttribute(string memberSignature, string typeName) + { + MemberSignature = memberSignature; + TypeName = typeName; + } + + /// Initializes the attribute. + /// The signature of the member depended on. + /// The full name of the type containing . + /// The name of the assembly containing . + public PreserveDependencyAttribute(string memberSignature, string typeName, string assemblyName) + { + MemberSignature = memberSignature; + TypeName = typeName; + AssemblyName = assemblyName; + } + + /// Gets the signature of the member depended on. + public string MemberSignature { get; } + + /// Gets the full name of the type containing the specified member. + /// If no type name is specified, the type of the consumer is assumed. + public string? TypeName { get; } + + /// Gets the assembly name of the specified type. + /// If no assembly name is specified, the assembly of the consumer is assumed. + public string? AssemblyName { get; } + + /// Gets or sets the condition in which the dependency is applicable, e.g. "DEBUG". + public string? Condition { get; set; } + } +} diff --git a/src/libraries/System.Text.Json/src/ILLinkTrim.xml b/src/libraries/System.Text.Json/src/ILLinkTrim.xml deleted file mode 100644 index 603e305f821..00000000000 --- a/src/libraries/System.Text.Json/src/ILLinkTrim.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 5eec31c1495..de2e37da40f 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -166,6 +166,9 @@ + + Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs + diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonConverterEnum.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonConverterEnum.cs index 7cdada18c46..17bb89ab4a3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonConverterEnum.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonConverterEnum.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Reflection; +using System.Runtime.CompilerServices; namespace System.Text.Json.Serialization.Converters { @@ -17,6 +18,10 @@ namespace System.Text.Json.Serialization.Converters return type.IsEnum; } + [PreserveDependency( + ".ctor(System.Text.Json.Serialization.Converters.EnumConverterOptions)", + "System.Text.Json.Serialization.Converters.JsonConverterEnum`1", + "System.Text.Json")] public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) { JsonConverter converter = (JsonConverter)Activator.CreateInstance( diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonKeyValuePairConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonKeyValuePairConverter.cs index 90d31281c30..13aa4e775e5 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonKeyValuePairConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonKeyValuePairConverter.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Reflection; +using System.Runtime.CompilerServices; namespace System.Text.Json.Serialization.Converters { @@ -18,6 +19,7 @@ namespace System.Text.Json.Serialization.Converters return (generic == typeof(KeyValuePair<,>)); } + [PreserveDependency(".ctor()", "System.Text.Json.Serialization.Converters.JsonKeyValuePairConverter`2", "System.Text.Json")] public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) { Type keyType = type.GetGenericArguments()[0]; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs index 2bf05a929d2..09397871eec 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs @@ -5,6 +5,7 @@ using System.Collections.Concurrent; using System.Diagnostics; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text.Json.Serialization; using System.Threading; @@ -44,6 +45,8 @@ namespace System.Text.Json options); } + [PreserveDependency(".ctor()", "System.Text.Json.JsonPropertyInfoNullable`2", "System.Text.Json")] + [PreserveDependency(".ctor()", "System.Text.Json.Serialization.JsonPropertyInfoNotNullableContravariant`4", "System.Text.Json")] internal static JsonPropertyInfo CreateProperty( Type declaredPropertyType, Type runtimePropertyType, diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs index 2cbd3a11675..bdd227bd6d9 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Reflection; +using System.Runtime.CompilerServices; using System.Text.Json.Serialization.Converters; namespace System.Text.Json.Serialization @@ -53,6 +54,10 @@ namespace System.Text.Json.Serialization } /// + [PreserveDependency( + ".ctor(System.Text.Json.Serialization.Converters.EnumConverterOptions, System.Text.Json.JsonNamingPolicy)", + "System.Text.Json.Serialization.Converters.JsonConverterEnum`1", + "System.Text.Json")] public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { JsonConverter converter = (JsonConverter)Activator.CreateInstance( diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs index baa6bc105f0..de70079b494 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Reflection; using System.Reflection.Emit; +using System.Runtime.CompilerServices; namespace System.Text.Json { @@ -60,6 +61,7 @@ namespace System.Text.Json return (Action)addMethod.CreateDelegate(typeof(Action), target); } + [PreserveDependency(".ctor()", "System.Text.Json.ImmutableEnumerableCreator`2", "System.Text.Json")] public override ImmutableCollectionCreator ImmutableCollectionCreateRange(Type constructingType, Type collectionType, Type elementType) { MethodInfo createRange = ImmutableCollectionCreateRangeMethod(constructingType, elementType); @@ -99,7 +101,7 @@ namespace System.Text.Json return creator; } - + [PreserveDependency(".ctor()", "System.Text.Json.ImmutableDictionaryCreator`2", "System.Text.Json")] public override ImmutableCollectionCreator ImmutableDictionaryCreateRange(Type constructingType, Type collectionType, Type elementType) { Debug.Assert(collectionType.IsGenericType); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs index 9d16c5b1cfc..83aedd14ffa 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs @@ -48,6 +48,7 @@ namespace System.Text.Json return (Action)addMethod.CreateDelegate(typeof(Action), target); } + [PreserveDependency(".ctor()", "System.Text.Json.ImmutableEnumerableCreator`2", "System.Text.Json")] public override ImmutableCollectionCreator ImmutableCollectionCreateRange(Type constructingType, Type collectionType, Type elementType) { MethodInfo createRange = ImmutableCollectionCreateRangeMethod(constructingType, elementType); @@ -70,7 +71,7 @@ namespace System.Text.Json return creator; } - + [PreserveDependency(".ctor()", "System.Text.Json.ImmutableDictionaryCreator`2", "System.Text.Json")] public override ImmutableCollectionCreator ImmutableDictionaryCreateRange(Type constructingType, Type collectionType, Type elementType) { Debug.Assert(collectionType.IsGenericType); -- 2.34.1