Skip to content

Latest linker is raising unexpected warnings in System.Text.Json #2122

@eerhardt

Description

@eerhardt

With change #2108, we are now getting unexpected ILLink warnings in System.Text.Json. This is blocking merging the latest linker into dotnet/runtime (dotnet/runtime#54907):

ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonArray: Using method 'System.Text.Json.Nodes.JsonNode.CreateDynamicObject(Expression,JsonNode)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Using JsonNode instances as dynamic types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.
ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonArray: Using method 'System.Text.Json.Nodes.JsonArray.Add<T>(T)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.
ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonArray: Using method 'System.Text.Json.Nodes.JsonNode.TrySetMemberMethodInfo.get' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
ILLink: Trim analysis warning IL2026: System.Text.Json.Serialization.Metadata.JsonTypeInfo: Using method 'System.Text.Json.Serialization.Metadata.JsonTypeInfo.JsonTypeInfo(Type,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
ILLink: Trim analysis warning IL2026: System.Text.Json.Serialization.Metadata.JsonTypeInfo: Using method 'System.Text.Json.Serialization.Metadata.JsonTypeInfo.JsonTypeInfo(Type,JsonConverter,Type,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonValue`1: Using method 'System.Text.Json.Nodes.JsonValue.Create<T>(T,Nullable<JsonNodeOptions>)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed. Use the overload that takes a JsonTypeInfo, or make sure all of the required types are preserved.
ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonValue`1: Using method 'System.Text.Json.Nodes.JsonNode.CreateDynamicObject(Expression,JsonNode)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Using JsonNode instances as dynamic types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.
ILLink: Trim analysis warning IL2026: System.Text.Json.Nodes.JsonValue`1: Using method 'System.Text.Json.Nodes.JsonNode.TrySetMemberMethodInfo.get' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

I reverted 2f62607 locally and verified that without that change we don't get any warnings. So this feels like a bug introduced with that change.

Going through the warnings listed above, all those methods do have [RequiresUnreferencedCode] applied to them, but all calls of the unsafe methods are either suppressed, or the caller has [RequiresUnreferencedCode] itself. So I'm not seeing what is wrong with the library code.

@marek-safar @agocke @sbomer @MichalStrehovsky

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions