|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Mono.Linker.Tests.Cases.Expectations.Assertions; |
| 12 | +using Mono.Linker.Tests.Cases.Expectations.Helpers; |
| 13 | + |
| 14 | +namespace Mono.Linker.Tests.Cases.DataFlow |
| 15 | +{ |
| 16 | + [ExpectedNoWarnings] |
| 17 | + [SkipKeptItemsValidation] |
| 18 | + public class DataflowInLocalMethodGroupArgument |
| 19 | + { |
| 20 | + public static void Main () |
| 21 | + { |
| 22 | + new GenericWithNonPublicConstructors<int> ().Test (); |
| 23 | + GenericWithNonPublicConstructors<int>.TestInstanceLocalMethod (); |
| 24 | + } |
| 25 | + |
| 26 | + public class GenericWithNonPublicConstructors<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicConstructors)] T> |
| 27 | + { |
| 28 | + public void Test () |
| 29 | + { |
| 30 | + [ExpectedWarning ("IL2087", "PublicFields", "RequiresPublicFields", "T", "GenericWithNonPublicConstructors")] |
| 31 | + static int GetIntWithBadDataflow (int x) |
| 32 | + { |
| 33 | + typeof (T).RequiresPublicFields(); |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + new Dictionary<int, int> ().GetOrAdd (0, GetIntWithBadDataflow); |
| 38 | + } |
| 39 | + |
| 40 | + public static void TestInstanceLocalMethod () |
| 41 | + { |
| 42 | + [ExpectedWarning ("IL2087", "PublicFields", "RequiresPublicFields", "T", "GenericWithNonPublicConstructors")] |
| 43 | + int GetIntWithBadDataflow (int x) |
| 44 | + { |
| 45 | + typeof (T).RequiresPublicFields(); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + new Dictionary<int, int> ().GetOrAdd (0, GetIntWithBadDataflow); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static class DictionaryExtensions |
| 55 | + { |
| 56 | + public static TValue GetOrAdd<TKey, TValue> (this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory) |
| 57 | + { |
| 58 | + if (dictionary.TryGetValue (key, out var value)) |
| 59 | + return value; |
| 60 | + |
| 61 | + value = valueFactory (key); |
| 62 | + dictionary.TryAdd (key, value); |
| 63 | + return value; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments