Use different warning code/message for "maybe-used" RequiresUnreferencedCode methods than "definitely used" RequiredUnreferencedCode methods
- Dominant language
- C#
- Stars
- 392
- Forks
- 128
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 2
Description
Today, the warning code IL2026 and the corresponding message is used for both cases where code directly calls a method that has the RequiresUnreferencedCode attribute and for cases where a DynamicallyAccessedMembers attribute preserves methods with the attribute.
For example the following code generates IL2026 as expected:
```
var bar = Foo("test"); // IL2026
[RequiresUnreferencedCode("")]
int Foo(string str);
```
However, the following also generates IL2026, without a direct reference to a method that has the `RequiresUnreferencedCode` attribute:
```
Baz(); // IL2026
void Baz<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]T>() where T:new()
{
return typeof(T).GetMethod("Safe").Invoke(new T());
}
class Foo
{
public void Safe();
[RequiresUnreferencedCode("")]
public void Bar();
}
```
An instance of this is in C#/WinRT:
> Trim analysis warning IL2026: WinRT.ComWrappersSupport.CreateRcwForComObject(IntPtr): Using method 'System.Type.GetType(String,Func,Func)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The type might be removed.
This warning shows up when a method with a generic parameter with the above annotation is instantiated with System.Type. The warning as written causes user confusion, because there is no call to that particular method. The first time I saw this issue, I spent nearly an hour trying to figure out what was going on (this also happens with any generic with the above annotation on any `Delegate`-derived type, so it's not just on System.Type).
By using a different warning code, users could specifically suppress the "maybe-used" warning and not risk accidentally suppressing a warning about another function that they explicitly call being marked with RequiresUnreferencedCode. And by including a special warning message that mentions that the method "might be called" instead of using wording that makes it seem that the method is definitely used, the warning would be more easily understandable.
If this warning could use a different error code and have a message something like the following, I would much appreciate it:
> Trim analysis warning ILXXXX: WinRT.ComWrappersSupport.CreateRcwForComObject(IntPtr): The type 'System.Type' is used in a case with {DynamicallyAccessedMembers annotations} annotations and has a preserved member with the 'RequiresUnreferencedCodeAttribute', 'System.Type.GetType(String,Func,Func)', which can break functionality when trimming application code. {RequiresUnreferencedCode message}
cc: @vitek-karas
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.