Skip to content

Commit eb864bf

Browse files
sbomerAustinWise
andauthored
Use attributes for IsDynamicCodeSupported feature switch (#99641)
* Use attributes for IsDynamicCodeSupported feature switch * Update src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/TypeUtils.cs Co-authored-by: Austin Wise <[email protected]> --------- Co-authored-by: Austin Wise <[email protected]>
1 parent 3d7ebcf commit eb864bf

File tree

13 files changed

+13
-42
lines changed

13 files changed

+13
-42
lines changed

src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
<assembly fullname="System.Private.CoreLib">
33
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="true">
44
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="true" />
5-
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="true" />
6-
</type>
7-
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
8-
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
9-
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
105
</type>
116
</assembly>
127
</linker>

src/coreclr/nativeaot/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
<linker>
2-
<type fullname="System.Runtime.CompilerServices.RuntimeFeature">
3-
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
4-
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
5-
</type>
62

73
<type fullname="System.Collections.Generic.Comparer`1" feature="System.Collections.Generic.DefaultComparers" featurevalue="false">
84
<method signature="System.Boolean get_SupportsGenericIComparableInterfaces()" body="stub" />
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace System.Runtime.CompilerServices
57
{
68
public static partial class RuntimeFeature
79
{
10+
[FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
811
public static bool IsDynamicCodeSupported => false;
12+
13+
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
914
public static bool IsDynamicCodeCompiled => false;
1015
}
1116
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<linker>
22
<assembly fullname="System.Linq.Expressions">
3-
<type fullname="System.Linq.Expressions.LambdaExpression">
4-
<method signature="System.Boolean get_CanCompileToIL()" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false" body="stub" value="false" />
5-
</type>
63
<type fullname="System.Dynamic.Utils.DelegateHelpers">
74
<method signature="System.Boolean get_CanEmitObjectArrayDelegate()" feature="System.Linq.Expressions.CanEmitObjectArrayDelegate" featurevalue="false" body="stub" value="false" />
85
</type>
9-
<type fullname="System.Linq.Expressions.Interpreter.CallInstruction">
10-
<method signature="System.Boolean get_CanCreateArbitraryDelegates()" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false" body="stub" value="false" />
11-
</type>
126
</assembly>
137
</linker>

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

-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public static Type LiftPrimitiveOrThrow(this Type type)
4141
{
4242
if (RuntimeFeature.IsDynamicCodeSupported)
4343
{
44-
#pragma warning disable IL3050
45-
// Analyzer doesn't yet understand feature switches
4644
return GetNullableType(type);
47-
#pragma warning restore IL3050
4845
}
4946
if (!type.IsValueType || IsNullableType(type))
5047
{

src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/DelegateHelpers.cs

-3
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,10 @@ private static System.Reflection.TypeInfo MakeNewCustomDelegate(Type[] types)
122122
const MethodImplAttributes implAttributes = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
123123
const MethodAttributes invokeAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
124124

125-
#pragma warning disable IL3050
126-
// Suppress analyzer warnings since they don't currently support feature flags
127125
TypeBuilder builder = AssemblyGen.DefineDelegateType("Delegate" + types.Length);
128126
builder.DefineConstructor(ctorAttributes, CallingConventions.Standard, delegateCtorSignature).SetImplementationFlags(implAttributes);
129127
builder.DefineMethod("Invoke", invokeAttributes, returnType, parameters).SetImplementationFlags(implAttributes);
130128
return builder.CreateTypeInfo();
131-
#pragma warning restore IL3050
132129
}
133130
else
134131
{

src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/CallInstruction.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal abstract partial class CallInstruction : Instruction
1616
/// </summary>
1717
public abstract int ArgumentCount { get; }
1818

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

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

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

117116
return res;
118-
#pragma warning restore IL3050
119117
}
120118

121119
private static CallInstruction GetArrayAccessor(MethodInfo info, int argumentCount)

src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/LambdaExpression.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class LambdaExpression : Expression, IParameterProvider
2525

2626
private readonly Expression _body;
2727

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

3131
// This could be flipped to false using feature switches at publishing time
@@ -138,10 +138,7 @@ public Delegate Compile()
138138
{
139139
if (CanCompileToIL)
140140
{
141-
#pragma warning disable IL3050
142-
// Analyzer doesn't yet understand feature switches
143141
return Compiler.LambdaCompiler.Compile(this);
144-
#pragma warning restore IL3050
145142
}
146143
else
147144
{
@@ -221,10 +218,7 @@ internal Expression(Expression body)
221218
{
222219
if (CanCompileToIL)
223220
{
224-
#pragma warning disable IL3050
225-
// Analyzer doesn't yet understand feature switches
226221
return (TDelegate)(object)Compiler.LambdaCompiler.Compile(this);
227-
#pragma warning restore IL3050
228222
}
229223
else
230224
{
@@ -629,10 +623,7 @@ internal static LambdaExpression CreateLambda(Type delegateType, Expression body
629623
MethodInfo create;
630624
if (LambdaExpression.CanCompileToIL)
631625
{
632-
#pragma warning disable IL3050
633-
// Analyzer doesn't yet understand feature switches
634626
create = typeof(Expression<>).MakeGenericType(delegateType).GetMethod("Create", BindingFlags.Static | BindingFlags.NonPublic)!;
635-
#pragma warning restore IL3050
636627
}
637628
else
638629
{

src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSite.cs

-3
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ internal T MakeUpdateDelegate()
289289
if (System.Linq.Expressions.LambdaExpression.CanCompileToIL
290290
&& target.IsGenericType && IsSimpleSignature(invoke, out Type[] args))
291291
{
292-
#pragma warning disable IL3050
293-
// Analyzer doesn't yet understand feature switches
294292
return MakeUpdateDelegateWhenCanCompileToIL();
295-
#pragma warning restore IL3050
296293
}
297294

298295
s_cachedNoMatch = CreateCustomNoMatchDelegate(invoke);

src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.NonNativeAot.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace System.Runtime.CompilerServices
57
{
68
public static partial class RuntimeFeature
79
{
10+
[FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
811
public static bool IsDynamicCodeSupported
912
{
1013
#if MONO
@@ -13,6 +16,7 @@ public static bool IsDynamicCodeSupported
1316
get;
1417
} = AppContext.TryGetSwitch("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", out bool isDynamicCodeSupported) ? isDynamicCodeSupported : true;
1518

19+
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
1620
public static bool IsDynamicCodeCompiled
1721
{
1822
#if MONO

src/libraries/System.Runtime/ref/System.Runtime.cs

+2
Original file line numberDiff line numberDiff line change
@@ -13138,7 +13138,9 @@ public static partial class RuntimeFeature
1313813138
public const string PortablePdb = "PortablePdb";
1313913139
public const string UnmanagedSignatureCallingConvention = "UnmanagedSignatureCallingConvention";
1314013140
public const string VirtualStaticsInInterfaces = "VirtualStaticsInInterfaces";
13141+
[System.Diagnostics.CodeAnalysis.FeatureGuard(typeof(System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute))]
1314113142
public static bool IsDynamicCodeCompiled { get { throw null; } }
13143+
[System.Diagnostics.CodeAnalysis.FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported")]
1314213144
public static bool IsDynamicCodeSupported { get { throw null; } }
1314313145
public static bool IsSupported(string feature) { throw null; }
1314413146
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptionsUpdateHandler.cs

-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public static void ClearCache(Type[]? types)
2828
if (RuntimeFeature.IsDynamicCodeSupported)
2929
{
3030
// Flush the dynamic method cache
31-
#pragma warning disable IL3050 // The analyzer doesn't understand runtime feature conditions: https://github.com/dotnet/linker/issues/2715
3231
ReflectionEmitCachingMemberAccessor.Clear();
33-
#pragma warning restore IL3050
3432
}
3533
}
3634
}

src/mono/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.iOS.xml

-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
<type fullname="System.Runtime.CompilerServices.RuntimeFeature">
44
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
55
</type>
6-
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
7-
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
8-
</type>
96
</assembly>
107
</linker>

0 commit comments

Comments
 (0)