dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

Trim analysis doesn't track AssemblyQualifiedName of a known type #108082

Open sbomer opened 1 month ago

sbomer commented 1 month ago
using System.Diagnostics.CodeAnalysis;

// warning IL2062: Value passed to parameter 's' of method 'G(String)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
G(typeof(C).AssemblyQualifiedName);

static void G([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] string s) {
    Activator.CreateInstance(Type.GetType(s));
}

class C {}

This produces a warning because the dataflow analysis treats the result of typeof(C).AssemblyQualifiedName) as unknown. Instead it should track the result as a known type name and not produce any warnings (note that G("C, assemblyname") works and does not warn).

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas See info in area-owners.md if you want to be subscribed.