From 0bd0dcd01117d7de6e439a94dafbd1800c10e4ba Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 17 May 2024 16:15:03 -0700 Subject: [PATCH] Fixes --- .../Decoding/SignatureDecoderTests.cs | 4 ++++ .../System/Reflection/MethodBaseTests.cs | 6 ++++++ .../System/Reflection/MethodBodyTests.cs | 19 +++++++++++++------ .../InlineArray/InlineArrayInvalid.csproj | 2 ++ .../classloader/RefFields/Validate.csproj | 2 ++ .../InvalidOperations.csproj | 2 ++ 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs b/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs index 7570404923a7ab..bcb6661e6ef2c5 100644 --- a/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs +++ b/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs @@ -278,10 +278,14 @@ public static class PinnedAndUnpinnedLocalsToDecode public static unsafe int DoSomething() { byte[] bytes = new byte[] { 1, 2, 3 }; + Keep(ref bytes); fixed (byte* bytePtr = bytes) { return *bytePtr; } + + // Reference local variables to prevent them from being optimized out by Roslyn + static void Keep(ref T value) { }; } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBaseTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBaseTests.cs index eeda2341e251dd..9d69ce855c6a41 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBaseTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBaseTests.cs @@ -113,12 +113,18 @@ private static int MyOtherMethod(int x) public static void MyOtherMethod(object arg) { int var1 = 2; + Keep(ref var1); + string var2 = "I am a string"; + Keep(ref var2); if (arg == null) { throw new ArgumentNullException("Input arg cannot be null."); } + + // Reference local variables to prevent them from being optimized out by Roslyn + static void Keep(ref T value) { }; } #pragma warning restore xUnit1013 // Public method should be marked as test diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs index 146baf023bbfe0..017c77b397b643 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs @@ -41,10 +41,10 @@ public static void Test_MethodBody_ExceptionHandlingClause() if (ehc.Flags != ExceptionHandlingClauseOptions.Finally && ehc.Flags != ExceptionHandlingClauseOptions.Filter) { Assert.Equal(typeof(Exception), ehc.CatchType); - Assert.Equal(19, ehc.HandlerLength); - Assert.Equal(70, ehc.HandlerOffset); + Assert.Equal(26, ehc.HandlerLength); + Assert.Equal(84, ehc.HandlerOffset); Assert.Equal(61, ehc.TryLength); - Assert.Equal(9, ehc.TryOffset); + Assert.Equal(23, ehc.TryOffset); return; } } @@ -64,10 +64,10 @@ public static void Test_MethodBody_ExceptionHandlingClause() if (ehc.Flags != ExceptionHandlingClauseOptions.Finally && ehc.Flags != ExceptionHandlingClauseOptions.Filter) { Assert.Equal(typeof(Exception), ehc.CatchType); - Assert.Equal(14, ehc.HandlerLength); - Assert.Equal(58, ehc.HandlerOffset); + Assert.Equal(21, ehc.HandlerLength); + Assert.Equal(72, ehc.HandlerOffset); Assert.Equal(50, ehc.TryLength); - Assert.Equal(8, ehc.TryOffset); + Assert.Equal(22, ehc.TryOffset); return; } } @@ -79,7 +79,10 @@ public static void Test_MethodBody_ExceptionHandlingClause() private static void MethodBodyExample(object arg) { int var1 = 2; + Keep(ref var1); + string var2 = "I am a string"; + Keep(ref var2); try { @@ -94,6 +97,7 @@ private static void MethodBodyExample(object arg) } catch (Exception ex) { + Keep(ref ex); Console.WriteLine(ex.Message); } finally @@ -101,6 +105,9 @@ private static void MethodBodyExample(object arg) var1 = 3; var2 = "I am a new string!"; } + + // Reference local variables to prevent them from being optimized out by Roslyn + static void Keep(ref T value) { }; } } } diff --git a/src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.csproj b/src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.csproj index 8323d950dff730..098ebbb26a691a 100644 --- a/src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.csproj +++ b/src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.csproj @@ -3,6 +3,8 @@ true true + + false diff --git a/src/tests/Loader/classloader/RefFields/Validate.csproj b/src/tests/Loader/classloader/RefFields/Validate.csproj index f8e94ce317a33f..8d5547ca7cf099 100644 --- a/src/tests/Loader/classloader/RefFields/Validate.csproj +++ b/src/tests/Loader/classloader/RefFields/Validate.csproj @@ -4,6 +4,8 @@ true true true + + false diff --git a/src/tests/baseservices/invalid_operations/InvalidOperations.csproj b/src/tests/baseservices/invalid_operations/InvalidOperations.csproj index 5f4c03e91ae11a..1c5f1bf8001393 100644 --- a/src/tests/baseservices/invalid_operations/InvalidOperations.csproj +++ b/src/tests/baseservices/invalid_operations/InvalidOperations.csproj @@ -3,6 +3,8 @@ true true + + false