dotnet / dotnet/linker

Type hierarchy overmarks non-public methods on unannotated base types

Open
#2,813 1 comment 0 reactions 0 assignees View on GitHub
area-Linker: DataFlow
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

```C#
class SuperBase
{
private void SuperBasePrivateMethod () { } // Is not preserved
}

class Base : SuperBase
{
private void BasePrivateMethod () { } // This doesn't need to be preserved, but it is
}

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicMethods)]
class Derived : Base
{
private void DerivedPrivateMethod () { } // This needs to be preserved due to the annotation
}

static Derived _instance;

public static void Test()
{
_instance = new Derived ();
Type type = _instance.GetType ();

type.RequiresNonPublicMethods (); // No warning (correct)
type.BaseType.RequiresNonPublicMethods (); // WARNS - IL2072 since non-public annotation doesn't propagate to base types
}
```

Non-public annotations don't propagate to base types (as is visible in the sample above, where the `type.BaseType` warns. But type hierarchy marking doesn't consider this for the first level base type and it applies the same annotation as for the derived type which is annotated.

If the annotation is specified on the Derived type it should be applied to base but without non-public annotations.

Note that not marking this method is perfectly safe since the annotations data flow correctly handles this - as seen by the code above producing a warning when trying to access the private method on the base type.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.