From 1f8e3e636a06700d3a07295a737f08fc14f66437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Sun, 22 Jan 2023 10:58:15 +0900 Subject: [PATCH] Don't look at custom attributes for field constants This should not be reachable. --- .../NativeFormat/NativeFormatRuntimeFieldInfo.cs | 2 +- .../Runtime/General/NativeFormat/DefaultValueParser.cs | 8 +++++++- .../NativeFormat/NativeFormatMethodParameterInfo.cs | 3 ++- .../NativeFormat/NativeFormatRuntimePropertyInfo.cs | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/NativeFormat/NativeFormatRuntimeFieldInfo.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/NativeFormat/NativeFormatRuntimeFieldInfo.cs index b967050ebb0ce..be8721f006b25 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/NativeFormat/NativeFormatRuntimeFieldInfo.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/NativeFormat/NativeFormatRuntimeFieldInfo.cs @@ -139,7 +139,7 @@ public sealed override RuntimeFieldHandle FieldHandle protected sealed override bool GetDefaultValueIfAvailable(bool raw, out object? defaultValue) { - return DefaultValueParser.GetDefaultValueIfAny(_reader, _field.DefaultValue, FieldType, CustomAttributes, raw, out defaultValue); + return DefaultValueParser.GetDefaultValueFromConstantIfAny(_reader, _field.DefaultValue, FieldType, raw, out defaultValue); } protected sealed override FieldAccessor TryGetFieldAccessor() diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/NativeFormat/DefaultValueParser.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/NativeFormat/DefaultValueParser.cs index 24fd595d5bd09..35931ee8b540f 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/NativeFormat/DefaultValueParser.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/NativeFormat/DefaultValueParser.cs @@ -8,7 +8,7 @@ namespace System.Reflection.Runtime.General.NativeFormat { internal static class DefaultValueParser { - public static bool GetDefaultValueIfAny(MetadataReader reader, Handle constantHandle, Type declaredType, IEnumerable customAttributes, bool raw, out object? defaultValue) + public static bool GetDefaultValueFromConstantIfAny(MetadataReader reader, Handle constantHandle, Type declaredType, bool raw, out object? defaultValue) { if (!(constantHandle.IsNull(reader))) { @@ -18,6 +18,12 @@ public static bool GetDefaultValueIfAny(MetadataReader reader, Handle constantHa return true; } + defaultValue = null; + return false; + } + + public static bool GetDefaultValueFromAttributeIfAny(IEnumerable customAttributes, bool raw, out object? defaultValue) + { if (Helpers.GetCustomAttributeDefaultValueIfAny(customAttributes, raw, out defaultValue)) return true; diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/NativeFormat/NativeFormatMethodParameterInfo.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/NativeFormat/NativeFormatMethodParameterInfo.cs index 3a49e94a9bd35..e14fd67f2bf2c 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/NativeFormat/NativeFormatMethodParameterInfo.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/NativeFormat/NativeFormatMethodParameterInfo.cs @@ -66,7 +66,8 @@ public sealed override int MetadataToken protected sealed override bool GetDefaultValueIfAvailable(bool raw, out object? defaultValue) { - return DefaultValueParser.GetDefaultValueIfAny(Reader, _parameter.DefaultValue, ParameterType, CustomAttributes, raw, out defaultValue); + return DefaultValueParser.GetDefaultValueFromConstantIfAny(Reader, _parameter.DefaultValue, ParameterType, raw, out defaultValue) + || DefaultValueParser.GetDefaultValueFromAttributeIfAny(CustomAttributes, raw, out defaultValue); } private readonly MethodHandle _methodHandle; diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/NativeFormat/NativeFormatRuntimePropertyInfo.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/NativeFormat/NativeFormatRuntimePropertyInfo.cs index 1d39b94dadda9..7966647740c76 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/NativeFormat/NativeFormatRuntimePropertyInfo.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/NativeFormat/NativeFormatRuntimePropertyInfo.cs @@ -128,7 +128,8 @@ protected sealed override QSignatureTypeHandle PropertyTypeHandle protected sealed override bool GetDefaultValueIfAny(bool raw, out object? defaultValue) { - return DefaultValueParser.GetDefaultValueIfAny(_reader, _property.DefaultValue, PropertyType, CustomAttributes, raw, out defaultValue); + return DefaultValueParser.GetDefaultValueFromConstantIfAny(_reader, _property.DefaultValue, PropertyType, raw, out defaultValue) + || DefaultValueParser.GetDefaultValueFromAttributeIfAny(CustomAttributes, raw, out defaultValue); } protected sealed override RuntimeNamedMethodInfo GetPropertyMethod(PropertyMethodSemantics whichMethod)