From: Michal Strehovský Date: Tue, 7 Jun 2022 17:36:40 +0000 (+0900) Subject: Fix trimmability of various System.Runtime tests (#70329) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~8729 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c27d571a2443b99507df6fe39025d9d49cb8ba2c;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix trimmability of various System.Runtime tests (#70329) The reflection patterns couldn't be statically analyzed. Needed to unblock #70201. NativeAOT trims by default and CoreLib is marked trimmable. --- diff --git a/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs b/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs index d67357b9..deb55e0 100644 --- a/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs +++ b/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs @@ -17,8 +17,7 @@ namespace System.Reflection.Tests private static readonly NullabilityInfoContext nullabilityContext = new NullabilityInfoContext(); private static readonly Type testType = typeof(TypeWithNotNullContext); private static readonly Type genericType = typeof(GenericTest); - private static readonly Type stringType = typeof(string); - private static readonly BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; + private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; public static IEnumerable FieldTestData() { @@ -705,7 +704,7 @@ namespace System.Reflection.Tests [MemberData(nameof(StringTypeTestData))] public void NullablePublicOnlyStringTypeTest(string methodName, NullabilityState param1State, NullabilityState param2State, NullabilityState param3State, Type[] types) { - ParameterInfo[] parameters = stringType.GetMethod(methodName, flags, types)!.GetParameters(); + ParameterInfo[] parameters = typeof(string).GetMethod(methodName, flags, types)!.GetParameters(); NullabilityInfo param1 = nullabilityContext.Create(parameters[0]); NullabilityInfo param2 = nullabilityContext.Create(parameters[1]); NullabilityInfo param3 = nullabilityContext.Create(parameters[2]); diff --git a/src/libraries/System.Runtime/tests/System/Text/ASCIIUtilityTests.cs b/src/libraries/System.Runtime/tests/System/Text/ASCIIUtilityTests.cs index 54a8611..6636bf0 100644 --- a/src/libraries/System.Runtime/tests/System/Text/ASCIIUtilityTests.cs +++ b/src/libraries/System.Runtime/tests/System/Text/ASCIIUtilityTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Reflection; using System.Runtime.InteropServices; @@ -427,9 +428,10 @@ namespace System.Text.Tests } } + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] private static Type GetAsciiUtilityType() { - return typeof(object).Assembly.GetType("System.Text.ASCIIUtility"); + return Type.GetType("System.Text.ASCIIUtility, System.Private.CoreLib"); } private sealed class UnsafeLazyDelegate where TDelegate : Delegate diff --git a/src/libraries/System.Runtime/tests/System/Text/Latin1UtilityTests.cs b/src/libraries/System.Runtime/tests/System/Text/Latin1UtilityTests.cs index c447431..8320ab8 100644 --- a/src/libraries/System.Runtime/tests/System/Text/Latin1UtilityTests.cs +++ b/src/libraries/System.Runtime/tests/System/Text/Latin1UtilityTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Reflection; using System.Runtime.InteropServices; @@ -325,9 +326,10 @@ namespace System.Text.Tests } } + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] private static Type GetLatin1UtilityType() { - return typeof(object).Assembly.GetType("System.Text.Latin1Utility"); + return Type.GetType("System.Text.Latin1Utility, System.Private.CoreLib"); } private sealed class UnsafeLazyDelegate where TDelegate : class diff --git a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.cs b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.cs index d9c0716..9229fd3 100644 --- a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.cs +++ b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.cs @@ -248,7 +248,7 @@ namespace System.Text.Unicode.Tests { return new Lazy(() => { - Type utf16UtilityType = typeof(Utf8).Assembly.GetType("System.Text.Unicode.Utf16Utility"); + Type utf16UtilityType = Type.GetType("System.Text.Unicode.Utf16Utility, System.Private.CoreLib"); if (utf16UtilityType is null) { diff --git a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs index 3668d19..db70182 100644 --- a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs +++ b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs @@ -396,7 +396,7 @@ namespace System.Text.Unicode.Tests { return new Lazy(() => { - Type utf8UtilityType = typeof(Utf8).Assembly.GetType("System.Text.Unicode.Utf8Utility"); + Type utf8UtilityType = Type.GetType("System.Text.Unicode.Utf8Utility, System.Private.CoreLib"); if (utf8UtilityType is null) {