Skip to content

Commit

Permalink
Use attributes for IsDynamicCodeSupported feature switch (#99641)
Browse files Browse the repository at this point in the history
* Use attributes for IsDynamicCodeSupported feature switch

* Update src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/TypeUtils.cs

Co-authored-by: Austin Wise <AustinWise@gmail.com>

---------

Co-authored-by: Austin Wise <AustinWise@gmail.com>
  • Loading branch information
sbomer and AustinWise authored Mar 13, 2024
1 parent 3d7ebcf commit eb864bf
Show file tree
Hide file tree
Showing 13 changed files with 13 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
<assembly fullname="System.Private.CoreLib">
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="true">
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="true" />
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="true" />
</type>
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<linker>
<type fullname="System.Runtime.CompilerServices.RuntimeFeature">
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
</type>

<type fullname="System.Collections.Generic.Comparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="false">
<method signature="System.Boolean get_SupportsGenericIComparableInterfaces()" body="stub" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// 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.Runtime.CompilerServices
{
public static partial class RuntimeFeature
{
[FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
public static bool IsDynamicCodeSupported => false;

[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
public static bool IsDynamicCodeCompiled => false;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<linker>
<assembly fullname="System.Linq.Expressions">
<type fullname="System.Linq.Expressions.LambdaExpression">
<method signature="System.Boolean get_CanCompileToIL()" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false" body="stub" value="false" />
</type>
<type fullname="System.Dynamic.Utils.DelegateHelpers">
<method signature="System.Boolean get_CanEmitObjectArrayDelegate()" feature="System.Linq.Expressions.CanEmitObjectArrayDelegate" featurevalue="false" body="stub" value="false" />
</type>
<type fullname="System.Linq.Expressions.Interpreter.CallInstruction">
<method signature="System.Boolean get_CanCreateArbitraryDelegates()" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false" body="stub" value="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public static Type LiftPrimitiveOrThrow(this Type type)
{
if (RuntimeFeature.IsDynamicCodeSupported)
{
#pragma warning disable IL3050
// Analyzer doesn't yet understand feature switches
return GetNullableType(type);
#pragma warning restore IL3050
}
if (!type.IsValueType || IsNullableType(type))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ private static System.Reflection.TypeInfo MakeNewCustomDelegate(Type[] types)
const MethodImplAttributes implAttributes = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
const MethodAttributes invokeAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;

#pragma warning disable IL3050
// Suppress analyzer warnings since they don't currently support feature flags
TypeBuilder builder = AssemblyGen.DefineDelegateType("Delegate" + types.Length);
builder.DefineConstructor(ctorAttributes, CallingConventions.Standard, delegateCtorSignature).SetImplementationFlags(implAttributes);
builder.DefineMethod("Invoke", invokeAttributes, returnType, parameters).SetImplementationFlags(implAttributes);
return builder.CreateTypeInfo();
#pragma warning restore IL3050
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal abstract partial class CallInstruction : Instruction
/// </summary>
public abstract int ArgumentCount { get; }

[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
private static bool CanCreateArbitraryDelegates => RuntimeFeature.IsDynamicCodeSupported;

#region Construction
Expand Down Expand Up @@ -50,8 +51,6 @@ public static CallInstruction Create(MethodInfo info, ParameterInfo[] parameters
if (!CanCreateArbitraryDelegates)
return new MethodInfoCallInstruction(info, argumentCount);

// This code should be unreachable in AOT. The analyzer currently doesn't understand feature switches
#pragma warning disable IL3050
if (!info.IsStatic && info.DeclaringType!.IsValueType)
{
return new MethodInfoCallInstruction(info, argumentCount);
Expand Down Expand Up @@ -115,7 +114,6 @@ public static CallInstruction Create(MethodInfo info, ParameterInfo[] parameters
s_cache[info] = res;

return res;
#pragma warning restore IL3050
}

private static CallInstruction GetArrayAccessor(MethodInfo info, int argumentCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class LambdaExpression : Expression, IParameterProvider

private readonly Expression _body;

// This can be flipped to false using feature switches at publishing time
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
public static bool CanCompileToIL => RuntimeFeature.IsDynamicCodeSupported;

// This could be flipped to false using feature switches at publishing time
Expand Down Expand Up @@ -138,10 +138,7 @@ public Delegate Compile()
{
if (CanCompileToIL)
{
#pragma warning disable IL3050
// Analyzer doesn't yet understand feature switches
return Compiler.LambdaCompiler.Compile(this);
#pragma warning restore IL3050
}
else
{
Expand Down Expand Up @@ -221,10 +218,7 @@ internal Expression(Expression body)
{
if (CanCompileToIL)
{
#pragma warning disable IL3050
// Analyzer doesn't yet understand feature switches
return (TDelegate)(object)Compiler.LambdaCompiler.Compile(this);
#pragma warning restore IL3050
}
else
{
Expand Down Expand Up @@ -629,10 +623,7 @@ internal static LambdaExpression CreateLambda(Type delegateType, Expression body
MethodInfo create;
if (LambdaExpression.CanCompileToIL)
{
#pragma warning disable IL3050
// Analyzer doesn't yet understand feature switches
create = typeof(Expression<>).MakeGenericType(delegateType).GetMethod("Create", BindingFlags.Static | BindingFlags.NonPublic)!;
#pragma warning restore IL3050
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ internal T MakeUpdateDelegate()
if (System.Linq.Expressions.LambdaExpression.CanCompileToIL
&& target.IsGenericType && IsSimpleSignature(invoke, out Type[] args))
{
#pragma warning disable IL3050
// Analyzer doesn't yet understand feature switches
return MakeUpdateDelegateWhenCanCompileToIL();
#pragma warning restore IL3050
}

s_cachedNoMatch = CreateCustomNoMatchDelegate(invoke);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// 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.Runtime.CompilerServices
{
public static partial class RuntimeFeature
{
[FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
public static bool IsDynamicCodeSupported
{
#if MONO
Expand All @@ -13,6 +16,7 @@ public static bool IsDynamicCodeSupported
get;
} = AppContext.TryGetSwitch("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", out bool isDynamicCodeSupported) ? isDynamicCodeSupported : true;

[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
public static bool IsDynamicCodeCompiled
{
#if MONO
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13138,7 +13138,9 @@ public static partial class RuntimeFeature
public const string PortablePdb = "PortablePdb";
public const string UnmanagedSignatureCallingConvention = "UnmanagedSignatureCallingConvention";
public const string VirtualStaticsInInterfaces = "VirtualStaticsInInterfaces";
[System.Diagnostics.CodeAnalysis.FeatureGuard(typeof(System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute))]
public static bool IsDynamicCodeCompiled { get { throw null; } }
[System.Diagnostics.CodeAnalysis.FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
public static bool IsDynamicCodeSupported { get { throw null; } }
public static bool IsSupported(string feature) { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public static void ClearCache(Type[]? types)
if (RuntimeFeature.IsDynamicCodeSupported)
{
// Flush the dynamic method cache
#pragma warning disable IL3050 // The analyzer doesn't understand runtime feature conditions: https://github.com/dotnet/linker/issues/2715
ReflectionEmitCachingMemberAccessor.Clear();
#pragma warning restore IL3050
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
<type fullname="System.Runtime.CompilerServices.RuntimeFeature">
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
</type>
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
</type>
</assembly>
</linker>

0 comments on commit eb864bf

Please sign in to comment.