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

NullableAnnotations.cs « DataFlow « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ab063e3a6a039c71580a8f75c3d7c036de43563 (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
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
// 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;
using DAM = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;
using DAMT = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;

namespace Mono.Linker.Tests.Cases.DataFlow
{
	[ExpectedNoWarnings]
	class NullableAnnotations
	{
		[Kept]
		struct TestType
		{
		}

		// This only gets annotations through Nullable<TestStructPassedInsideNullable>
		[Kept]
		struct TestStructPassedInsideNullable
		{
			[Kept]
			[KeptBackingField]
			public string FirstName { [Kept] get; [Kept] set; }
			[Kept]
			[KeptBackingField]
			public string LastName { [Kept] get; [Kept] set; }
		}


		[Kept]
		struct TestStructWithRucMethod
		{
			[Kept]
			[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
			[RequiresUnreferencedCode ("message")]
			void MethodWithRuc () { }
		}

		[Kept]
		public static void Main ()
		{
			NullableOfAnnotatedGenericParameterRequiresPublicProperties<TestType> ();
			Type _ = ReturnUnderlyingTypeThatRequiresProperties<Nullable<TestType>> (new ());
			TestRequireRucMethodThroughNullable ();

			DamOnNullableKeepsUnderlyingMembers ();
			UnderlyingTypeOfCreatedNullableOfAnnotatedTRequiresPublicProperties<TestType> ();
			RequirePublicFieldsOnGenericParam<Nullable<StructWithFieldsReferencedThroughDamOnNullable>> ();
			NullableOfUnannotatedGenericParamPassedAsGenericParamRequiresPublicFields<StructWithFieldsReferencedThroughDamOnNullable> ();
			NullableOfAnnotatedGenericParamPassedAsGenericParamRequiresPublicFields<StructWithFieldsReferencedThroughDamOnNullable> ();

			TestGetUnderlyingTypeOnStructs ();
			TestAnnotationsOnNullableKeepsMembersOnUnderlyingType ();
			TestGetUnderlyingTypeOfCreatedNullableOnStructs ();
			ImproperMakeGenericTypeDoesntWarn ();
			MakeGenericTypeWithUnknownValue (new object[2] { 1, 2 });
		}

		[Kept]
		static void ImproperMakeGenericTypeDoesntWarn ()
		{
			typeof (Nullable<>).MakeGenericType (typeof (Nullable<int>)).GetProperties ();  // No warning - we treat the cases where reflection throws as "no value".
			typeof (Nullable<>).MakeGenericType (typeof (int[])).GetProperties ();  // No warning - we treat the cases where reflection throws as "no value".
		}

		[Kept]
		[ExpectedWarning ("IL2026", "message")]
		static void RequireAllFromUnderlyingTypeWithMethodWithRUC ()
		{
			var T = typeof (Nullable<TestStructWithRucMethod>);
			var uT = Nullable.GetUnderlyingType (T);
			uT.RequiresAll ();
		}

		[Kept]
		[ExpectedWarning ("IL2026", "message")]
		static void RequireAllFromNullableOfTypeWithMethodWithRuc ()
		{
			typeof (Nullable<TestStructWithRucMethod>).RequiresAll ();
		}

		[Kept]
		[ExpectedWarning ("IL2026", "message")]
		static void RequireAllFromMadeGenericNullableOfTypeWithMethodWithRuc ()
		{
			typeof (Nullable<>).MakeGenericType (typeof (TestStructWithRucMethod)).RequiresAll ();
		}

		[Kept]
		static void TestRequireRucMethodThroughNullable ()
		{
			RequireAllFromUnderlyingTypeWithMethodWithRUC ();
			RequireAllFromNullableOfTypeWithMethodWithRuc ();
			RequireAllFromMadeGenericNullableOfTypeWithMethodWithRuc ();
		}

		[Kept]
		static void UnderlyingTypeOfAnnotatedGenericParameterRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] TNullable> ()
		{
			Nullable.GetUnderlyingType (typeof (TNullable)).RequiresPublicProperties ();
		}

		[Kept]
		static void UnderlyingTypeOfAnnotatedParameterRequiresPublicProperties ([KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] Type tNullable)
		{
			Nullable.GetUnderlyingType (tNullable).RequiresPublicProperties ();
		}

		[Kept]
		[ExpectedWarning ("IL2067")]
		static void UnderlyingTypeOfUnannotatedParameterRequiresPublicProperties (Type tNullable)
		{
			Nullable.GetUnderlyingType (tNullable).RequiresPublicProperties ();
		}

		[Kept]
		[ExpectedWarning ("IL2087")]
		static void UnderlyingTypeOfUnannotatedGenericParameterRequiresProperties<TNullable> ()
		{
			Nullable.GetUnderlyingType (typeof (TNullable)).RequiresPublicProperties ();
		}

		[Kept]
		static void NullableOfAnnotatedGenericParameterRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] T> () where T : struct
		{
			Nullable.GetUnderlyingType (typeof (Nullable<T>)).RequiresPublicProperties ();
		}

		[Kept]
		[ExpectedWarning ("IL2087")]
		static void NullableOfUnannotatedGenericParameterRequiresPublicProperties<T> () where T : struct
		{
			Nullable.GetUnderlyingType (typeof (Nullable<T>)).RequiresPublicProperties ();
		}

		[Kept]
		static void MakeGenericNullableOfAnnotatedParameterRequiresPublicProperties ([KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] Type t)
		{
			Nullable.GetUnderlyingType (typeof (Nullable<>).MakeGenericType (t)).RequiresPublicProperties ();
		}

		[Kept]
		[ExpectedWarning ("IL2067")]
		static void MakeGenericNullableOfUnannotatedParameterRequiresPublicProperties (Type t)
		{
			Nullable.GetUnderlyingType (typeof (Nullable<>).MakeGenericType (t)).RequiresPublicProperties ();
		}

		[Kept]
		static void MakeGenericNullableOfAnnotatedGenericParameterRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] T> ()
		{
			Nullable.GetUnderlyingType (typeof (Nullable<>).MakeGenericType (typeof (T))).RequiresPublicProperties ();
		}

		[Kept]
		[ExpectedWarning ("IL2087")]
		static void MakeGenericNullableOfUnannotatedGenericParameterRequiresPublicProperties<T> ()
		{
			Nullable.GetUnderlyingType (typeof (Nullable<>).MakeGenericType (typeof (T))).RequiresPublicProperties ();
		}

		[Kept]
		static void TestGetUnderlyingTypeOnStructs ()
		{
			UnderlyingTypeOfAnnotatedParameterRequiresPublicProperties (typeof (TestType));
			UnderlyingTypeOfAnnotatedGenericParameterRequiresPublicProperties<TestType> ();
			UnderlyingTypeOfUnannotatedParameterRequiresPublicProperties (typeof (TestType));
			UnderlyingTypeOfUnannotatedGenericParameterRequiresProperties<TestType> ();
		}

		[Kept]
		static void TestAnnotationsOnNullableKeepsMembersOnUnderlyingType ()
		{
			UnderlyingTypeOfAnnotatedParameterRequiresPublicProperties (typeof (Nullable<TestStructPassedInsideNullable>));
			UnderlyingTypeOfAnnotatedGenericParameterRequiresPublicProperties<Nullable<TestStructPassedInsideNullable>> ();
			UnderlyingTypeOfUnannotatedParameterRequiresPublicProperties (typeof (Nullable<TestStructPassedInsideNullable>));
			UnderlyingTypeOfUnannotatedGenericParameterRequiresProperties<Nullable<TestStructPassedInsideNullable>> ();
		}

		[Kept]
		static void TestGetUnderlyingTypeOfCreatedNullableOnStructs ()
		{
			MakeGenericNullableOfAnnotatedParameterRequiresPublicProperties (typeof (TestType));
			MakeGenericNullableOfAnnotatedGenericParameterRequiresPublicProperties<TestType> ();
			NullableOfUnannotatedGenericParameterRequiresPublicProperties<TestType> ();
			MakeGenericNullableOfUnannotatedParameterRequiresPublicProperties (typeof (TestType));
			MakeGenericNullableOfUnannotatedGenericParameterRequiresPublicProperties<TestType> ();
			NullableOfUnannotatedGenericParameterRequiresPublicProperties<TestType> ();
		}


		[Kept]
		[return: DynamicallyAccessedMembers (DAMT.PublicProperties)]
		[return: KeptAttributeAttribute (typeof (DAM))]
		static Type ReturnUnderlyingTypeThatRequiresProperties<[KeptAttributeAttribute (typeof (DAM))][DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] T> (T instance)
		{
			Type type = Nullable.GetUnderlyingType (typeof (T)) ?? typeof (T);
			return type;
		}

		[Kept]
		struct StructWithUnreferencedFields
		{
			[Kept]
			public int field1;

			[Kept]
			public StructReferencedThroughDam s;

			[KeptBackingField]
			public int prop { get; set; }
		}

		[Kept]
		struct StructReferencedThroughDam { }

		[Kept]
		static void DamOnNullableKeepsUnderlyingMembers ()
		{
			typeof (Nullable<StructWithUnreferencedFields>).RequiresPublicFields ();
		}

		[Kept]
		static void UnderlyingTypeOfCreatedNullableOfAnnotatedTRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] T> () where T : struct
		{
			Type t = typeof (Nullable<T>);
			t = Nullable.GetUnderlyingType (t);
			t.RequiresPublicProperties ();
		}

		[Kept]
		struct StructWithFieldsReferencedThroughDamOnNullable
		{
			[Kept]
			public int field;
			public int method () { return 0; }
		}

		[Kept]
		static void RequirePublicFieldsOnGenericParam<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicFields)] T> ()
		{
		}

		[Kept]
		[ExpectedWarning ("IL2091")]
		static void NullableOfUnannotatedGenericParamPassedAsGenericParamRequiresPublicFields<T> () where T : struct
		{
			RequirePublicFieldsOnGenericParam<Nullable<T>> ();
		}

		[Kept]
		static void NullableOfAnnotatedGenericParamPassedAsGenericParamRequiresPublicFields<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicFields)] T> () where T : struct
		{
			RequirePublicFieldsOnGenericParam<Nullable<T>> ();
		}

		[Kept]
		[ExpectedWarning ("IL2075")]
		static void MakeGenericTypeWithUnknownValue (object[] maybetypes)
		{
			Type[] types = new Type[] { maybetypes[0] as Type };  // Roundabout way to get UnknownValue - it is getting tricky to do that reliably
			Type nullable = typeof (Nullable<>).MakeGenericType (types);
			nullable.GetProperties ();  // Must WARN
		}
	}
}