Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ReflectionAccessFromCompilerGeneratedCode.cs « RequiresCapability « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14fd917fb68f3908690226f1fc8c8bc4a8b8ef17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;

namespace Mono.Linker.Tests.Cases.RequiresCapability
{
	[SkipKeptItemsValidation]
	[ExpectedNoWarnings]
	public class ReflectionAccessFromCompilerGeneratedCode
	{
		public static void Main ()
		{
			ReflectionAccessFromStateMachine.Test ();
			ReflectionAccessFromLocalFunction.Test ();
			ReflectionAccessFromLambda.Test ();
		}

		class ReflectionAccessFromStateMachine
		{
			[ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", CompilerGeneratedCode = true,
				ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2026", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true,
				ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction", CompilerGeneratedCode = true,
					ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations), CompilerGeneratedCode = true,
					ProducedBy = ProducedBy.Trimmer)]
			static IEnumerable<int> TestIterator ()
			{
				typeof (TypeWithMethodWithRequires).RequiresAll ();
				yield return 0;
			}

			[RequiresUnreferencedCode ("--TestIteratorWithRUC--")]
			static IEnumerable<int> TestIteratorWithRUC ()
			{
				typeof (TypeWithMethodWithRequires).RequiresAll ();
				yield return 0;
			}

			[ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", CompilerGeneratedCode = true,
				ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2026", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true,
				ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction", CompilerGeneratedCode = true,
					ProducedBy = ProducedBy.Trimmer)]
			[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations), CompilerGeneratedCode = true,
					ProducedBy = ProducedBy.Trimmer)]
			static async void TestAsync ()
			{
				typeof (TypeWithMethodWithRequires).RequiresAll ();
				await MethodAsync ();
			}

			[RequiresUnreferencedCode ("--TestAsyncWithRUC--")]
			static async void TestAsyncWithRUC ()
			{
				typeof (TypeWithMethodWithRequires).RequiresAll ();
				await MethodAsync ();
			}

			[ExpectedWarning ("IL2026", "--TestIteratorWithRUC--")]
			[ExpectedWarning ("IL2026", "--TestAsyncWithRUC--")]
			public static void Test ()
			{
				TestIterator ();
				TestIteratorWithRUC ();
				TestAsync ();
				TestAsyncWithRUC ();
			}
		}

		class ReflectionAccessFromLocalFunction
		{
			static void TestLocalFunction ()
			{
				[ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2026", "--MethodWithLocalFunctionWithRUC.LocalFunction--",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations),
					ProducedBy = ProducedBy.Trimmer)]
				void LocalFunction ()
				{
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				}
				LocalFunction ();
			}

			[ExpectedWarning ("IL2026", "--LocalFunction--")]
			static void TestLocalFunctionWithRUC ()
			{
				[RequiresUnreferencedCode ("--LocalFunction--")]
				void LocalFunction ()
				{
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				}
				LocalFunction ();
			}

			[RequiresUnreferencedCode ("--TestLocalFunctionInMethodWithRUC--")]
			static void TestLocalFunctionInMethodWithRUC ()
			{
				void LocalFunction ()
				{
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				}
				LocalFunction ();
			}

			[ExpectedWarning ("IL2026", "--TestLocalFunctionInMethodWithRUC--")]
			public static void Test ()
			{
				TestLocalFunction ();
				TestLocalFunctionWithRUC ();
				TestLocalFunctionInMethodWithRUC ();
			}
		}

		class ReflectionAccessFromLambda
		{
			static void TestLambda ()
			{
				var lambda =
				[ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2026", "--MethodWithLocalFunctionWithRUC.LocalFunction--",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction",
					ProducedBy = ProducedBy.Trimmer)]
				[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations),
					ProducedBy = ProducedBy.Trimmer)]
				() => {
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				};
				lambda ();
			}

			[ExpectedWarning ("IL2026", "--TestLambdaInMethodWithRUC--")]
			static void TestLambdaWithRUC ()
			{
				var lambda =
				[RequiresUnreferencedCode ("--TestLambdaInMethodWithRUC--")]
				() => {
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				};
				lambda ();
			}

			[RequiresUnreferencedCode ("--TestLambdaInMethodWithRUC--")]
			static void TestLambdaInMethodWithRUC ()
			{
				var lambda =
				() => {
					typeof (TypeWithMethodWithRequires).RequiresAll ();
				};
				lambda ();
			}

			[ExpectedWarning ("IL2026", "--TestLambdaInMethodWithRUC--")]
			public static void Test ()
			{
				TestLambda ();
				TestLambdaWithRUC ();
				TestLambdaInMethodWithRUC ();
			}
		}

		static async Task<int> MethodAsync ()
		{
			return await Task.FromResult (0);
		}

		class TypeWithMethodWithRequires
		{
			[RequiresUnreferencedCode ("--TypeWithMethodWithRequires.MethodWithRequires--")]
			[RequiresAssemblyFiles ("--TypeWithMethodWithRequires.MethodWithRequires--")]
			[RequiresDynamicCode ("--TypeWithMethodWithRequires.MethodWithRequires--")]
			public static void MethodWithRequires ()
			{
			}

			public static void MethodWithAnnotations ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type t) { }

			[ExpectedWarning ("IL2026", "--MethodWithLocalFunctionWithRUC.LocalFunction--")]
			public static void MethodWithLocalFunctionWithRUC ()
			{
				[RequiresUnreferencedCode ("--MethodWithLocalFunctionWithRUC.LocalFunction--")]
				void LocalFunction ()
				{ }
				LocalFunction ();
			}

			public static void MethodWithLocalFunctionCallsRUC ()
			{
				[ExpectedWarning ("IL2026", "--MethodWithRUC--")]
				void LocalFunction () => MethodWithRUC ();
				LocalFunction ();
			}
		}

		[RequiresUnreferencedCode ("--MethodWithRUC--")]
		static void MethodWithRUC () { }
	}
}