From 28d35bddd97fd4bd1b2c20ff629f6d2a240cf362 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Tue, 28 May 2024 18:52:41 -0700 Subject: [PATCH] ILLink: Add test for dataflow in local method group passed as argument to delegate Adds a test for issue #95258 which passes without any source code fix. --- .../DataFlowTests.cs | 6 ++ .../DataflowInLocalMethodGroupArgument.cs | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/DataflowInLocalMethodGroupArgument.cs diff --git a/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs b/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs index c59afbfd8fd5f..167794b4fee4b 100644 --- a/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs +++ b/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs @@ -118,6 +118,12 @@ public Task ConstructorDataFlow () return RunTest (); } + [Fact] + public Task DataflowInLocalMethodGroupArgument () + { + return RunTest (); + } + [Fact] public Task DynamicDependencyDataflow () { diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/DataflowInLocalMethodGroupArgument.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/DataflowInLocalMethodGroupArgument.cs new file mode 100644 index 0000000000000..3ecffa38f953e --- /dev/null +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/DataflowInLocalMethodGroupArgument.cs @@ -0,0 +1,66 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; + +namespace Mono.Linker.Tests.Cases.DataFlow +{ + [ExpectedNoWarnings] + [SkipKeptItemsValidation] + public class DataflowInLocalMethodGroupArgument + { + public static void Main () + { + new GenericWithNonPublicConstructors ().Test (); + GenericWithNonPublicConstructors.TestInstanceLocalMethod (); + } + + public class GenericWithNonPublicConstructors<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicConstructors)] T> + { + public void Test () + { + [ExpectedWarning ("IL2087", "PublicFields", "RequiresPublicFields", "T", "GenericWithNonPublicConstructors")] + static int GetIntWithBadDataflow (int x) + { + typeof (T).RequiresPublicFields(); + return 0; + } + + new Dictionary ().GetOrAdd (0, GetIntWithBadDataflow); + } + + public static void TestInstanceLocalMethod () + { + [ExpectedWarning ("IL2087", "PublicFields", "RequiresPublicFields", "T", "GenericWithNonPublicConstructors")] + int GetIntWithBadDataflow (int x) + { + typeof (T).RequiresPublicFields(); + return 0; + } + + new Dictionary ().GetOrAdd (0, GetIntWithBadDataflow); + } + } + } + + public static class DictionaryExtensions + { + public static TValue GetOrAdd (this Dictionary dictionary, TKey key, Func valueFactory) + { + if (dictionary.TryGetValue (key, out var value)) + return value; + + value = valueFactory (key); + dictionary.TryAdd (key, value); + return value; + } + } +}