dotnet / dotnet/linker

Report better location for warnings from assembly level attributes

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

Description

Take this as an example:
```C#
[assembly: asmattr.My(typeof (asmattr.TestClass))]

namespace asmattr
{
public class MyAttribute : Attribute
{
public MyAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type) { }
}

internal class TestClass
{
[RequiresUnreferencedCode("Some")]
public TestClass() { }
}
}
```

The assembly level attribute should produce `IL2026` and it does, but it does say where it came from:
```
ILLink : Trim analysis warning IL2026: asmattr: Using member 'asmattr.TestClass.TestClass()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Some.
```

Note that the location in this case is the name of the assembly (`asmattr` in this case).

In order to figure this out one needs to:
* Know that this can happen due to applied DAM annotation
* Know that the annotation would have to be applied to one of the owning types - in this case `TestClass`
* Search the code for usage of `TestClass`
* Know that the `MyAttribute` constructor applies DAM onto the parameter

It gets worse if this is on a subclass (since I need to know to look for usage of any of the owning types).

Linker can't report the exact location, because PDB has no information about location of attribute instantiation. Normally if the attribute is applied on a type/method level, we would report the location as that type/method. Which is definitely better, but still relatively confusing.

Ideally we would report that this is coming from an attribute usage (and which attribute). So something like:
```
ILLink : Trim analysis warning IL2026: asmattr, assembly level attribute asmattr.MyAttribute: Using member 'asmattr.TestClass.TestClass()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Some.
```

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.