Skip to content

Commit

Permalink
Use FeatureSwitchDefinition in a few places (#106564)
Browse files Browse the repository at this point in the history
This replaces much of our ILLink.Substitutions.xml with nearly
equivalent FeatureSwitchDefinitionAttribute. The minor difference is
that both true and false are substituted (when the feature switch is
set accordingly), whereas some of our substitution xmls only defined a
substitution for the false case.

---------

Co-authored-by: Jackson Schuster <[email protected]>
  • Loading branch information
sbomer and jtschuster authored Aug 27, 2024
1 parent 231cbb5 commit 0fcba3f
Show file tree
Hide file tree
Showing 58 changed files with 69 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

<ItemGroup>
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
<ILLinkSubstitutionsXmls Condition="'$(TargetsWindows)' == 'true'" Include="$(ILLinkDirectory)ILLink.Substitutions.Windows.xml" />
</ItemGroup>

<!-- Note that various places in SPCL depend on this resource name i.e. TplEventSource -->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Internal.Runtime.InteropServices
[SupportedOSPlatform("windows")]
internal static class InMemoryAssemblyLoader
{
[FeatureSwitchDefinition("System.Runtime.InteropServices.EnableCppCLIHostActivation")]
private static bool IsSupported { get; } = InitializeIsSupported();
private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableCppCLIHostActivation", out bool isSupported) ? isSupported : true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

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

Expand Down Expand Up @@ -59,6 +60,7 @@ public static void ApplyUpdate(Assembly assembly, ReadOnlySpan<byte> metadataDel
/// <summary>
/// Returns true if the apply assembly update is enabled and available.
/// </summary>
[FeatureSwitchDefinition("System.Reflection.Metadata.MetadataUpdater.IsSupported")]
public static bool IsSupported { get; } = IsApplyUpdateSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ internal static unsafe bool IsPinnable(object? obj)
=> (obj == null) || !RuntimeHelpers.GetMethodTable(obj)->ContainsGCPointers;

#if TARGET_WINDOWS
[FeatureSwitchDefinition("System.Runtime.InteropServices.BuiltInComInterop.IsSupported")]
internal static bool IsBuiltInComSupported { get; } = IsBuiltInComSupportedInternal();

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "MarshalNative_IsBuiltInComSupported")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<linker>

<type fullname="System.Collections.Generic.Comparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="false">
<method signature="System.Boolean get_SupportsGenericIComparableInterfaces()" body="stub" />
</type>
<type fullname="System.Collections.Generic.EqualityComparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="false">
<method signature="System.Boolean get_SupportsGenericIEquatableInterfaces()" body="stub" />
</type>
<type fullname="System.Collections.Generic.Comparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="true" featuredefault="true">
<method signature="System.Boolean get_SupportsGenericIComparableInterfaces()" body="stub" value="true" />
</type>
<type fullname="System.Collections.Generic.EqualityComparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="true" featuredefault="true">
<method signature="System.Boolean get_SupportsGenericIEquatableInterfaces()" body="stub" value="true" />
</type>

<type fullname="System.Reflection.Runtime.General.TypeUnifier" feature="System.Reflection.IsTypeConstructionEagerlyValidated" featurevalue="false">
<method signature="System.Boolean get_IsTypeConstructionEagerlyValidated()" body="stub" />
</type>

<type fullname="System.RuntimeType" feature="System.Reflection" featurevalue="false">
<method signature="System.Boolean get_IsReflectionDisabled()" body="stub" value="true" />
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;

Expand All @@ -12,7 +13,7 @@ namespace System.Collections.Generic
{
public abstract partial class Comparer<T> : IComparer, IComparer<T>
{
// The AOT compiler can flip this to false under certain circumstances.
[FeatureSwitchDefinition("System.Collections.Generic.DefaultComparers")]
private static bool SupportsGenericIComparableInterfaces => true;

[Intrinsic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;

Expand All @@ -12,7 +13,7 @@ namespace System.Collections.Generic
{
public abstract partial class EqualityComparer<T> : IEqualityComparer, IEqualityComparer<T>
{
// The AOT compiler can flip this to false under certain circumstances.
[FeatureSwitchDefinition("System.Collections.Generic.DefaultComparers")]
private static bool SupportsGenericIEquatableInterfaces => true;

[Intrinsic]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace System.Reflection.Metadata
{
public static class MetadataUpdater
Expand All @@ -10,6 +12,7 @@ public static void ApplyUpdate(Assembly assembly, ReadOnlySpan<byte> metadataDel
throw new PlatformNotSupportedException();
}

[FeatureSwitchDefinition("System.Reflection.Metadata.MetadataUpdater.IsSupported")]
public static bool IsSupported => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.MethodInfos;
Expand Down Expand Up @@ -36,6 +37,7 @@ namespace System.Reflection.Runtime.General
{
internal static partial class TypeUnifier
{
[FeatureSwitchDefinition("System.Reflection.IsTypeConstructionEagerlyValidated")]
// This can be replaced at native compile time using a feature switch.
internal static bool IsTypeConstructionEagerlyValidated => true;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
'$(ILEmitBackendSaveAssemblies)' == 'true'">$(DefineConstants);SAVE_ASSEMBLIES</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<Compile Remove="ServiceLookup\ILEmit\**\*.cs" />
Expand All @@ -47,6 +43,10 @@
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' or $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection.Abstractions\src\Microsoft.Extensions.DependencyInjection.Abstractions.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public sealed class ServiceProvider : IServiceProvider, IKeyedServiceProvider, I

internal ServiceProviderEngineScope Root { get; }

[FeatureSwitchDefinition("Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability")]
internal static bool VerifyOpenGenericServiceTrimmability { get; } =
AppContext.TryGetSwitch("Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability", out bool verifyOpenGenerics) ? verifyOpenGenerics : false;

[FeatureSwitchDefinition("Microsoft.Extensions.DependencyInjection.DisableDynamicEngine")]
internal static bool DisableDynamicEngine { get; } =
AppContext.TryGetSwitch("Microsoft.Extensions.DependencyInjection.DisableDynamicEngine", out bool disableDynamicEngine) ? disableDynamicEngine : false;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
</PropertyGroup>

<ItemGroup>
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup>
<Compile Include="MS\Internal\Xml\Linq\ComponentModel\XComponentModel.cs" />
<Compile Include="System\ComponentModel\ArrayConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DesigntimeLicenseContextSerializer
{
internal const byte BinaryWriterMagic = 255;

[FeatureSwitchDefinition("System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization")]
private static bool EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization { get; } = AppContext.TryGetSwitch("System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization", out bool isEnabled) ? isEnabled : false;

// Not creatable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<linker>
<assembly fullname="System.Diagnostics.DiagnosticSource" feature="System.Diagnostics.Metrics.Meter.IsSupported" featurevalue="false">
<type fullname="System.Diagnostics.Metrics.Meter">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
<type fullname="System.Diagnostics.Metrics.Instrument">
<method signature="System.Boolean get_Enabled()" body="stub" value="false" />
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' or $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' or
'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="System\Diagnostics\Activity.DateTime.corefx.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Diagnostics.Metrics
Expand All @@ -20,6 +21,7 @@ public class Meter : IDisposable

internal bool Disposed { get; private set; }

[FeatureSwitchDefinition("System.Diagnostics.Metrics.Meter.IsSupported")]
internal static bool IsSupported { get; } = InitializeIsSupported();

private static bool InitializeIsSupported() =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
</PropertyGroup>

<ItemGroup>
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Dynamic.Utils
{
internal static class DelegateHelpers
{
// This can be flipped to false using feature switches at publishing time
[FeatureSwitchDefinition("System.Linq.Expressions.CanEmitObjectArrayDelegate")]
internal static bool CanEmitObjectArrayDelegate => true;

// Separate class so that the it can be trimmed away and doesn't get conflated
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'android' or '$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'maccatalyst' or '$(TargetPlatformIdentifier)' == 'tvos' or '$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == 'wasi'">
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.mobile.xml" Condition="'$(TargetPlatformIdentifier)' != 'browser' and '$(TargetPlatformIdentifier)' != 'wasi'" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\Net\Http\ByteArrayContent.cs" />
<Compile Include="System\Net\Http\CancellationHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ private void ThrowForModifiedManagedSslOptionsIfStarted()
_socketHandler!.SslOptions = _socketHandler!.SslOptions;
}

[FeatureSwitchDefinition("System.Net.Http.UseNativeHttpHandler")]
private static bool IsNativeHandlerEnabled => RuntimeSettingParser.QueryRuntimeSettingSwitch(
"System.Net.Http.UseNativeHttpHandler",
false);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows' and '$(UseManagedNtlm)' != 'true'">
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
<Compile Include="System\Net\NegotiateAuthenticationPal.Unix.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\GssSafeHandles.cs"
Link="Common\Microsoft\Win32\SafeHandles\GssSafeHandles.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal partial class NegotiateAuthenticationPal
{
private static readonly Lazy<bool> _hasSystemNetSecurityNative = new Lazy<bool>(CheckHasSystemNetSecurityNative);
internal static bool HasSystemNetSecurityNative => _hasSystemNetSecurityNative.Value;

[FeatureSwitchDefinition("System.Net.Security.UseManagedNtlm")]
private static bool UseManagedNtlm { get; } =
AppContext.TryGetSwitch("System.Net.Security.UseManagedNtlm", out bool useManagedNtlm) ?
useManagedNtlm :
Expand Down

This file was deleted.

Loading

0 comments on commit 0fcba3f

Please sign in to comment.