Skip to content

ILC: missing warning for DAM on type when used through derived type #104740

@sbomer

Description

@sbomer

DAM on type is supposed to ensure that GetType called on that type or any derived type will satisfy the DAM annotations. In this example, d.GetType() should keep public methods on the base type and produce a warning for the RUC method.

using System.Diagnostics.CodeAnalysis;

class Program {
    public static void Main() {
        RequirePublicMethods(d.GetType());
    }

    static D d = new();

    static void RequirePublicMethods([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type t) {}
}


[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
class B {
    [RequiresUnreferencedCode("M")]
    public virtual void M() {} // There should be a warning about reflection access (through DAM-on-type) to M
}

class D : B {}

ProcessTypeGetTypeDataflow gets called for the derived type D, but nothing applies the annotations on B. This logic doesn't do it because it assumes B's annotations were already applied, and so doesn't keep B's members while processing D:

if (type.HasBaseType)
{
var baseAnnotation = flowAnnotations.GetTypeAnnotation(type.BaseType);
var annotationToApplyToBase = Annotations.GetMissingMemberTypes(annotation, baseAnnotation);
// Apply any annotations that didn't exist on the base type to the base type.
// This may produce redundant warnings when the annotation is DAMT.All or DAMT.PublicConstructors and the base already has a
// subset of those annotations.
reflectionMarker.MarkTypeForDynamicallyAccessedMembers(origin, type.BaseType, annotationToApplyToBase, type.GetDisplayName(), declaredOnly: false);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-NativeAOT-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions