Skip to content

Commit c230fdf

Browse files
authored
Nullable annotations part 3 (#2285)
* Nullable annotations part 3 * Fix spelling
1 parent ff06442 commit c230fdf

File tree

5 files changed

+150
-128
lines changed

5 files changed

+150
-128
lines changed

src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Reflection;
99
using Mono.Cecil;
1010

11+
#nullable enable
12+
1113
namespace Mono.Linker
1214
{
1315
// Temporary workaround - should be removed once linker can be upgraded to build against
@@ -118,7 +120,7 @@ public static IEnumerable<IMetadataTokenProvider> GetDynamicallyAccessedMembers
118120
}
119121
}
120122

121-
public static IEnumerable<MethodDefinition> GetConstructorsOnType (this TypeDefinition type, Func<MethodDefinition, bool> filter, BindingFlags? bindingFlags = null)
123+
public static IEnumerable<MethodDefinition> GetConstructorsOnType (this TypeDefinition type, Func<MethodDefinition, bool>? filter, BindingFlags? bindingFlags = null)
122124
{
123125
foreach (var method in type.Methods) {
124126
if (!method.IsConstructor)
@@ -143,8 +145,9 @@ public static IEnumerable<MethodDefinition> GetConstructorsOnType (this TypeDefi
143145
}
144146
}
145147

146-
public static IEnumerable<MethodDefinition> GetMethodsOnTypeHierarchy (this TypeDefinition type, LinkContext context, Func<MethodDefinition, bool> filter, BindingFlags? bindingFlags = null)
148+
public static IEnumerable<MethodDefinition> GetMethodsOnTypeHierarchy (this TypeDefinition thisType, LinkContext context, Func<MethodDefinition, bool>? filter, BindingFlags? bindingFlags = null)
147149
{
150+
TypeDefinition? type = thisType;
148151
bool onBaseType = false;
149152
while (type != null) {
150153
foreach (var method in type.Methods) {
@@ -186,8 +189,9 @@ public static IEnumerable<MethodDefinition> GetMethodsOnTypeHierarchy (this Type
186189
}
187190
}
188191

189-
public static IEnumerable<FieldDefinition> GetFieldsOnTypeHierarchy (this TypeDefinition type, LinkContext context, Func<FieldDefinition, bool> filter, BindingFlags? bindingFlags = BindingFlags.Default)
192+
public static IEnumerable<FieldDefinition> GetFieldsOnTypeHierarchy (this TypeDefinition thisType, LinkContext context, Func<FieldDefinition, bool>? filter, BindingFlags? bindingFlags = BindingFlags.Default)
190193
{
194+
TypeDefinition? type = thisType;
191195
bool onBaseType = false;
192196
while (type != null) {
193197
foreach (var field in type.Fields) {
@@ -225,7 +229,7 @@ public static IEnumerable<FieldDefinition> GetFieldsOnTypeHierarchy (this TypeDe
225229
}
226230
}
227231

228-
public static IEnumerable<TypeDefinition> GetNestedTypesOnType (this TypeDefinition type, Func<TypeDefinition, bool> filter, BindingFlags? bindingFlags = BindingFlags.Default)
232+
public static IEnumerable<TypeDefinition> GetNestedTypesOnType (this TypeDefinition type, Func<TypeDefinition, bool>? filter, BindingFlags? bindingFlags = BindingFlags.Default)
229233
{
230234
foreach (var nestedType in type.NestedTypes) {
231235
if (filter != null && !filter (nestedType))
@@ -245,8 +249,9 @@ public static IEnumerable<TypeDefinition> GetNestedTypesOnType (this TypeDefinit
245249
}
246250
}
247251

248-
public static IEnumerable<PropertyDefinition> GetPropertiesOnTypeHierarchy (this TypeDefinition type, LinkContext context, Func<PropertyDefinition, bool> filter, BindingFlags? bindingFlags = BindingFlags.Default)
252+
public static IEnumerable<PropertyDefinition> GetPropertiesOnTypeHierarchy (this TypeDefinition thisType, LinkContext context, Func<PropertyDefinition, bool>? filter, BindingFlags? bindingFlags = BindingFlags.Default)
249253
{
254+
TypeDefinition? type = thisType;
250255
bool onBaseType = false;
251256
while (type != null) {
252257
foreach (var property in type.Properties) {
@@ -293,8 +298,9 @@ public static IEnumerable<PropertyDefinition> GetPropertiesOnTypeHierarchy (this
293298
}
294299
}
295300

296-
public static IEnumerable<EventDefinition> GetEventsOnTypeHierarchy (this TypeDefinition type, LinkContext context, Func<EventDefinition, bool> filter, BindingFlags? bindingFlags = BindingFlags.Default)
301+
public static IEnumerable<EventDefinition> GetEventsOnTypeHierarchy (this TypeDefinition thisType, LinkContext context, Func<EventDefinition, bool>? filter, BindingFlags? bindingFlags = BindingFlags.Default)
297302
{
303+
TypeDefinition? type = thisType;
298304
bool onBaseType = false;
299305
while (type != null) {
300306
foreach (var @event in type.Events) {
@@ -343,13 +349,14 @@ public static IEnumerable<EventDefinition> GetEventsOnTypeHierarchy (this TypeDe
343349

344350
// declaredOnly will cause this to retrieve interfaces recursively required by the type, but doesn't necessarily
345351
// include interfaces required by any base types.
346-
public static IEnumerable<InterfaceImplementation> GetAllInterfaceImplementations (this TypeDefinition type, LinkContext context, bool declaredOnly)
352+
public static IEnumerable<InterfaceImplementation> GetAllInterfaceImplementations (this TypeDefinition thisType, LinkContext context, bool declaredOnly)
347353
{
354+
TypeDefinition? type = thisType;
348355
while (type != null) {
349356
foreach (var i in type.Interfaces) {
350357
yield return i;
351358

352-
TypeDefinition interfaceType = context.TryResolve (i.InterfaceType);
359+
TypeDefinition? interfaceType = context.TryResolve (i.InterfaceType);
353360
if (interfaceType != null) {
354361
// declaredOnly here doesn't matter since interfaces don't have base types
355362
foreach (var innerInterface in interfaceType.GetAllInterfaceImplementations (context, declaredOnly: true))

src/linker/Linker.Dataflow/FlowAnnotations.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Mono.Cecil;
1010
using Mono.Cecil.Cil;
1111

12+
#nullable enable
13+
1214
namespace Mono.Linker.Dataflow
1315
{
1416
class FlowAnnotations
@@ -74,15 +76,15 @@ public bool ShouldWarnWhenAccessedForReflection (IMemberDefinition provider) =>
7476

7577
public DynamicallyAccessedMemberTypes GetGenericParameterAnnotation (GenericParameter genericParameter)
7678
{
77-
TypeDefinition declaringType = _context.Resolve (genericParameter.DeclaringType);
79+
TypeDefinition? declaringType = _context.Resolve (genericParameter.DeclaringType);
7880
if (declaringType != null) {
7981
if (GetAnnotations (declaringType).TryGetAnnotation (genericParameter, out var annotation))
8082
return annotation;
8183

8284
return DynamicallyAccessedMemberTypes.None;
8385
}
8486

85-
MethodDefinition declaringMethod = _context.Resolve (genericParameter.DeclaringMethod);
87+
MethodDefinition? declaringMethod = _context.Resolve (genericParameter.DeclaringMethod);
8688
if (declaringMethod != null && GetAnnotations (declaringMethod.DeclaringType).TryGetAnnotation (declaringMethod, out var methodTypeAnnotations) &&
8789
methodTypeAnnotations.TryGetAnnotation (genericParameter, out var methodAnnotation))
8890
return methodAnnotation;
@@ -159,7 +161,7 @@ static bool IsDynamicallyAccessedMembersAttribute (CustomAttribute attribute)
159161
return attributeType.Name == "DynamicallyAccessedMembersAttribute" && attributeType.Namespace == "System.Diagnostics.CodeAnalysis";
160162
}
161163

162-
DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttribute (IMemberDefinition member, ICustomAttributeProvider providerIfNotMember = null)
164+
DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttribute (IMemberDefinition member, ICustomAttributeProvider? providerIfNotMember = null)
163165
{
164166
ICustomAttributeProvider provider = providerIfNotMember ?? member;
165167
if (!_context.CustomAttributes.HasAny (provider))
@@ -209,7 +211,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
209211
// Next go over all methods with an explicit annotation
210212
if (type.HasMethods) {
211213
foreach (MethodDefinition method in type.Methods) {
212-
DynamicallyAccessedMemberTypes[] paramAnnotations = null;
214+
DynamicallyAccessedMemberTypes[]? paramAnnotations = null;
213215

214216
// We convert indices from metadata space to IL space here.
215217
// IL space assigns index 0 to the `this` parameter on instance methods.
@@ -267,7 +269,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
267269
2106, method, subcategory: MessageSubCategory.TrimAnalysis);
268270
}
269271

270-
DynamicallyAccessedMemberTypes[] genericParameterAnnotations = null;
272+
DynamicallyAccessedMemberTypes[]? genericParameterAnnotations = null;
271273
if (method.HasGenericParameters) {
272274
for (int genericParameterIndex = 0; genericParameterIndex < method.GenericParameters.Count; genericParameterIndex++) {
273275
var genericParameter = method.GenericParameters[genericParameterIndex];
@@ -313,7 +315,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
313315
continue;
314316
}
315317

316-
FieldDefinition backingFieldFromSetter = null;
318+
FieldDefinition? backingFieldFromSetter = null;
317319

318320
// Propagate the annotation to the setter method
319321
MethodDefinition setMethod = property.SetMethod;
@@ -342,7 +344,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
342344
}
343345
}
344346

345-
FieldDefinition backingFieldFromGetter = null;
347+
FieldDefinition? backingFieldFromGetter = null;
346348

347349
// Propagate the annotation to the getter method
348350
MethodDefinition getMethod = property.GetMethod;
@@ -366,7 +368,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
366368
}
367369
}
368370

369-
FieldDefinition backingField;
371+
FieldDefinition? backingField;
370372
if (backingFieldFromGetter != null && backingFieldFromSetter != null &&
371373
backingFieldFromGetter != backingFieldFromSetter) {
372374
_context.LogWarning (
@@ -389,7 +391,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
389391
}
390392
}
391393

392-
DynamicallyAccessedMemberTypes[] typeGenericParameterAnnotations = null;
394+
DynamicallyAccessedMemberTypes[]? typeGenericParameterAnnotations = null;
393395
if (type.HasGenericParameters) {
394396
for (int genericParameterIndex = 0; genericParameterIndex < type.GenericParameters.Count; genericParameterIndex++) {
395397
var genericParameter = type.GenericParameters[genericParameterIndex];
@@ -405,13 +407,13 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
405407
return new TypeAnnotations (type, typeAnnotation, annotatedMethods.ToArray (), annotatedFields.ToArray (), typeGenericParameterAnnotations);
406408
}
407409

408-
bool ScanMethodBodyForFieldAccess (MethodBody body, bool write, out FieldDefinition found)
410+
bool ScanMethodBodyForFieldAccess (MethodBody body, bool write, out FieldDefinition? found)
409411
{
410412
// Tries to find the backing field for a property getter/setter.
411413
// Returns true if this is a method body that we can unambiguously analyze.
412414
// The found field could still be null if there's no backing store.
413415

414-
FieldReference foundReference = null;
416+
FieldReference? foundReference = null;
415417

416418
foreach (Instruction instruction in body.Instructions) {
417419
switch (instruction.OpCode.Code) {
@@ -464,7 +466,7 @@ bool IsTypeInterestingForDataflow (TypeReference typeReference)
464466
if (typeReference.MetadataType == MetadataType.String)
465467
return true;
466468

467-
TypeDefinition type = _context.TryResolve (typeReference);
469+
TypeDefinition? type = _context.TryResolve (typeReference);
468470
return type != null && (
469471
_hierarchyInfo.IsSystemType (type) ||
470472
_hierarchyInfo.IsSystemReflectionIReflect (type));
@@ -480,9 +482,9 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
480482

481483
if (methodAnnotations.ParameterAnnotations != null || baseMethodAnnotations.ParameterAnnotations != null) {
482484
if (methodAnnotations.ParameterAnnotations == null)
483-
ValidateMethodParametersHaveNoAnnotations (ref baseMethodAnnotations, method, baseMethod, method);
485+
ValidateMethodParametersHaveNoAnnotations (baseMethodAnnotations.ParameterAnnotations!, method, baseMethod, method);
484486
else if (baseMethodAnnotations.ParameterAnnotations == null)
485-
ValidateMethodParametersHaveNoAnnotations (ref methodAnnotations, method, baseMethod, method);
487+
ValidateMethodParametersHaveNoAnnotations (methodAnnotations.ParameterAnnotations, method, baseMethod, method);
486488
else {
487489
if (methodAnnotations.ParameterAnnotations.Length != baseMethodAnnotations.ParameterAnnotations.Length)
488490
return;
@@ -499,9 +501,9 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
499501

500502
if (methodAnnotations.GenericParameterAnnotations != null || baseMethodAnnotations.GenericParameterAnnotations != null) {
501503
if (methodAnnotations.GenericParameterAnnotations == null)
502-
ValidateMethodGenericParametersHaveNoAnnotations (ref baseMethodAnnotations, method, baseMethod, method);
504+
ValidateMethodGenericParametersHaveNoAnnotations (baseMethodAnnotations.GenericParameterAnnotations!, method, baseMethod, method);
503505
else if (baseMethodAnnotations.GenericParameterAnnotations == null)
504-
ValidateMethodGenericParametersHaveNoAnnotations (ref methodAnnotations, method, baseMethod, method);
506+
ValidateMethodGenericParametersHaveNoAnnotations (methodAnnotations.GenericParameterAnnotations, method, baseMethod, method);
505507
else {
506508
if (methodAnnotations.GenericParameterAnnotations.Length != baseMethodAnnotations.GenericParameterAnnotations.Length)
507509
return;
@@ -518,10 +520,10 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
518520
}
519521
}
520522

521-
void ValidateMethodParametersHaveNoAnnotations (ref MethodAnnotations methodAnnotations, MethodDefinition method, MethodDefinition baseMethod, IMemberDefinition origin)
523+
void ValidateMethodParametersHaveNoAnnotations (DynamicallyAccessedMemberTypes[] parameterAnnotations, MethodDefinition method, MethodDefinition baseMethod, IMemberDefinition origin)
522524
{
523-
for (int parameterIndex = 0; parameterIndex < methodAnnotations.ParameterAnnotations.Length; parameterIndex++) {
524-
var annotation = methodAnnotations.ParameterAnnotations[parameterIndex];
525+
for (int parameterIndex = 0; parameterIndex < parameterAnnotations.Length; parameterIndex++) {
526+
var annotation = parameterAnnotations[parameterIndex];
525527
if (annotation != DynamicallyAccessedMemberTypes.None)
526528
LogValidationWarning (
527529
DiagnosticUtilities.GetMethodParameterFromIndex (method, parameterIndex),
@@ -530,10 +532,10 @@ void ValidateMethodParametersHaveNoAnnotations (ref MethodAnnotations methodAnno
530532
}
531533
}
532534

533-
void ValidateMethodGenericParametersHaveNoAnnotations (ref MethodAnnotations methodAnnotations, MethodDefinition method, MethodDefinition baseMethod, IMemberDefinition origin)
535+
void ValidateMethodGenericParametersHaveNoAnnotations (DynamicallyAccessedMemberTypes[] genericParameterAnnotations, MethodDefinition method, MethodDefinition baseMethod, IMemberDefinition origin)
534536
{
535-
for (int genericParameterIndex = 0; genericParameterIndex < methodAnnotations.GenericParameterAnnotations.Length; genericParameterIndex++) {
536-
if (methodAnnotations.GenericParameterAnnotations[genericParameterIndex] != DynamicallyAccessedMemberTypes.None) {
537+
for (int genericParameterIndex = 0; genericParameterIndex < genericParameterAnnotations.Length; genericParameterIndex++) {
538+
if (genericParameterAnnotations[genericParameterIndex] != DynamicallyAccessedMemberTypes.None) {
537539
LogValidationWarning (
538540
method.GenericParameters[genericParameterIndex],
539541
baseMethod.GenericParameters[genericParameterIndex],
@@ -589,14 +591,14 @@ readonly struct TypeAnnotations
589591
readonly DynamicallyAccessedMemberTypes _typeAnnotation;
590592
readonly MethodAnnotations[] _annotatedMethods;
591593
readonly FieldAnnotation[] _annotatedFields;
592-
readonly DynamicallyAccessedMemberTypes[] _genericParameterAnnotations;
594+
readonly DynamicallyAccessedMemberTypes[]? _genericParameterAnnotations;
593595

594596
public TypeAnnotations (
595597
TypeDefinition type,
596598
DynamicallyAccessedMemberTypes typeAnnotation,
597599
MethodAnnotations[] annotatedMethods,
598600
FieldAnnotation[] annotatedFields,
599-
DynamicallyAccessedMemberTypes[] genericParameterAnnotations)
601+
DynamicallyAccessedMemberTypes[]? genericParameterAnnotations)
600602
=> (_type, _typeAnnotation, _annotatedMethods, _annotatedFields, _genericParameterAnnotations)
601603
= (type, typeAnnotation, annotatedMethods, annotatedFields, genericParameterAnnotations);
602604

@@ -659,15 +661,15 @@ public bool TryGetAnnotation (GenericParameter genericParameter, out Dynamically
659661
readonly struct MethodAnnotations
660662
{
661663
public readonly MethodDefinition Method;
662-
public readonly DynamicallyAccessedMemberTypes[] ParameterAnnotations;
664+
public readonly DynamicallyAccessedMemberTypes[]? ParameterAnnotations;
663665
public readonly DynamicallyAccessedMemberTypes ReturnParameterAnnotation;
664-
public readonly DynamicallyAccessedMemberTypes[] GenericParameterAnnotations;
666+
public readonly DynamicallyAccessedMemberTypes[]? GenericParameterAnnotations;
665667

666668
public MethodAnnotations (
667669
MethodDefinition method,
668-
DynamicallyAccessedMemberTypes[] paramAnnotations,
670+
DynamicallyAccessedMemberTypes[]? paramAnnotations,
669671
DynamicallyAccessedMemberTypes returnParamAnnotations,
670-
DynamicallyAccessedMemberTypes[] genericParameterAnnotations)
672+
DynamicallyAccessedMemberTypes[]? genericParameterAnnotations)
671673
=> (Method, ParameterAnnotations, ReturnParameterAnnotation, GenericParameterAnnotations) =
672674
(method, paramAnnotations, returnParamAnnotations, genericParameterAnnotations);
673675

0 commit comments

Comments
 (0)