Skip to content

Commit 28d35bd

Browse files
authored
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.
1 parent 8b1d06a commit 28d35bd

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public Task ConstructorDataFlow ()
118118
return RunTest ();
119119
}
120120

121+
[Fact]
122+
public Task DataflowInLocalMethodGroupArgument ()
123+
{
124+
return RunTest ();
125+
}
126+
121127
[Fact]
122128
public Task DynamicDependencyDataflow ()
123129
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)