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

Allow trimming FeatureGuard and FeatureSwitchDefinition attributes #100263

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
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 @@ -250,6 +250,12 @@
<type fullname="System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
<type fullname="System.Diagnostics.CodeAnalysis.FeatureGuardAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
<type fullname="System.Diagnostics.CodeAnalysis.FeatureSwitchDefinitionAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
<type fullname="System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Reflection;

/// <summary>
/// Ensures setting _AggressiveAttributeTrimming = true causes various attributes to be trimmed
/// </summary>
class Program
{
[UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2111", Justification = "Expected trim warning for reflection over annotated members.")]
[UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2026", Justification = "Expected trim warning for reflection over annotated members.")]
static int Main(string[] args)
{
// Reference to IsDynamicCodeSupported (which has FeatureGuard(typeof(RequiresDynamicCodeAttribute)))
// should not produce a warning because both RequiresDynamicCodeAttribute and FeatureGuardAttribute are removed.
if (RuntimeFeature.IsDynamicCodeSupported)
{
UseDynamicCode();
}

// Check that a few attribute instances are indeed removed
CheckRemovedAttributes(typeof(MembersWithRemovedAttributes));

return 100;
}

[RequiresDynamicCode(nameof(UseDynamicCode))]
static void UseDynamicCode() { }

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

[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
static bool FeatureGuard => throw null!;

[FeatureSwitchDefinition("Program.MembersWithRemovedAttributes.FeatureSwitchDefinition")]
static bool FeatureSwitchDefinition => throw null!;

[RequiresDynamicCode(nameof(RequiresDynamicCode))]
static void RequiresDynamicCode() { }

[RequiresUnreferencedCode(nameof(RequiresUnreferencedCode))]
static void RequiresUnreferencedCode() { }
}

static void CheckRemovedAttributes([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{
Console.WriteLine($"Validating {type}");
foreach (var member in type.GetMembers(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
CheckRemovedAttributes(member);

if (member is MethodInfo method)
{
foreach (var parameter in method.GetParameters())
{
CheckRemovedAttributes(parameter);
}
}
}
}

static void CheckRemovedAttributes(ICustomAttributeProvider provider)
{
foreach (var attribute in provider.GetCustomAttributes(false))
{
if (attribute is NullableContextAttribute)
continue;

throw new Exception($"Unexpected attribute {attribute.GetType()} on {provider}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<ItemGroup>
<TestConsoleAppSourceFiles Include="AggressiveAttributeTrimmingTest.cs">
<EnabledProperties>_AggressiveAttributeTrimming</EnabledProperties>
<DisabledProperties>SuppressTrimAnalysisWarnings;TrimmerSingleWarn</DisabledProperties>
</TestConsoleAppSourceFiles>
<TestConsoleAppSourceFiles Include="AppDomainGetThreadGenericPrincipalTest.cs" />
<TestConsoleAppSourceFiles Include="AppDomainGetThreadWindowsPrincipalTest.cs">
<SkipOnTestRuntimes>osx-x64;linux-x64;browser-wasm</SkipOnTestRuntimes>
Expand Down
Loading