Skip to content

ILLink: Trim analyzer doesn't warn on reflection access to annotated members when the source of the Type is GetType on a DAM-annotated class #102002

Description

@jtschuster

The following code in Test() should cause warnings to appear on MethodWithDataflow and MethodWithRequires because o is known to be DamOnTypeAccessesMembers which is annotated with DAMT.All, and is passed to RequiresAll, enforcing the requirement. The analyzer should recognize the pattern of Object.GetType() on a statically known type with DAM on the type.

Related to #93720

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public class DamOnTypeAccessesMembers
{
	[ExpectedWarning("IL2114", nameof(MethodWithDataflow), nameof(DynamicallyAccessedMembersAttribute), Tool.Trimmer | Tool.NativeAot, "")]
	public void MethodWithDataflow ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type type) { }

	[RequiresDynamicCode ("--MethodWithRequires--")]
	[RequiresUnreferencedCode("--MethodWithRequires--")]
	[RequiresAssemblyFiles("--MethodWithRequires--")]
	[ExpectedWarning("IL2112", "requires unreferenced code", "--MethodWithRequires--", Tool.Trimmer | Tool.NativeAot, "")]
	public void MethodWithRequires () { }

	public static void Test ()
	{
		var o = new DamOnTypeAccessesMembers ();
		var t = o.GetType ();
		t.RequiresAll();
		t.ToString ();
		// This triggers the warnings in the analyzer, but not Object.GetType() on DamOnTypeAccessesMembers
		//typeof(DamOnTypeAccessesMembers).RequiresAll();
	}
}

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzersin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

Status
No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions