Skip to content

Commit ed2ddf8

Browse files
[release/10.0.1xx] Source code updates from dotnet/runtime (#3713)
Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
1 parent 8bb83ad commit ed2ddf8

File tree

8 files changed

+110
-4
lines changed

8 files changed

+110
-4
lines changed

src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Reflection;
910
using System.Runtime.CompilerServices;
1011
using System.Text.Encodings.Web;
@@ -523,7 +524,7 @@ private static EnumFieldInfo[] ResolveEnumFields(JsonNamingPolicy? namingPolicy)
523524
Debug.Assert(names.Length == values.Length);
524525

525526
Dictionary<string, string>? enumMemberAttributes = null;
526-
foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
527+
foreach (FieldInfo field in GetFields())
527528
{
528529
if (field.GetCustomAttribute<JsonStringEnumMemberNameAttribute>() is { } attribute)
529530
{
@@ -561,6 +562,12 @@ private static EnumFieldInfo[] ResolveEnumFields(JsonNamingPolicy? namingPolicy)
561562
}
562563

563564
return enumFields;
565+
566+
#if !NET9_0_OR_GREATER
567+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2090:UnrecognizedReflectionPattern",
568+
Justification = "Enum fields are always preserved by trimming.")]
569+
#endif
570+
static IEnumerable<FieldInfo> GetFields() => typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
564571
}
565572

566573
private static string ResolveAndValidateJsonName(string name, JsonNamingPolicy? namingPolicy, EnumFieldNameKind kind)

src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public override JsonConverter CreateConverter(Type type, JsonSerializerOptions o
2828
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2071:UnrecognizedReflectionPattern",
2929
Justification = "'EnumConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because enumType's constructors are not annotated. " +
3030
"But EnumConverter doesn't call new T(), so this is safe.")]
31+
#if !NET9_0_OR_GREATER
32+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
33+
Justification = "'EnumConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because enumType's constructors are not annotated. " +
34+
"But EnumConverter doesn't call new T(), so this is safe.")]
35+
#endif
3136
public static JsonConverter Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy? namingPolicy, JsonSerializerOptions options)
3237
{
3338
if (!Helpers.IsSupportedTypeCode(Type.GetTypeCode(enumType)))

src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverterFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public static JsonConverter CreateValueConverter(Type valueTypeToConvert, JsonCo
4646
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2071:UnrecognizedReflectionPattern",
4747
Justification = "'NullableConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because valueTypeToConvert's constructors are not annotated. " +
4848
"But NullableConverter doesn't call new T(), so this is safe.")]
49+
#if !NET9_0_OR_GREATER
50+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
51+
Justification = "'NullableConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because valueTypeToConvert's constructors are not annotated. " +
52+
"But NullableConverter doesn't call new T(), so this is safe.")]
53+
#endif
4954
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
5055
private static Type GetNullableConverterType(Type valueTypeToConvert) => typeof(NullableConverter<>).MakeGenericType(valueTypeToConvert);
5156
}

src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionEmitMemberAccessor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, T
153153
return dynamicMethod;
154154
}
155155

156+
#if !NET9_0_OR_GREATER
157+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2046",
158+
Justification = "The type is annotated with RequiresUnreferencedCode, so the trimmer will warn at construction time.")]
159+
#endif
156160
public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TCollection>() =>
157161
CreateDelegate<Action<TCollection, object?>>(CreateAddMethodDelegate(typeof(TCollection)));
158162

src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionMemberAccessor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, T
112112
};
113113
}
114114

115+
#if !NET9_0_OR_GREATER
116+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2046",
117+
Justification = "The type is annotated with RequiresUnreferencedCode, so the trimmer will warn at construction time.")]
118+
#endif
115119
public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TCollection>()
116120
{
117121
Type collectionType = typeof(TCollection);

src/runtime/src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,18 @@ public override MultiValue GetBackingFieldTargetValue(IPropertyReferenceOperatio
256256

257257
public override MultiValue GetParameterTargetValue(IParameterSymbol parameter)
258258
{
259-
return new MethodParameterValue(new ParameterProxy(parameter, parameter.ContainingSymbol as IMethodSymbol ?? (IMethodSymbol)OwningSymbol));
259+
var parameterMethod = parameter.ContainingSymbol as IMethodSymbol ?? OwningSymbol as IMethodSymbol;
260+
if (parameterMethod is null)
261+
{
262+
// If the parameter is not associated with a method, ignore it as it's not interesting for trim analysis.
263+
// This can happen in the parameter initializer of an indexer property, for example.
264+
// When visiting the assignment for the parameter initializer, the owning symbol will be the property
265+
// symbol, not the get/set method. The get/set methods get analyzed in a separate context where the owning
266+
// symbol of the same parameter (this time on the method) is the get/set method.
267+
return TopValue;
268+
}
269+
270+
return new MethodParameterValue(new ParameterProxy(parameter, parameterMethod));
260271
}
261272

262273
public override void HandleAssignment(MultiValue source, MultiValue target, IOperation operation, in FeatureContext featureContext)

src/runtime/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/PropertyDataFlow.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public static void Main()
4848

4949
ExplicitIndexerAccess.Test();
5050
ImplicitIndexerAccess.Test();
51+
AnnotatedIndexerParameter.Test();
52+
IndexerDefaultArgument.Test();
5153

5254
AnnotationOnUnsupportedType.Test();
5355
AutoPropertyUnrecognizedField.Test();
@@ -927,6 +929,74 @@ public static void Test()
927929
}
928930
}
929931

932+
class AnnotatedIndexerParameter
933+
{
934+
public Type this[[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type index]
935+
{
936+
get
937+
{
938+
index.RequiresPublicConstructors();
939+
return null;
940+
}
941+
[ExpectedWarning("IL2067", ["this[Type].set", "index"], Tool.Analyzer, "")]
942+
[ExpectedWarning("IL2067", ["Item.set", "index"], Tool.Trimmer | Tool.NativeAot, "")]
943+
set
944+
{
945+
index.RequiresPublicMethods();
946+
}
947+
}
948+
949+
[ExpectedWarning("IL2067", ["this[Type].set", nameof(unannotated), "index"], Tool.Analyzer, "")]
950+
[ExpectedWarning("IL2067", ["Item.set", nameof(unannotated), "index"], Tool.Trimmer | Tool.NativeAot, "")]
951+
static void ParameterMismatch(Type unannotated = null)
952+
{
953+
var instance = new AnnotatedIndexerParameter();
954+
instance[unannotated] = null;
955+
}
956+
957+
static void ParameterMatch([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type annotated = null)
958+
{
959+
var instance = new AnnotatedIndexerParameter();
960+
instance[annotated] = null;
961+
}
962+
963+
public static void Test()
964+
{
965+
ParameterMismatch();
966+
ParameterMatch();
967+
}
968+
}
969+
970+
class IndexerDefaultArgument
971+
{
972+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
973+
Type this[int index = 0]
974+
{
975+
get => throw new NotImplementedException();
976+
set => throw new NotImplementedException();
977+
}
978+
979+
[ExpectedWarning("IL2072", ["this[Int32].get", nameof(DataFlowTypeExtensions.RequiresAll)], Tool.Analyzer, "")]
980+
[ExpectedWarning("IL2072", ["Item.get", nameof(DataFlowTypeExtensions.RequiresAll)], Tool.Trimmer | Tool.NativeAot, "")]
981+
static void TestRead(IndexerDefaultArgument instance = null)
982+
{
983+
instance[1].RequiresAll();
984+
}
985+
986+
[ExpectedWarning("IL2072", [nameof(GetTypeWithPublicConstructors), "this[Int32].set"], Tool.Analyzer, "")]
987+
[ExpectedWarning("IL2072", [nameof(GetTypeWithPublicConstructors), "Item.set"], Tool.Trimmer | Tool.NativeAot, "")]
988+
static void TestWrite(IndexerDefaultArgument instance = null)
989+
{
990+
instance[1] = GetTypeWithPublicConstructors();
991+
}
992+
993+
public static void Test()
994+
{
995+
TestRead();
996+
TestWrite();
997+
}
998+
}
999+
9301000
class AnnotationOnUnsupportedType
9311001
{
9321002
[ExpectedWarning("IL2099", nameof(PropertyWithUnsupportedType))]

src/source-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
"commitSha": "7f327e02b43a2486faa9cfdf0a1eae34c06e2d1a"
8080
},
8181
{
82-
"barId": 293389,
82+
"barId": 293575,
8383
"path": "runtime",
8484
"remoteUri": "https://github.com/dotnet/runtime",
85-
"commitSha": "ef887229235419a8c0f509018185f66f54709d13"
85+
"commitSha": "34d6f3ea64a58f29b965f427afd99ec1e57e859b"
8686
},
8787
{
8888
"barId": 291014,

0 commit comments

Comments
 (0)