Skip to content

Commit 0bc3670

Browse files
committed
added nullability annotations to methodbinder.cs
1 parent 5266dc4 commit 0bc3670

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/runtime/methodbinder.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal void AddMethod(MethodBase m)
5454
/// Given a sequence of MethodInfo and a sequence of types, return the
5555
/// MethodInfo that matches the signature represented by those types.
5656
/// </summary>
57-
internal static MethodInfo MatchSignature(MethodInfo[] mi, Type[] tp)
57+
internal static MethodInfo? MatchSignature(MethodInfo[] mi, Type[] tp)
5858
{
5959
if (tp == null)
6060
{
@@ -88,7 +88,7 @@ internal static MethodInfo MatchSignature(MethodInfo[] mi, Type[] tp)
8888
/// return the MethodInfo that represents the matching closed generic.
8989
/// If unsuccessful, returns null and may set a Python error.
9090
/// </summary>
91-
internal static MethodInfo MatchParameters(MethodInfo[] mi, Type[] tp)
91+
internal static MethodInfo? MatchParameters(MethodInfo[] mi, Type[]? tp)
9292
{
9393
if (tp == null)
9494
{
@@ -127,7 +127,7 @@ internal static MethodInfo MatchParameters(MethodInfo[] mi, Type[] tp)
127127
/// Given a sequence of MethodInfo and two sequences of type parameters,
128128
/// return the MethodInfo that matches the signature and the closed generic.
129129
/// </summary>
130-
internal static MethodInfo MatchSignatureAndParameters(MethodInfo[] mi, Type[] genericTp, Type[] sigTp)
130+
internal static MethodInfo? MatchSignatureAndParameters(MethodInfo[] mi, Type[] genericTp, Type[] sigTp)
131131
{
132132
if (genericTp == null || sigTp == null)
133133
{
@@ -300,7 +300,7 @@ internal static int ArgPrecedence(Type t)
300300
/// <param name="args">The Python arguments.</param>
301301
/// <param name="kw">The Python keyword arguments.</param>
302302
/// <returns>A Binding if successful. Otherwise null.</returns>
303-
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
303+
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
304304
{
305305
return Bind(inst, args, kw, null, null);
306306
}
@@ -316,14 +316,14 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
316316
/// <param name="kw">The Python keyword arguments.</param>
317317
/// <param name="info">If not null, only bind to that method.</param>
318318
/// <returns>A Binding if successful. Otherwise null.</returns>
319-
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
319+
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info)
320320
{
321321
return Bind(inst, args, kw, info, null);
322322
}
323323

324324
private readonly struct MatchedMethod
325325
{
326-
public MatchedMethod(int kwargsMatched, int defaultsNeeded, object[] margs, int outs, MethodBase mb)
326+
public MatchedMethod(int kwargsMatched, int defaultsNeeded, object?[] margs, int outs, MethodBase mb)
327327
{
328328
KwargsMatched = kwargsMatched;
329329
DefaultsNeeded = defaultsNeeded;
@@ -334,7 +334,7 @@ public MatchedMethod(int kwargsMatched, int defaultsNeeded, object[] margs, int
334334

335335
public int KwargsMatched { get; }
336336
public int DefaultsNeeded { get; }
337-
public object[] ManagedArgs { get; }
337+
public object?[] ManagedArgs { get; }
338338
public int Outs { get; }
339339
public MethodBase Method { get; }
340340
}
@@ -363,11 +363,9 @@ public MismatchedMethod(Exception exception, MethodBase mb)
363363
/// <param name="info">If not null, only bind to that method.</param>
364364
/// <param name="methodinfo">If not null, additionally attempt to bind to the generic methods in this array by inferring generic type parameters.</param>
365365
/// <returns>A Binding if successful. Otherwise null.</returns>
366-
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodInfo[] methodinfo)
366+
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodInfo[]? methodinfo)
367367
{
368368
// loop to find match, return invoker w/ or w/o error
369-
MethodBase[] _methods = null;
370-
371369
var kwargDict = new Dictionary<string, PyObject>();
372370
if (kw != null)
373371
{
@@ -384,6 +382,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
384382

385383
var pynargs = (int)Runtime.PyTuple_Size(args);
386384
var isGeneric = false;
385+
386+
MethodBase[] _methods;
387387
if (info != null)
388388
{
389389
_methods = new MethodBase[1];
@@ -405,7 +405,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
405405
isGeneric = true;
406406
}
407407
ParameterInfo[] pi = mi.GetParameters();
408-
ArrayList defaultArgList;
408+
ArrayList? defaultArgList;
409409
bool paramsArray;
410410
int kwargsMatched;
411411
int defaultsNeeded;
@@ -447,7 +447,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
447447
{
448448
bool isUnary = pynargs == 0;
449449
// Postprocessing to extend margs.
450-
var margsTemp = isUnary ? new object[1] : new object[2];
450+
var margsTemp = isUnary ? new object?[1] : new object?[2];
451451
// If reverse, the bound instance is the right operand.
452452
int boundOperandIndex = isReverse ? 1 : 0;
453453
// If reverse, the passed instance is the left operand.
@@ -512,7 +512,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
512512
var outs = bestMatch.Outs;
513513
var mi = bestMatch.Method;
514514

515-
object target = null;
515+
object? target = null;
516516
if (!mi.IsStatic && inst != null)
517517
{
518518
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
@@ -540,8 +540,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
540540
// is a generic method and info is null. That happens when a generic
541541
// method was not called using the [] syntax. Let's introspect the
542542
// type of the arguments and use it to construct the correct method.
543-
Type[] types = Runtime.PythonArgsToTypeArray(args, true);
544-
MethodInfo mi = MatchParameters(methodinfo, types);
543+
Type[]? types = Runtime.PythonArgsToTypeArray(args, true);
544+
MethodInfo? mi = MatchParameters(methodinfo, types);
545545
if (mi != null)
546546
{
547547
return Bind(inst, args, kw, mi, null);
@@ -605,14 +605,14 @@ static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStar
605605
/// <param name="needsResolution"><c>true</c>, if overloading resolution is required</param>
606606
/// <param name="outs">Returns number of output parameters</param>
607607
/// <returns>If successful, an array of .NET arguments that can be passed to the method. Otherwise null.</returns>
608-
static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
608+
static object?[]? TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
609609
BorrowedReference args, int pyArgCount,
610610
Dictionary<string, PyObject> kwargDict,
611-
ArrayList defaultArgList,
611+
ArrayList? defaultArgList,
612612
out int outs)
613613
{
614614
outs = 0;
615-
var margs = new object[pi.Length];
615+
var margs = new object?[pi.Length];
616616
int arrayStart = paramsArray ? pi.Length - 1 : -1;
617617

618618
for (int paramIndex = 0; paramIndex < pi.Length; paramIndex++)
@@ -634,7 +634,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
634634
NewReference tempObject = default;
635635
if (hasNamedParam)
636636
{
637-
op = kwargDict[parameter.Name];
637+
op = kwargDict[parameter.Name!];
638638
}
639639
else
640640
{
@@ -676,7 +676,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
676676
/// <param name="isOut">Whether the CLR type is passed by reference.</param>
677677
/// <returns>true on success</returns>
678678
static bool TryConvertArgument(BorrowedReference op, Type parameterType,
679-
out object arg, out bool isOut)
679+
out object? arg, out bool isOut)
680680
{
681681
arg = null;
682682
isOut = false;
@@ -701,12 +701,12 @@ static bool TryConvertArgument(BorrowedReference op, Type parameterType,
701701
/// <param name="parameterType">The parameter's managed type.</param>
702702
/// <param name="argument">Pointer to the Python argument object.</param>
703703
/// <returns>null if conversion is not possible</returns>
704-
static Type TryComputeClrArgumentType(Type parameterType, BorrowedReference argument)
704+
static Type? TryComputeClrArgumentType(Type parameterType, BorrowedReference argument)
705705
{
706706
// this logic below handles cases when multiple overloading methods
707707
// are ambiguous, hence comparison between Python and CLR types
708708
// is necessary
709-
Type clrtype = null;
709+
Type? clrtype = null;
710710

711711
if (clrtype != null)
712712
{
@@ -773,7 +773,7 @@ static Type TryComputeClrArgumentType(Type parameterType, BorrowedReference argu
773773
static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] parameters,
774774
Dictionary<string, PyObject> kwargDict,
775775
out bool paramsArray,
776-
out ArrayList defaultArgList,
776+
out ArrayList? defaultArgList,
777777
out int kwargsMatched,
778778
out int defaultsNeeded)
779779
{
@@ -834,7 +834,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
834834
return Invoke(inst, args, kw, null, null);
835835
}
836836

837-
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
837+
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info)
838838
{
839839
return Invoke(inst, args, kw, info, null);
840840
}
@@ -872,7 +872,7 @@ protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference ar
872872
to.Append(')');
873873
}
874874

875-
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodInfo[] methodinfo)
875+
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodInfo[]? methodinfo)
876876
{
877877
// No valid methods, nothing to bind.
878878
if (GetMethods().Length == 0)
@@ -885,7 +885,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
885885
return Exceptions.RaiseTypeError(msg.ToString());
886886
}
887887

888-
Binding binding = Bind(inst, args, kw, info, methodinfo);
888+
Binding? binding = Bind(inst, args, kw, info, methodinfo);
889889
object result;
890890
IntPtr ts = IntPtr.Zero;
891891

@@ -1041,11 +1041,11 @@ int IComparer<MaybeMethodBase>.Compare(MaybeMethodBase m1, MaybeMethodBase m2)
10411041
internal class Binding
10421042
{
10431043
public MethodBase info;
1044-
public object[] args;
1045-
public object inst;
1044+
public object?[] args;
1045+
public object? inst;
10461046
public int outs;
10471047

1048-
internal Binding(MethodBase info, object inst, object[] args, int outs)
1048+
internal Binding(MethodBase info, object? inst, object?[] args, int outs)
10491049
{
10501050
this.info = info;
10511051
this.inst = inst;
@@ -1057,7 +1057,7 @@ internal Binding(MethodBase info, object inst, object[] args, int outs)
10571057

10581058
static internal class ParameterInfoExtensions
10591059
{
1060-
public static object GetDefaultValue(this ParameterInfo parameterInfo)
1060+
public static object? GetDefaultValue(this ParameterInfo parameterInfo)
10611061
{
10621062
// parameterInfo.HasDefaultValue is preferable but doesn't exist in .NET 4.0
10631063
bool hasDefaultValue = (parameterInfo.Attributes & ParameterAttributes.HasDefault) ==

0 commit comments

Comments
 (0)