Skip to content

Commit

Permalink
Nullable annotate System.Reflection.MetadataLoadContext.
Browse files Browse the repository at this point in the history
* Nullable annotate System.Reflection.MetadataLoadContext

* address feedback

* address feedback

* undo accidental change

* address more feedback

* Revert "address more feedback"

This reverts commit 6519238.
  • Loading branch information
eiriktsarpalis authored Jan 30, 2020
1 parent 77b1d64 commit b27a4b3
Show file tree
Hide file tree
Showing 106 changed files with 761 additions and 753 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace System.Reflection
public abstract partial class MetadataAssemblyResolver
{
protected MetadataAssemblyResolver() { }
public abstract System.Reflection.Assembly Resolve(System.Reflection.MetadataLoadContext context, System.Reflection.AssemblyName assemblyName);
public abstract System.Reflection.Assembly? Resolve(System.Reflection.MetadataLoadContext context, System.Reflection.AssemblyName assemblyName);
}
public sealed partial class MetadataLoadContext : System.IDisposable
{
public MetadataLoadContext(System.Reflection.MetadataAssemblyResolver resolver, string coreAssemblyName = null) { }
public System.Reflection.Assembly CoreAssembly { get { throw null; } }
public MetadataLoadContext(System.Reflection.MetadataAssemblyResolver resolver, string? coreAssemblyName = null) { }
public System.Reflection.Assembly? CoreAssembly { get { throw null; } }
public void Dispose() { }
public System.Collections.Generic.IEnumerable<System.Reflection.Assembly> GetAssemblies() { throw null; }
public System.Reflection.Assembly LoadFromAssemblyName(System.Reflection.AssemblyName assemblyName) { throw null; }
Expand All @@ -27,6 +27,6 @@ public void Dispose() { }
public partial class PathAssemblyResolver : System.Reflection.MetadataAssemblyResolver
{
public PathAssemblyResolver(System.Collections.Generic.IEnumerable<string> assemblyPaths) { }
public override System.Reflection.Assembly Resolve(System.Reflection.MetadataLoadContext context, System.Reflection.AssemblyName assemblyName) { throw null; }
public override System.Reflection.Assembly? Resolve(System.Reflection.MetadataLoadContext context, System.Reflection.AssemblyName assemblyName) { throw null; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>netstandard2.0-Debug;netstandard2.0-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Reflection.MetadataLoadContext.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<NoWarn>$(NoWarn);CS1573</NoWarn>
<!-- Only the netcoreapp version supports the new reflection apis (IsSZArray, etc.) -->
<Configurations>$(NetCoreAppCurrent)-Debug;$(NetCoreAppCurrent)-Release;netcoreapp3.0-Debug;netcoreapp3.0-Release;netstandard2.0-Debug;netstandard2.0-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\CoreRtBridge.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Reflection;
using System.Reflection.TypeLoading;
using CultureInfo = System.Globalization.CultureInfo;
Expand All @@ -11,7 +12,7 @@ namespace System
internal sealed partial class DefaultBinder : Binder
{
private readonly MetadataLoadContext _loader;
private readonly Type _objectType;
private readonly Type? _objectType;

internal DefaultBinder(MetadataLoadContext loader)
{
Expand All @@ -35,16 +36,16 @@ internal DefaultBinder(MetadataLoadContext loader)
// The most specific match will be selected.
//
public sealed override MethodBase BindToMethod(
BindingFlags bindingAttr, MethodBase[] match, ref object[] args,
ParameterModifier[] modifiers, CultureInfo cultureInfo, string[] names, out object state) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);
BindingFlags bindingAttr, MethodBase[] match, ref object?[] args,
ParameterModifier[]? modifiers, CultureInfo? cultureInfo, string[]? names, out object state) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);

// Given a set of fields that match the base criteria, select a field.
// if value is null then we have no way to select a field
public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo cultureInfo) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);
public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo? cultureInfo) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);

// Given a set of methods that match the base criteria, select a method based upon an array of types.
// This method should return null if no method matches the criteria.
public sealed override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
public sealed override MethodBase? SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[]? modifiers)
{
int i;
int j;
Expand Down Expand Up @@ -80,7 +81,7 @@ public sealed override MethodBase SelectMethod(BindingFlags bindingAttr, MethodB
if (pCls == _objectType)
continue;

Type type = types[j];
Type? type = types[j];
if (type.IsSignatureType())
{
if (!(candidates[i] is MethodInfo methodInfo))
Expand Down Expand Up @@ -143,8 +144,8 @@ public sealed override MethodBase SelectMethod(BindingFlags bindingAttr, MethodB
}

// Given a set of properties that match the base criteria, select one.
public sealed override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType,
Type[] indexes, ParameterModifier[] modifiers)
public sealed override PropertyInfo? SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type? returnType,
Type[]? indexes, ParameterModifier[]? modifiers)
{
// Allow a null indexes array. But if it is not null, every element must be non-null as well.
if (indexes != null)
Expand Down Expand Up @@ -230,6 +231,7 @@ public sealed override PropertyInfo SelectProperty(BindingFlags bindingAttr, Pro
paramOrder[i] = i;
for (i = 1; i < curIdx; i++)
{
Debug.Assert(returnType != null);
int newMin = FindMostSpecificType(candidates[currentMin].PropertyType, candidates[i].PropertyType, returnType);
if (newMin == 0 && indexes != null)
newMin = FindMostSpecific(
Expand Down Expand Up @@ -262,13 +264,13 @@ public sealed override PropertyInfo SelectProperty(BindingFlags bindingAttr, Pro

// The default binder doesn't support any change type functionality.
// This is because the default is built into the low level invoke code.
public override object ChangeType(object value, Type type, CultureInfo cultureInfo) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);
public override object ChangeType(object value, Type type, CultureInfo? cultureInfo) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);

public sealed override void ReorderArgumentArray(ref object[] args, object state) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);
public sealed override void ReorderArgumentArray(ref object?[] args, object state) => throw new InvalidOperationException(SR.Arg_InvalidOperation_Reflection);

// Return any exact bindings that may exist. (This method is not defined on the
// Binder and is used by RuntimeType.)
public static MethodBase ExactBinding(MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
public static MethodBase? ExactBinding(MethodBase[] match, Type[] types, ParameterModifier[]? modifiers)
{
if (match == null)
throw new ArgumentNullException(nameof(match));
Expand Down Expand Up @@ -311,12 +313,12 @@ public static MethodBase ExactBinding(MethodBase[] match, Type[] types, Paramete

// Return any exact bindings that may exist. (This method is not defined on the
// Binder and is used by RuntimeType.)
public static PropertyInfo ExactPropertyBinding(PropertyInfo[] match, Type returnType, Type[] types, ParameterModifier[] modifiers)
public static PropertyInfo? ExactPropertyBinding(PropertyInfo[] match, Type? returnType, Type[]? types, ParameterModifier[]? modifiers)
{
if (match == null)
throw new ArgumentNullException(nameof(match));

PropertyInfo bestMatch = null;
PropertyInfo? bestMatch = null;
int typesLength = (types != null) ? types.Length : 0;
for (int i = 0; i < match.Length; i++)
{
Expand All @@ -327,7 +329,7 @@ public static PropertyInfo ExactPropertyBinding(PropertyInfo[] match, Type retur
Type pCls = par[j].ParameterType;

// If the classes exactly match continue
if (pCls != types[j])
if (pCls != types![j])
break;
}
if (j < typesLength)
Expand All @@ -343,9 +345,9 @@ public static PropertyInfo ExactPropertyBinding(PropertyInfo[] match, Type retur
return bestMatch;
}

private static int FindMostSpecific(ParameterInfo[] p1, int[] paramOrder1, Type paramArrayType1,
ParameterInfo[] p2, int[] paramOrder2, Type paramArrayType2,
Type[] types, object[] args)
private static int FindMostSpecific(ParameterInfo[] p1, int[] paramOrder1, Type? paramArrayType1,
ParameterInfo[] p2, int[] paramOrder2, Type? paramArrayType2,
Type[] types, object[]? args)
{
// A method using params is always less specific than one not using params
if (paramArrayType1 != null && paramArrayType2 == null) return 2;
Expand Down Expand Up @@ -450,22 +452,22 @@ private static int FindMostSpecificType(Type c1, Type c2, Type t)
{
if (c1.IsByRef && c2.IsByRef)
{
c1 = c1.GetElementType();
c2 = c2.GetElementType();
c1 = c1.GetElementType()!;
c2 = c2.GetElementType()!;
}
else if (c1.IsByRef)
{
if (c1.GetElementType() == c2)
return 2;

c1 = c1.GetElementType();
c1 = c1.GetElementType()!;
}
else
{
if (c2.GetElementType() == c1)
return 1;

c2 = c2.GetElementType();
c2 = c2.GetElementType()!;
}
}

Expand Down Expand Up @@ -494,9 +496,9 @@ private static int FindMostSpecificType(Type c1, Type c2, Type t)
}
}

private static int FindMostSpecificMethod(MethodBase m1, int[] paramOrder1, Type paramArrayType1,
MethodBase m2, int[] paramOrder2, Type paramArrayType2,
Type[] types, object[] args)
private static int FindMostSpecificMethod(MethodBase m1, int[] paramOrder1, Type? paramArrayType1,
MethodBase m2, int[] paramOrder2, Type? paramArrayType2,
Type[] types, object[]? args)
{
// Find the most specific method based on the parameters.
int res = FindMostSpecific(m1.GetParametersNoCopy(), paramOrder1, paramArrayType1,
Expand All @@ -510,8 +512,8 @@ private static int FindMostSpecificMethod(MethodBase m1, int[] paramOrder1, Type
if (CompareMethodSig(m1, m2))
{
// Determine the depth of the declaring types for both methods.
int hierarchyDepth1 = GetHierarchyDepth(m1.DeclaringType);
int hierarchyDepth2 = GetHierarchyDepth(m2.DeclaringType);
int hierarchyDepth1 = GetHierarchyDepth(m1.DeclaringType!);
int hierarchyDepth2 = GetHierarchyDepth(m2.DeclaringType!);

// The most derived method is the most specific one.
if (hierarchyDepth1 == hierarchyDepth2)
Expand All @@ -537,8 +539,8 @@ private static int FindMostSpecificProperty(PropertyInfo cur1, PropertyInfo cur2
// Check to see if the fields have the same name.
if (cur1.Name == cur2.Name)
{
int hierarchyDepth1 = GetHierarchyDepth(cur1.DeclaringType);
int hierarchyDepth2 = GetHierarchyDepth(cur2.DeclaringType);
int hierarchyDepth1 = GetHierarchyDepth(cur1.DeclaringType!);
int hierarchyDepth2 = GetHierarchyDepth(cur2.DeclaringType!);

if (hierarchyDepth1 == hierarchyDepth2)
{
Expand Down Expand Up @@ -576,7 +578,7 @@ private static int GetHierarchyDepth(Type t)
{
int depth = 0;

Type currentType = t;
Type? currentType = t;
do
{
depth++;
Expand All @@ -586,16 +588,16 @@ private static int GetHierarchyDepth(Type t)
return depth;
}

internal static MethodBase FindMostDerivedNewSlotMeth(MethodBase[] match, int cMatches)
internal static MethodBase? FindMostDerivedNewSlotMeth(MethodBase[] match, int cMatches)
{
int deepestHierarchy = 0;
MethodBase methWithDeepestHierarchy = null;
MethodBase? methWithDeepestHierarchy = null;

for (int i = 0; i < cMatches; i++)
{
// Calculate the depth of the hierarchy of the declaring type of the
// current method.
int currentHierarchyDepth = GetHierarchyDepth(match[i].DeclaringType);
int currentHierarchyDepth = GetHierarchyDepth(match[i].DeclaringType!);

// The two methods have the same name, signature, and hierarchy depth.
// This can only happen if at least one is vararg or generic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public abstract class MetadataAssemblyResolver
/// The MetadataLoadContext cannot consume assemblies from other MetadataLoadContexts or other type providers (such as the underlying runtime's own Reflection system.)
/// If a handler returns such an assembly, the MetadataLoadContext throws a FileLoadException.
/// </remarks>
public abstract Assembly Resolve(MetadataLoadContext context, AssemblyName assemblyName);
public abstract Assembly? Resolve(MetadataLoadContext context, AssemblyName assemblyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public sealed partial class MetadataLoadContext : IDisposable
/// <param name="coreAssemblyName">
/// The name of the assembly that contains the core types such as System.Object. Typically, this would be "mscorlib".
/// </param>
public MetadataLoadContext(MetadataAssemblyResolver resolver, string coreAssemblyName = null)
public MetadataLoadContext(MetadataAssemblyResolver resolver, string? coreAssemblyName = null)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));
Expand Down Expand Up @@ -251,7 +251,7 @@ public Assembly LoadFromAssemblyName(AssemblyName assemblyName)
/// type, the necessary constructor or any of the parameter types of the constructor, the MetadataLoadContext will not throw. It will omit the pseudo-custom
/// attribute from the list of returned attributes.
/// </summary>
public Assembly CoreAssembly
public Assembly? CoreAssembly
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed partial class MetadataLoadContext
private static readonly string[] s_CoreNames = { "mscorlib", "System.Runtime", "netstandard" };

// Cache loaded coreAssembly and core types.
internal RoAssembly TryGetCoreAssembly(string coreAssemblyName, out Exception e)
internal RoAssembly? TryGetCoreAssembly(string? coreAssemblyName, out Exception? e)
{
e = null;
Debug.Assert(_coreAssembly == null);
Expand All @@ -30,12 +30,12 @@ internal RoAssembly TryGetCoreAssembly(string coreAssemblyName, out Exception e)
return _coreAssembly;
}

private RoAssembly TryGetDefaultCoreAssembly(out Exception e)
private RoAssembly? TryGetDefaultCoreAssembly(out Exception? e)
{
foreach (string coreName in s_CoreNames)
{
RoAssemblyName roAssemblyName = new AssemblyName(coreName).ToRoAssemblyName();
RoAssembly roAssembly = TryResolveAssembly(roAssemblyName, out e);
RoAssembly? roAssembly = TryResolveAssembly(roAssemblyName, out e);

// Stop on the first core assembly we find
if (roAssembly != null)
Expand All @@ -49,7 +49,7 @@ private RoAssembly TryGetDefaultCoreAssembly(out Exception e)
return null;
}

private RoAssembly _coreAssembly = null;
private RoAssembly? _coreAssembly = null;

/// <summary>
/// Returns a lazily created and cached Type instance corresponding to the indicated core type. This method throws
Expand All @@ -59,16 +59,16 @@ private RoAssembly TryGetDefaultCoreAssembly(out Exception e)
internal RoType GetCoreType(CoreType coreType)
{
CoreTypes coreTypes = GetAllFoundCoreTypes();
RoType t = TryGetCoreType(coreType);
return t ?? throw coreTypes.GetException(coreType);
RoType? t = TryGetCoreType(coreType);
return t ?? throw coreTypes.GetException(coreType)!;
}

/// <summary>
/// Returns a lazily created and cached Type instance corresponding to the indicated core type. This method returns null
/// if the core assembly name wasn't supplied, the core assembly could not be loaded for some reason or if the specified
/// type does not exist in the core assembly.
/// </summary>
internal RoType TryGetCoreType(CoreType coreType)
internal RoType? TryGetCoreType(CoreType coreType)
{
CoreTypes coreTypes = GetAllFoundCoreTypes();
return coreTypes[coreType];
Expand All @@ -87,6 +87,6 @@ internal RoType TryGetCoreType(CoreType coreType)
// one reason, we have to instance it per MetadataLoadContext.
//
internal Binder GetDefaultBinder() => _lazyDefaultBinder ?? (_lazyDefaultBinder = new DefaultBinder(this));
private volatile Binder _lazyDefaultBinder;
private volatile Binder? _lazyDefaultBinder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private void Dispose(bool disposing)
ConcurrentBag<IDisposable> disposables = _disposables;
if (disposables != null)
{
_disposables = null;
_disposables = null!;

foreach (IDisposable disposable in disposables)
{
Expand Down
Loading

0 comments on commit b27a4b3

Please sign in to comment.