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();
	}
}

Activity

  1. added
    area-Tools-ILLink
    on May 7, 2024
  2. added
    untriaged
    on May 7, 2024
  3. dotnet-policy-service commented on May 7, 2024

    @dotnet-policy-service
    Contributor

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

  4. added this to the 9.0.0 milestone on Jun 3, 2024
  5. removed
    untriaged
    on Jun 3, 2024
  6. added
    in-pr
    on Aug 5, 2024
  7. self-assigned this
    on Aug 5, 2024
  8. reopened this on Aug 7, 2024
  9. modified the milestones: 9.0.0, 10.0.0 on Aug 13, 2024
  10. locked and limited conversation to collaborators on Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLinkin-pr

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions