Skip to content

Commit 715e697

Browse files
authored
Removing RUC from Default and Ambient attributes (#100821)
* Removing RUC from Default and Ambient attributes * FB * FB2
1 parent 8c5bc36 commit 715e697

File tree

9 files changed

+31
-7
lines changed

9 files changed

+31
-7
lines changed

docs/workflow/trimming/feature-switches.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ configurations but their defaults might vary as any SDK can set the defaults dif
3636
| EnableGeneratedComInterfaceComImportInterop | System.Runtime.InteropServices.Marshalling.EnableGeneratedComInterfaceComImportInterop | When set to true, enables casting source-generated COM object wrappers to built-in COM-based COM interfaces. |
3737
| _UseManagedNtlm | System.Net.Security.UseManagedNtlm | When set to true, uses built-in managed implementation of NTLM and SPNEGO algorithm for HTTP, SMTP authentication, and NegotiateAuthentication API instead of system provided GSSAPI implementation. |
3838
| _ComObjectDescriptorSupport | System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported | When set to true, supports creating a TypeDescriptor based view of COM objects. |
39+
| _DesignerHostSupport | System.ComponentModel.Design.IDesignerHost.IsSupported | When set to true, supports creating design components at runtime. |
40+
| _DefaultValueAttributeSupport | System.ComponentModel.DefaultValueAttribute.IsSupported | When set to true, supports creating a DefaultValueAttribute at runtime. |
3941

4042
Any feature-switch which defines property can be set in csproj file or
4143
on the command line as any other MSBuild property. Those without predefined property name

src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public AmbientValueAttribute(long value) { }
3838
public AmbientValueAttribute(object? value) { }
3939
public AmbientValueAttribute(float value) { }
4040
public AmbientValueAttribute(string? value) { }
41-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")]
4241
public AmbientValueAttribute([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type type, string value) { }
4342
public object? Value { get { throw null; } }
4443
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }

src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.LibraryBuild.xml

+7
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@
2222
<property name="Target">M:System.ComponentModel.TypeDescriptor.NodeFor(System.Object,System.Boolean)</property>
2323
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported=true.</property>
2424
</attribute>
25+
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
26+
<argument>ILLink</argument>
27+
<argument>IL2026</argument>
28+
<property name="Scope">member</property>
29+
<property name="Target">M:System.ComponentModel.AmbientValueAttribute.#ctor(System.Type,System.String)</property>
30+
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.ComponentModel.Design.IDesignerHost.IsSupported=true.</property>
31+
</attribute>
2532
</assembly>
2633
</linker>

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,23 @@ public sealed class AmbientValueAttribute : Attribute
2424
/// specified value to the specified type, and using the U.S. English culture as the
2525
/// translation context.
2626
/// </summary>
27-
[RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)]
2827
public AmbientValueAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, string value)
2928
{
3029
// The try/catch here is because attributes should never throw exceptions. We would fail to
3130
// load an otherwise normal class.
3231

32+
Debug.Assert(IDesignerHost.IsSupported, "Runtime instantiation of this attribute is not allowed with trimming.");
3333
if (!IDesignerHost.IsSupported)
3434
{
3535
return;
3636
}
3737

3838
try
3939
{
40-
_value = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value);
40+
_value = TypeDescriptorGetConverter(type).ConvertFromInvariantString(value);
41+
42+
[RequiresUnreferencedCode("AmbientValueAttribute usage of TypeConverter is not compatible with trimming.")]
43+
static TypeConverter TypeDescriptorGetConverter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type) => TypeDescriptor.GetConverter(type);
4144
}
4245
catch
4346
{

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/IDesignerHost.cs

+3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ namespace System.ComponentModel.Design
1212
public interface IDesignerHost : IServiceContainer
1313
{
1414
[FeatureSwitchDefinition("System.ComponentModel.Design.IDesignerHost.IsSupported")]
15+
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
16+
#pragma warning disable IL4000
1517
internal static bool IsSupported => AppContext.TryGetSwitch("System.ComponentModel.Design.IDesignerHost.IsSupported", out bool isSupported) ? isSupported : true;
18+
#pragma warning restore IL4000
1619

1720
/// <summary>
1821
/// Gets or sets a value indicating whether the designer host

src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml

+7
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@
2222
<property name="Target">M:System.StartupHookProvider.ProcessStartupHooks(System.String)</property>
2323
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true.</property>
2424
</attribute>
25+
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
26+
<argument>ILLink</argument>
27+
<argument>IL2026</argument>
28+
<property name="Scope">member</property>
29+
<property name="Target">M:System.ComponentModel.DefaultValueAttribute.#ctor(System.Type,System.String)</property>
30+
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.ComponentModel.DefaultValueAttribute.IsSupported=true.</property>
31+
</attribute>
2532
</assembly>
2633
</linker>

src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@ public class DefaultValueAttribute : Attribute
2424
private static object? s_convertFromInvariantString;
2525

2626
[FeatureSwitchDefinition("System.ComponentModel.DefaultValueAttribute.IsSupported")]
27+
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
28+
#pragma warning disable IL4000
2729
internal static bool IsSupported => AppContext.TryGetSwitch("System.ComponentModel.DefaultValueAttribute.IsSupported", out bool isSupported) ? isSupported : true;
30+
#pragma warning restore IL4000
2831

2932
/// <summary>
3033
/// Initializes a new instance of the <see cref='DefaultValueAttribute'/>
3134
/// class, converting the specified value to the specified type, and using the U.S. English
3235
/// culture as the translation context.
3336
/// </summary>
34-
[RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")]
3537
public DefaultValueAttribute(
3638
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type,
3739
string? value)
3840
{
3941
// The null check and try/catch here are because attributes should never throw exceptions.
4042
// We would fail to load an otherwise normal class.
4143

44+
Debug.Assert(IsSupported, "Runtime instantiation of this attribute is not allowed with trimming.");
4245
if (!IsSupported)
4346
{
44-
Debug.Assert(!IsSupported, "Runtime instantiation of this attribute is not allowed.");
4547
return;
4648
}
4749

@@ -69,7 +71,7 @@ public DefaultValueAttribute(
6971
_value = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
7072
}
7173

72-
[RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")]
74+
[RequiresUnreferencedCode("DefaultValueAttribute usage of TypeConverter is not compatible with trimming.")]
7375
// Looking for ad hoc created TypeDescriptor.ConvertFromInvariantString(Type, string)
7476
static bool TryConvertFromInvariantString(
7577
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type typeToConvert,

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

-1
Original file line numberDiff line numberDiff line change
@@ -8047,7 +8047,6 @@ public DefaultValueAttribute(object? value) { }
80478047
public DefaultValueAttribute(sbyte value) { }
80488048
public DefaultValueAttribute(float value) { }
80498049
public DefaultValueAttribute(string? value) { }
8050-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")]
80518050
public DefaultValueAttribute([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type type, string? value) { }
80528051
[System.CLSCompliantAttribute(false)]
80538052
public DefaultValueAttribute(ushort value) { }

src/tools/illink/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Copyright (c) .NET Foundation. All rights reserved.
5353
<!-- Trim managed NTLM on Linux when it's not explicitly requested -->
5454
<_UseManagedNtlm Condition="'$(_UseManagedNtlm)' == '' and $(RuntimeIdentifier.StartsWith('linux'))">false</_UseManagedNtlm>
5555
<_ComObjectDescriptorSupport Condition="'$(_ComObjectDescriptorSupport)' == ''">false</_ComObjectDescriptorSupport>
56+
<_DesignerHostSupport Condition="'$(_DesignerHostSupport)' == ''">false</_DesignerHostSupport>
57+
<_DefaultValueAttributeSupport Condition="'$(_DefaultValueAttributeSupport)' == ''">false</_DefaultValueAttributeSupport>
5658
</PropertyGroup>
5759

5860

0 commit comments

Comments
 (0)