Remove ILLinkTrim.xml from System.Text.Json (dotnet/corefx#42118)
authorStephen Toub <stoub@microsoft.com>
Fri, 25 Oct 2019 18:05:19 +0000 (14:05 -0400)
committerGitHub <noreply@github.com>
Fri, 25 Oct 2019 18:05:19 +0000 (14:05 -0400)
* Remove ILLinkTrim.xml from System.Text.Json

Instead use PreserveDependencyAttribute.

* Address PR feedback

Commit migrated from https://github.com/dotnet/corefx/commit/567241af05d1e43142d04572dee84870b78761aa

src/libraries/Common/src/System/Runtime/CompilerServices/PreserveDependencyAttribute.cs [new file with mode: 0644]
src/libraries/System.Text.Json/src/ILLinkTrim.xml [deleted file]
src/libraries/System.Text.Json/src/System.Text.Json.csproj
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonConverterEnum.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonKeyValuePairConverter.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs

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 (file)
index 0000000..de14ff3
--- /dev/null
@@ -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
+{
+    /// <summary>States a dependency that one member has on another.</summary>
+    /// <remarks>
+    /// 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.
+    /// </remarks>
+    [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
+    {
+        /// <summary>Initializes the attribute.</summary>
+        /// <param name="memberSignature">The signature of the member depended.</param>
+        public PreserveDependencyAttribute(string memberSignature)
+        {
+            MemberSignature = memberSignature;
+        }
+
+        /// <summary>Initializes the attribute.</summary>
+        /// <param name="memberSignature">The signature of the member depended on.</param>
+        /// <param name="typeName">The full name of the type containing <paramref name="memberSignature"/>.</param>
+        public PreserveDependencyAttribute(string memberSignature, string typeName)
+        {
+            MemberSignature = memberSignature;
+            TypeName = typeName;
+        }
+
+        /// <summary>Initializes the attribute.</summary>
+        /// <param name="memberSignature">The signature of the member depended on.</param>
+        /// <param name="typeName">The full name of the type containing <paramref name="memberSignature"/>.</param>
+        /// <param name="assemblyName">The name of the assembly containing <paramref name="typeName"/>.</param>
+        public PreserveDependencyAttribute(string memberSignature, string typeName, string assemblyName)
+        {
+            MemberSignature = memberSignature;
+            TypeName = typeName;
+            AssemblyName = assemblyName;
+        }
+
+        /// <summary>Gets the signature of the member depended on.</summary>
+        public string MemberSignature { get; }
+
+        /// <summary>Gets the full name of the type containing the specified member.</summary>
+        /// <remarks>If no type name is specified, the type of the consumer is assumed.</remarks>
+        public string? TypeName { get; }
+
+        /// <summary>Gets the assembly name of the specified type.</summary>
+        /// <remarks>If no assembly name is specified, the assembly of the consumer is assumed.</remarks>
+        public string? AssemblyName { get; }
+
+        /// <summary>Gets or sets the condition in which the dependency is applicable, e.g. "DEBUG".</summary>
+        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 (file)
index 603e305..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<linker>
-  <assembly fullname="System.Text.Json">
-    <type fullname="System.Text.Json.JsonPropertyInfoNullable`2">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.Serialization.JsonPropertyInfoNotNullableContravariant`4">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.Serialization.Converters.JsonConverterEnum`1">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.Serialization.Converters.JsonKeyValuePairConverter`2">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.ImmutableCollectionCreator">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.ImmutableDictionaryCreator`2">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-    <type fullname="System.Text.Json.ImmutableEnumerableCreator`2">
-      <!-- Instantiated via reflection -->
-      <method name=".ctor" />
-    </type>
-  </assembly>
-</linker>
index 5eec31c..de2e37d 100644 (file)
     <Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs" />
     <Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs" />
     <Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs" />
+    <Compile Include="$(CommonPath)\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs">
+      <Link>Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs</Link>
+    </Compile>
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsNETStandard)' == 'true' OR '$(TargetsNetFx)' == 'true'">
     <Compile Include="System\Collections\Generic\StackExtensions.netstandard.cs" />
index 7cdada1..17bb89a 100644 (file)
@@ -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(
index 90d3128..13aa4e7 100644 (file)
@@ -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];
index 2bf05a9..0939787 100644 (file)
@@ -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,
index 2cbd3a1..bdd227b 100644 (file)
@@ -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
         }
 
         /// <inheritdoc />
+        [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(
index baa6bc1..de70079 100644 (file)
@@ -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<TProperty>)addMethod.CreateDelegate(typeof(Action<TProperty>), 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);
index 9d16c5b..83aedd1 100644 (file)
@@ -48,6 +48,7 @@ namespace System.Text.Json
             return (Action<TProperty>)addMethod.CreateDelegate(typeof(Action<TProperty>), 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);