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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
// 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.Diagnostics.CodeAnalysis;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
namespace Mono.Linker.Tests.Cases.DataFlow
{
[ExpectedNoWarnings]
[SkipKeptItemsValidation]
public class ArrayDataFlow
{
public static void Main ()
{
TestArrayWithInitializerOneElementStaticType ();
TestArrayWithInitializerOneElementParameter (typeof (TestType));
TestArrayWithInitializerMultipleElementsStaticType ();
TestArrayWithInitializerMultipleElementsMix<TestType> (typeof (TestType));
TestArraySetElementOneElementStaticType ();
TestArraySetElementOneElementMix ();
TestArraySetElementOneElementMerged ();
TestArraySetElementOneElementParameter (typeof (TestType));
TestArraySetElementMultipleElementsStaticType ();
TestMergedArrayElement (1);
TestArraySetElementMultipleElementsMix<TestType> (typeof (TestType));
TestArraySetElementAndInitializerMultipleElementsMix<TestType> (typeof (TestType));
TestGetElementAtUnknownIndex ();
TestMergedArrayElementWithUnknownIndex (0);
// Array reset - certain operations on array are not tracked fully (or impossible due to unknown inputs)
// and sometimes the only valid thing to do is to reset the array to all unknowns as it's impossible
// to determine what the operation did to the array. So after the reset, everything in the array
// should be treated as unknown value.
TestArrayResetStoreUnknownIndex ();
TestArrayResetGetElementOnByRefArray ();
TestArrayResetAfterCall ();
TestArrayResetAfterAssignment ();
TestMultiDimensionalArray.Test ();
WriteCapturedArrayElement.Test ();
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArrayWithInitializerOneElementStaticType ()
{
Type[] arr = new Type[] { typeof (TestType) };
arr[0].RequiresAll ();
arr[1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArrayWithInitializerOneElementParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
Type[] arr = new Type[] { type };
arr[0].RequiresAll ();
arr[1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArrayWithInitializerMultipleElementsStaticType ()
{
Type[] arr = new Type[] { typeof (TestType), typeof (TestType), typeof (TestType) };
arr[0].RequiresAll ();
arr[1].RequiresAll ();
arr[2].RequiresAll ();
arr[3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArrayWithInitializerMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[] arr = new Type[] { typeof (TestType), typeof (TProperties), typeAll };
arr[0].RequiresAll ();
arr[1].RequiresPublicProperties ();
arr[1].RequiresPublicFields (); // Should warn
arr[2].RequiresAll ();
arr[3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArraySetElementOneElementStaticType ()
{
Type[] arr = new Type[1];
arr[0] = typeof (TestType);
arr[0].RequiresAll ();
arr[1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2072", nameof (GetUnknownType), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2072", nameof (GetTypeWithPublicConstructors), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Trimmer)]
// https://github.com/dotnet/linker/issues/2736
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
static void TestArraySetElementOneElementMix ()
{
Type[] arr = new Type[1];
if (string.Empty.Length == 0)
arr[0] = GetUnknownType ();
else
arr[0] = GetTypeWithPublicConstructors ();
arr[0].RequiresAll ();
}
[ExpectedWarning ("IL2072", nameof (GetUnknownType), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2072", nameof (GetTypeWithPublicConstructors), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
// https://github.com/dotnet/linker/issues/2737
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementOneElementMerged ()
{
Type[] arr = new Type[1];
arr[0] = string.Empty.Length == 0 ? GetUnknownType () : GetTypeWithPublicConstructors ();
arr[0].RequiresAll ();
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArraySetElementOneElementParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
Type[] arr = new Type[1];
arr[0] = type;
arr[0].RequiresAll ();
arr[1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArraySetElementMultipleElementsStaticType ()
{
Type[] arr = new Type[3];
arr[0] = typeof (TestType);
arr[1] = typeof (TestType);
arr[2] = typeof (TestType);
arr[0].RequiresAll ();
arr[1].RequiresAll ();
arr[2].RequiresAll ();
arr[3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2072", nameof (ArrayDataFlow.GetMethods))]
[ExpectedWarning ("IL2072", nameof (ArrayDataFlow.GetFields))]
static void TestMergedArrayElement (int i)
{
Type[] arr = new Type[] { null };
if (i == 1)
arr[0] = GetMethods ();
else
arr[0] = GetFields ();
arr[0].RequiresAll (); // Should warn - Methods/Fields does not have match annotations with All.
}
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type GetMethods () => null;
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
static Type GetFields () => null;
[ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArraySetElementMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[] arr = new Type[3];
arr[0] = typeof (TestType);
arr[1] = typeof (TProperties);
arr[2] = typeAll;
arr[0].RequiresAll ();
arr[1].RequiresPublicProperties ();
arr[1].RequiresPublicFields (); // Should warn
arr[2].RequiresAll ();
arr[3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArraySetElementAndInitializerMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[] arr = new Type[] { typeof (TestType), null, null };
arr[1] = typeof (TProperties);
arr[2] = typeAll;
arr[0].RequiresAll ();
arr[1].RequiresPublicProperties ();
arr[1].RequiresPublicFields (); // Should warn
arr[2].RequiresAll ();
arr[3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
static void TestGetElementAtUnknownIndex (int i = 0)
{
Type[] arr = new Type[] { typeof (TestType) };
arr[i].RequiresPublicFields ();
}
// Trimmer code doesnt handle locals from different branches separetely, therefore merges incorrectly GetMethods with Unknown producing both warnings
[ExpectedWarning ("IL2072", nameof (ArrayDataFlow.GetMethods), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll))]
static void TestMergedArrayElementWithUnknownIndex (int i)
{
Type[] arr = new Type[] { null };
if (i == 1)
arr[0] = GetMethods ();
else
arr[i] = GetFields ();
arr[0].RequiresAll (); // Should warn - there is an unknown value on fields therefore the merged value should be unknown
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
static void TestArrayResetStoreUnknownIndex (int i = 0)
{
Type[] arr = new Type[] { typeof (TestType) };
arr[0].RequiresPublicProperties ();
arr[i] = typeof (TestType); // Unknown index - we reset array to all unknowns
arr[0].RequiresPublicFields (); // Warns
}
// https://github.com/dotnet/linker/issues/2680 - analyzer doesn't reset array in this case
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
// https://github.com/dotnet/linker/issues/2632 - Ref params don't reset or track.
// [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestArrayResetGetElementOnByRefArray (int i = 0)
{
Type[] arr = new Type[] { typeof (TestType), typeof (TestType) };
arr[0].RequiresPublicProperties ();
TakesTypeByRef (ref arr[0]); // Should reset index 0 - linker doesn't
arr[0].RequiresPublicMethods (); // Should warn -- linker doesn't
arr[1].RequiresPublicMethods (); // Shouldn't warn
TakesTypeByRef (ref arr[i]); // Reset - unknown index
arr[1].RequiresPublicFields (); // Warns
}
static void TakesTypeByRef (ref Type type) { }
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
static void TestArrayResetAfterCall ()
{
Type[] arr = new Type[] { typeof (TestType) };
arr[0].RequiresPublicProperties ();
// Calling a method and passing the array will reset the array after the call
// This is necessary since the array is passed by referenced and its unknown
// what the method will do to the array
TakesTypesArray (arr);
arr[0].RequiresPublicFields (); // Warns
}
static void TakesTypesArray (Type[] types) { }
// https://github.com/dotnet/linker/issues/2680
// [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
static void TestArrayResetAfterAssignment ()
{
Type[] arr = new Type[] { typeof (TestType) };
arr[0].RequiresPublicProperties ();
// Assigning the array out of the method means that others can modify it - for non-method-calls it's not very likely to cause problems
// because the only meaningful way this could work in the program is if some other thread accessed and modified the array
// but it's still better to be safe in this case.
_externalArray = arr;
arr[0].RequiresPublicFields (); // Should warn
}
static Type[] _externalArray;
static class TestMultiDimensionalArray
{
public static void Test ()
{
TestArrayWithInitializerOneElementStaticType ();
TestArrayWithInitializerOneElementParameter (typeof (TestType));
TestArrayWithInitializerMultipleElementsStaticType ();
TestArrayWithInitializerMultipleElementsMix<TestType> (typeof (TestType));
TestArraySetElementOneElementStaticType ();
TestArraySetElementOneElementParameter (typeof (TestType));
TestArraySetElementMultipleElementsStaticType ();
TestArraySetElementMultipleElementsMix<TestType> (typeof (TestType));
TestArraySetElementAndInitializerMultipleElementsMix<TestType> (typeof (TestType));
TestGetElementAtUnknownIndex ();
// Array reset - certain operations on array are not tracked fully (or impossible due to unknown inputs)
// and sometimes the only valid thing to do is to reset the array to all unknowns as it's impossible
// to determine what the operation did to the array. So after the reset, everything in the array
// should be treated as unknown value.
TestArrayResetStoreUnknownIndex ();
TestArrayResetGetElementOnByRefArray ();
TestArrayResetAfterCall ();
TestArrayResetAfterAssignment ();
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayWithInitializerOneElementStaticType ()
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayWithInitializerOneElementParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
Type[,] arr = new Type[,] { { type } };
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Below are because we do not handle Multi dimensional arrays
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayWithInitializerMultipleElementsStaticType ()
{
Type[,] arr = new Type[,] { { typeof (TestType), typeof (TestType), typeof (TestType) } };
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresAll ();
arr[0, 2].RequiresAll ();
arr[0, 3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
// Bug
// [ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Below are because we do not handle Multi dimensional arrays
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayWithInitializerMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[,] arr = new Type[,] { { typeof (TestType), typeof (TProperties), typeAll } };
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicProperties ();
arr[0, 1].RequiresPublicFields (); // Should warn
arr[0, 2].RequiresAll ();
arr[0, 3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementOneElementStaticType ()
{
Type[,] arr = new Type[1, 1];
arr[0, 0] = typeof (TestType);
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementOneElementParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
Type[,] arr = new Type[1, 1];
arr[0, 0] = type;
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Below are because we do not handle Multi dimensional arrays
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementMultipleElementsStaticType ()
{
Type[,] arr = new Type[1, 3];
arr[0, 0] = typeof (TestType);
arr[0, 1] = typeof (TestType);
arr[0, 2] = typeof (TestType);
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresAll ();
arr[0, 2].RequiresAll ();
arr[0, 3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
// Bug
// [ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Below are because we do not handle Multi dimensional arrays
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[,] arr = new Type[1, 3];
arr[0, 0] = typeof (TestType);
arr[0, 1] = typeof (TProperties);
arr[0, 2] = typeAll;
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicProperties ();
arr[0, 1].RequiresPublicFields (); // Should warn
arr[0, 2].RequiresAll ();
arr[0, 3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
// Bug
// [ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
// Below are because we do not handle Multi dimensional arrays
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll), ProducedBy = ProducedBy.Trimmer)]
static void TestArraySetElementAndInitializerMultipleElementsMix<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] TProperties> (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeAll)
{
Type[,] arr = new Type[,] { { typeof (TestType), null, null } };
arr[0, 1] = typeof (TProperties);
arr[0, 2] = typeAll;
arr[0, 0].RequiresAll ();
arr[0, 1].RequiresPublicProperties ();
arr[0, 1].RequiresPublicFields (); // Should warn
arr[0, 2].RequiresAll ();
arr[0, 3].RequiresPublicMethods (); // Should warn - unknown value at this index
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
static void TestGetElementAtUnknownIndex (int i = 0)
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, i].RequiresPublicFields ();
}
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayResetStoreUnknownIndex (int i = 0)
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, 0].RequiresPublicProperties ();
arr[0, i] = typeof (TestType); // Unknown index - we reset array to all unknowns
arr[0, 0].RequiresPublicFields (); // Warns
}
// https://github.com/dotnet/linker/issues/2680 - analyzer doesn't reset array in this case
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayResetGetElementOnByRefArray (int i = 0)
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, 0].RequiresPublicProperties ();
TakesTypeByRef (ref arr[0, 0]); // No reset - known index
arr[0, 0].RequiresPublicMethods (); // Doesn't warn
TakesTypeByRef (ref arr[0, i]); // Reset - unknown index
arr[0, 0].RequiresPublicFields (); // Warns
}
static void TakesTypeByRef (ref Type type) { }
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayResetAfterCall ()
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, 0].RequiresPublicProperties ();
// Calling a method and passing the array will reset the array after the call
// This is necessary since the array is passed by referenced and its unknown
// what the method will do to the array
TakesTypesArray (arr);
arr[0, 0].RequiresPublicFields (); // Warns
}
static void TakesTypesArray (Type[,] types) { }
// https://github.com/dotnet/linker/issues/2680
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)]
// Multidimensional Arrays not handled -- assumed to be UnknownValue
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicProperties), ProducedBy = ProducedBy.Trimmer)]
static void TestArrayResetAfterAssignment ()
{
Type[,] arr = new Type[,] { { typeof (TestType) } };
arr[0, 0].RequiresPublicProperties ();
// Assigning the array out of the method means that others can modify it - for non-method-calls it's not very likely to cause problems
// because the only meaningful way this could work in the program is if some other thread accessed and modified the array
// but it's still better to be safe in this case.
_externalArray = arr;
arr[0, 0].RequiresPublicFields (); // Should warn
}
static Type[,] _externalArray;
}
class WriteCapturedArrayElement
{
[ExpectedWarning ("IL2072", nameof (GetUnknownType), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2072", nameof (GetTypeWithPublicConstructors), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
// https://github.com/dotnet/linker/issues/2737
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Trimmer)]
static void TestNullCoalesce ()
{
Type[] arr = new Type[1];
arr[0] = GetUnknownType () ?? GetTypeWithPublicConstructors ();
arr[0].RequiresAll ();
}
[ExpectedWarning ("IL2072", nameof (GetUnknownType), nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Trimmer)]
// https://github.com/dotnet/linker/issues/2736
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll),
ProducedBy = ProducedBy.Analyzer)]
static void TestNullCoalescingAssignment ()
{
Type[] arr = new Type[1];
arr[0] ??= GetUnknownType ();
arr[0].RequiresAll ();
}
// Both linker and analyzer get this wrong. They should produce IL2072 instead.
// https://github.com/dotnet/linker/issues/2736
// https://github.com/dotnet/linker/issues/2737
[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll))]
static void TestNullCoalescingAssignmentComplex ()
{
Type[] arr = new Type[1];
arr[0] ??= (GetUnknownType () ?? GetTypeWithPublicConstructors ());
arr[0].RequiresAll ();
}
public static void Test ()
{
TestNullCoalesce ();
TestNullCoalescingAssignment ();
TestNullCoalescingAssignmentComplex ();
}
}
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)]
private static Type GetTypeWithPublicConstructors ()
{
return null;
}
private static Type GetUnknownType ()
{
return null;
}
public class TestType { }
}
}
|