From 4e86b1c63d9c41c6bfb6f42710be907199ce2671 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Wed, 6 Mar 2024 23:05:26 -0800 Subject: [PATCH] Fix String.IsInterned regression (#99389) Introduced by FCall to QCall conversion Fixes #99386 --- .../src/System/String.CoreCLR.cs | 3 +-- .../Common/tests/Tests/System/StringTests.cs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs index d2785251613ef..d19cb01034a74 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs @@ -23,13 +23,12 @@ public static string Intern(string str) } [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "String_IsInterned")] - [return: MarshalAs(UnmanagedType.Bool)] private static partial void IsInterned(StringHandleOnStack src); public static string? IsInterned(string str) { ArgumentNullException.ThrowIfNull(str); - Intern(new StringHandleOnStack(ref str!)); + IsInterned(new StringHandleOnStack(ref str!)); return str; } diff --git a/src/libraries/Common/tests/Tests/System/StringTests.cs b/src/libraries/Common/tests/Tests/System/StringTests.cs index 429192f549d67..f21dbe3baad6a 100644 --- a/src/libraries/Common/tests/Tests/System/StringTests.cs +++ b/src/libraries/Common/tests/Tests/System/StringTests.cs @@ -7612,7 +7612,7 @@ public static unsafe void InternTest() } [Fact] - public static void InternalTestAotSubset() + public static void InternTestAotSubset() { #pragma warning disable 0618 // suppress obsolete warning for String.Copy string emptyFromField = string.Empty; @@ -7629,6 +7629,17 @@ public static void InternalTestAotSubset() #pragma warning restore 0618 // restore warning when accessing obsolete members } + [Fact] + public static void InternTestCanReturnNull() + { + for (int i = 0; i < 20; i++) + { + if (string.IsInterned(Guid.NewGuid().ToString()) == null) + return; + } + Assert.Fail("string.IsInterned never returns null"); + } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization))] [ActiveIssue("https://github.com/dotnet/runtime/issues/34577", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public static unsafe void NormalizationTest() // basic test; more tests in globalization tests