Skip to content

Commit 8969e1e

Browse files
committed
Resolve ILLink warnings in System.Memory.Data
The current APIs require unreferenced code, since they call into JsonSerializer without providing JsonTypeInfo. #54979 is logged to add "trim compatible" APIs here.
1 parent ca00588 commit 8969e1e

File tree

5 files changed

+17
-47
lines changed

5 files changed

+17
-47
lines changed

src/libraries/System.Memory.Data/ref/System.Memory.Data.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ namespace System
99
public partial class BinaryData
1010
{
1111
public BinaryData(byte[] data) { }
12+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
1213
public BinaryData(object? jsonSerializable, System.Text.Json.JsonSerializerOptions? options = null, System.Type? type = null) { }
1314
public BinaryData(System.ReadOnlyMemory<byte> data) { }
1415
public BinaryData(string data) { }
15-
public static BinaryData Empty { get; }
16+
public static System.BinaryData Empty { get { throw null; } }
1617
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
17-
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; }
18+
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
1819
public static System.BinaryData FromBytes(byte[] data) { throw null; }
1920
public static System.BinaryData FromBytes(System.ReadOnlyMemory<byte> data) { throw null; }
21+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
2022
public static System.BinaryData FromObjectAsJson<T>(T jsonSerializable, System.Text.Json.JsonSerializerOptions? options = null) { throw null; }
2123
public static System.BinaryData FromStream(System.IO.Stream stream) { throw null; }
2224
public static System.Threading.Tasks.Task<System.BinaryData> FromStreamAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
@@ -27,6 +29,7 @@ public BinaryData(string data) { }
2729
public static implicit operator System.ReadOnlySpan<byte> (System.BinaryData? data) { throw null; }
2830
public byte[] ToArray() { throw null; }
2931
public System.ReadOnlyMemory<byte> ToMemory() { throw null; }
32+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
3033
public T? ToObjectFromJson<T>(System.Text.Json.JsonSerializerOptions? options = null) { throw null; }
3134
public System.IO.Stream ToStream() { throw null; }
3235
public override string ToString() { throw null; }

src/libraries/System.Memory.Data/ref/System.Memory.Data.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<ItemGroup>
77
<Compile Include="System.Memory.Data.cs" />
88
</ItemGroup>
9+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net5.0'))">
10+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
11+
</ItemGroup>
912
<ItemGroup>
1013
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\ref\System.Text.Json.csproj" />
1114
</ItemGroup>

src/libraries/System.Memory.Data/src/ILLink/ILLink.Suppressions.xml

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libraries/System.Memory.Data/src/System.Memory.Data.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
Link="Common\System\Threading\Tasks\TaskToApm.cs" />
1414
</ItemGroup>
1515

16+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net5.0'))">
17+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
18+
</ItemGroup>
19+
1620
<ItemGroup>
1721
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\src\System.Text.Json.csproj" />
1822
</ItemGroup>

src/libraries/System.Memory.Data/src/System/BinaryData.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.ComponentModel;
55
using System.Diagnostics.CodeAnalysis;
66
using System.IO;
7-
using System.Runtime.InteropServices;
87
using System.Text;
98
using System.Text.Json;
109
using System.Threading;
@@ -17,6 +16,8 @@ namespace System
1716
/// </summary>
1817
public class BinaryData
1918
{
19+
private const string JsonSerializerRequiresUnreferencedCode = "JSON serialization and deserialization might require types that cannot be statically analyzed.";
20+
2021
/// <summary>
2122
/// The backing store for the <see cref="BinaryData"/> instance.
2223
/// </summary>
@@ -41,12 +42,12 @@ public BinaryData(byte[] data)
4142
/// Creates a <see cref="BinaryData"/> instance by serializing the provided object to JSON
4243
/// using <see cref="JsonSerializer"/>.
4344
/// </summary>
44-
///
4545
/// <param name="jsonSerializable">The object that will be serialized to JSON using
4646
/// <see cref="JsonSerializer"/>.</param>
4747
/// <param name="options">The options to use when serializing to JSON.</param>
4848
/// <param name="type">The type to use when serializing the data. If not specified, <see cref="object.GetType"/> will
4949
/// be used to determine the type.</param>
50+
[RequiresUnreferencedCode(JsonSerializerRequiresUnreferencedCode)]
5051
public BinaryData(object? jsonSerializable, JsonSerializerOptions? options = default, Type? type = default)
5152
{
5253
type ??= jsonSerializable?.GetType() ?? typeof(object);
@@ -177,12 +178,11 @@ private static async Task<BinaryData> FromStreamAsync(Stream stream, bool async,
177178
/// Creates a <see cref="BinaryData"/> instance by serializing the provided object using
178179
/// the <see cref="JsonSerializer"/>.
179180
/// </summary>
180-
///
181181
/// <typeparam name="T">The type to use when serializing the data.</typeparam>
182182
/// <param name="jsonSerializable">The data to use.</param>
183183
/// <param name="options">The options to use when serializing to JSON.</param>
184-
///
185184
/// <returns>A value representing the UTF-8 encoding of the JSON representation of <paramref name="jsonSerializable" />.</returns>
185+
[RequiresUnreferencedCode(JsonSerializerRequiresUnreferencedCode)]
186186
public static BinaryData FromObjectAsJson<T>(T jsonSerializable, JsonSerializerOptions? options = default)
187187
{
188188
byte[] buffer = JsonSerializer.SerializeToUtf8Bytes(jsonSerializable, typeof(T), options);
@@ -230,6 +230,7 @@ public override unsafe string ToString()
230230
/// converted to.</typeparam>
231231
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use when serializing to JSON.</param>
232232
/// <returns>The data converted to the specified type.</returns>
233+
[RequiresUnreferencedCode(JsonSerializerRequiresUnreferencedCode)]
233234
public T? ToObjectFromJson<T>(JsonSerializerOptions? options = default)
234235
{
235236
return JsonSerializer.Deserialize<T>(_bytes.Span, options);

0 commit comments

Comments
 (0)