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; + } + } +}