Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress trim warning caused by recent attribute removal changes #62023

Merged
merged 1 commit into from
Nov 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,23 @@ internal sealed partial class ReflectTypeDescriptionProvider : TypeDescriptionPr

// These are attributes that, when we discover them on interfaces, we do
// not merge them into the attribute set for a class.
private static readonly Type[] s_skipInterfaceAttributeList = new Type[]
private static readonly Type[] s_skipInterfaceAttributeList = InitializeSkipInterfaceAttributeList();
Copy link
Member

@am11 am11 Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add UnconditionalSuppressMessage to this field directly, without adding InitializeSkipInterfaceAttributeList method? It seems to target all applicable constructs.

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that won't work. The warning is coming from a static constructor - which in this case is generated by the compiler. Currently the trimmer has no logic to try to map the warning from a static constructor back to the field (not even sure if it's possible in most cases).
First I simply added an explicit static .cctor, but Roslyn doesn't like it - probably due to beforefieldinit optimization which is really only possible when no explicit .cctor is specified. So the static method is a workaround to that.


[UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2045:AttributeRemoval",
Justification = "The ComVisibleAttribute is marked for removal and it's referenced here. Since this array" +
"contains only attributes which are going to be ignored, removing such attribute" +
"will not break the functionality in any way.")]
private static Type[] InitializeSkipInterfaceAttributeList()
{
return new Type[]
{
#if FEATURE_SKIP_INTERFACE
typeof(System.Runtime.InteropServices.GuidAttribute),
typeof(System.Runtime.InteropServices.InterfaceTypeAttribute)
typeof(System.Runtime.InteropServices.GuidAttribute),
typeof(System.Runtime.InteropServices.InterfaceTypeAttribute)
#endif
typeof(System.Runtime.InteropServices.ComVisibleAttribute),
};
typeof(System.Runtime.InteropServices.ComVisibleAttribute),
};
}


internal static Guid ExtenderProviderKey { get; } = Guid.NewGuid();
Expand Down