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
|
// 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;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
namespace Mono.Linker.Tests.Cases.RequiresCapability
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
class RequiresAccessedThrough
{
public static void Main ()
{
TestRequiresOnlyThroughReflection ();
AccessedThroughReflectionOnGenericType<TestType>.Test ();
AccessThroughSpecialAttribute.Test ();
AccessThroughPInvoke.Test ();
AccessThroughNewConstraint.Test<TestType> ();
AccessThroughNewConstraint.TestNewConstraintOnTypeParameter<TestType> ();
AccessThroughNewConstraint.TestNewConstraintOnTypeParameterOfStaticType<TestType> ();
AccessThroughNewConstraint.TestNewConstraintOnTypeParameterInAnnotatedMethod ();
AccessThroughNewConstraint.TestNewConstraintOnTypeParameterInAnnotatedType ();
AccessThroughLdToken.Test ();
}
class TestType { }
[RequiresUnreferencedCode ("Message for --RequiresOnlyThroughReflection--")]
static void RequiresOnlyThroughReflection ()
{
}
[ExpectedWarning ("IL2026", "--RequiresOnlyThroughReflection--")]
static void TestRequiresOnlyThroughReflection ()
{
typeof (RequiresAccessedThrough)
.GetMethod (nameof (RequiresOnlyThroughReflection), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.Invoke (null, new object[0]);
}
class AccessedThroughReflectionOnGenericType<T>
{
[RequiresUnreferencedCode ("Message for --GenericType.RequiresOnlyThroughReflection--")]
public static void RequiresOnlyThroughReflection ()
{
}
[ExpectedWarning ("IL2026", "--GenericType.RequiresOnlyThroughReflection--")]
public static void Test ()
{
typeof (AccessedThroughReflectionOnGenericType<T>)
.GetMethod (nameof (RequiresOnlyThroughReflection))
.Invoke (null, new object[0]);
}
}
class AccessThroughSpecialAttribute
{
// https://github.com/dotnet/linker/issues/1873
// [ExpectedWarning ("IL2026", "--DebuggerProxyType.Method--")]
[DebuggerDisplay ("Some{*}value")]
class TypeWithDebuggerDisplay
{
[RequiresUnreferencedCode ("Message for --DebuggerProxyType.Method--")]
public void Method ()
{
}
}
public static void Test ()
{
var _ = new TypeWithDebuggerDisplay ();
}
}
class AccessThroughPInvoke
{
class PInvokeReturnType
{
[RequiresUnreferencedCode ("Message for --PInvokeReturnType.ctor--")]
public PInvokeReturnType () { }
}
// https://github.com/mono/linker/issues/2116
[ExpectedWarning ("IL2026", "--PInvokeReturnType.ctor--", ProducedBy = ProducedBy.Trimmer)]
[DllImport ("nonexistent")]
static extern PInvokeReturnType PInvokeReturnsType ();
// Analyzer doesn't support IL2050 yet
[ExpectedWarning ("IL2050", ProducedBy = ProducedBy.Trimmer)]
public static void Test ()
{
PInvokeReturnsType ();
}
}
class AccessThroughNewConstraint
{
class NewConstraintTestType
{
[RequiresUnreferencedCode ("Message for --NewConstraintTestType.ctor--")]
[RequiresAssemblyFiles ("Message for --NewConstraintTestType.ctor--")]
[RequiresDynamicCode ("Message for --NewConstraintTestType.ctor--")]
public NewConstraintTestType () { }
}
[RequiresUnreferencedCode ("Message for --NewConstraintTestAnnotatedType--")]
class NewConstraintTestAnnotatedType
{
}
static void GenericMethod<T> () where T : new() { }
[ExpectedWarning ("IL2026", "--NewConstraintTestType.ctor--")]
[ExpectedWarning ("IL2026", "--NewConstraintTestAnnotatedType--")]
[ExpectedWarning ("IL3002", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
public static void Test<T> () where T : new()
{
GenericMethod<NewConstraintTestType> ();
GenericMethod<NewConstraintTestAnnotatedType> ();
GenericMethod<T> (); // should not crash analyzer
}
static class NewConstraintOnTypeParameterOfStaticType<T> where T : new()
{
public static void DoNothing () { }
}
class NewConstraintOnTypeParameter<T> where T : new()
{
}
[ExpectedWarning ("IL2026", "--NewConstraintTestType.ctor--")]
[ExpectedWarning ("IL2026", "--NewConstraintTestAnnotatedType--")]
[ExpectedWarning ("IL3002", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
public static void TestNewConstraintOnTypeParameter<T> () where T : new()
{
_ = new NewConstraintOnTypeParameter<NewConstraintTestType> ();
_ = new NewConstraintOnTypeParameter<NewConstraintTestAnnotatedType> ();
_ = new NewConstraintOnTypeParameter<T> (); // should not crash analyzer
}
[ExpectedWarning ("IL2026", "--AnnotatedMethod--")]
[ExpectedWarning ("IL3002", "--AnnotatedMethod--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--AnnotatedMethod--", ProducedBy = ProducedBy.Analyzer)]
public static void TestNewConstraintOnTypeParameterInAnnotatedMethod ()
{
AnnotatedMethod ();
}
[RequiresUnreferencedCode ("--AnnotatedMethod--")]
[RequiresAssemblyFiles ("--AnnotatedMethod--")]
[RequiresDynamicCode ("--AnnotatedMethod--")]
static void AnnotatedMethod ()
{
_ = new NewConstraintOnTypeParameter<NewConstraintTestType> ();
_ = new NewConstraintOnTypeParameter<NewConstraintTestAnnotatedType> ();
}
[ExpectedWarning ("IL2026", "--AnnotatedType--")]
public static void TestNewConstraintOnTypeParameterInAnnotatedType ()
{
AnnotatedType.Method ();
}
[RequiresUnreferencedCode ("--AnnotatedType--")]
class AnnotatedType
{
[ExpectedWarning ("IL3002", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
public static void Method ()
{
_ = new NewConstraintOnTypeParameter<NewConstraintTestType> ();
_ = new NewConstraintOnTypeParameter<NewConstraintTestAnnotatedType> ();
}
}
[ExpectedWarning ("IL2026", "--NewConstraintTestType.ctor--")]
[ExpectedWarning ("IL2026", "--NewConstraintTestAnnotatedType--")]
[ExpectedWarning ("IL3002", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--NewConstraintTestType.ctor--", ProducedBy = ProducedBy.Analyzer)]
public static void TestNewConstraintOnTypeParameterOfStaticType<T> () where T : new()
{
NewConstraintOnTypeParameterOfStaticType<NewConstraintTestType>.DoNothing ();
NewConstraintOnTypeParameterOfStaticType<NewConstraintTestAnnotatedType>.DoNothing ();
NewConstraintOnTypeParameterOfStaticType<T>.DoNothing (); // should not crash analyzer
}
}
class AccessThroughLdToken
{
static bool PropertyWithLdToken {
[RequiresUnreferencedCode ("Message for --PropertyWithLdToken.get--")]
[RequiresAssemblyFiles ("Message for --PropertyWithLdToken.get--")]
[RequiresDynamicCode ("Message for --PropertyWithLdToken.get--")]
get {
return false;
}
}
[ExpectedWarning ("IL2026", "--PropertyWithLdToken.get--")]
[ExpectedWarning ("IL2026", "--PropertyWithLdToken.get--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL3002", "--PropertyWithLdToken.get--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--PropertyWithLdToken.get--", ProducedBy = ProducedBy.Analyzer)]
public static void Test ()
{
Expression<Func<bool>> getter = () => PropertyWithLdToken;
}
}
}
}
|