@@ -546,7 +546,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
546
546
var margs = new object [ clrArgCount ] ;
547
547
548
548
int paramsArrayIndex = paramsArray ? pi . Length - 1 : - 1 ; // -1 indicates no paramsArray
549
- var usedImplicitConversion = false ;
549
+ int implicitConversions = 0 ;
550
550
var kwargsMatched = 0 ;
551
551
552
552
// Conversion loop for each parameter
@@ -658,18 +658,26 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
658
658
else if ( matches . Count == 0 )
659
659
{
660
660
// accepts non-decimal numbers in decimal parameters
661
- if ( underlyingType == typeof ( decimal ) )
661
+ if ( underlyingType == typeof ( decimal ) || underlyingType == typeof ( double ) )
662
662
{
663
663
clrtype = parameter . ParameterType ;
664
- usedImplicitConversion |= typematch = Converter . ToManaged ( op , clrtype , out arg , false ) ;
664
+ typematch = Converter . ToManaged ( op , clrtype , out arg , false ) ;
665
+ if ( typematch )
666
+ {
667
+ implicitConversions ++ ;
668
+ }
665
669
}
666
670
if ( ! typematch )
667
671
{
668
672
// this takes care of implicit conversions
669
673
var opImplicit = parameter . ParameterType . GetMethod ( "op_Implicit" , new [ ] { clrtype } ) ;
670
674
if ( opImplicit != null )
671
675
{
672
- usedImplicitConversion |= typematch = opImplicit . ReturnType == parameter . ParameterType ;
676
+ typematch = opImplicit . ReturnType == parameter . ParameterType ;
677
+ if ( typematch )
678
+ {
679
+ implicitConversions ++ ;
680
+ }
673
681
clrtype = parameter . ParameterType ;
674
682
}
675
683
}
@@ -739,13 +747,10 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
739
747
}
740
748
}
741
749
742
- var match = new MatchedMethod ( kwargsMatched , margs , outs , methodInformation ) ;
743
- if ( usedImplicitConversion )
750
+ var match = new MatchedMethod ( kwargsMatched , margs , outs , methodInformation , implicitConversions ) ;
751
+ if ( implicitConversions > 0 )
744
752
{
745
- if ( matchesUsingImplicitConversion == null )
746
- {
747
- matchesUsingImplicitConversion = new List < MatchedMethod > ( ) ;
748
- }
753
+ matchesUsingImplicitConversion ??= new List < MatchedMethod > ( ) ;
749
754
matchesUsingImplicitConversion . Add ( match ) ;
750
755
}
751
756
else
@@ -767,11 +772,22 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
767
772
// we solve the ambiguity by taking the one with the highest precedence but only
768
773
// considering the actual arguments passed, ignoring the optional arguments for
769
774
// which the default values were used
770
- var bestMatch = matchesTouse
771
- . GroupBy ( x => x . KwargsMatched )
772
- . OrderByDescending ( x => x . Key )
773
- . First ( )
774
- . MinBy ( x => GetMatchedArgumentsPrecedence ( x . MethodInformation , pyArgCount , kwArgDict ? . Keys ) ) ;
775
+ MatchedMethod bestMatch ;
776
+ if ( matchesTouse . Count == 1 )
777
+ {
778
+ bestMatch = matchesTouse [ 0 ] ;
779
+ }
780
+ else
781
+ {
782
+ bestMatch = matchesTouse
783
+ . GroupBy ( x => x . KwargsMatched )
784
+ . OrderByDescending ( x => x . Key )
785
+ . First ( )
786
+ . GroupBy ( x => x . ImplicitOperations )
787
+ . OrderBy ( x => x . Key )
788
+ . First ( )
789
+ . MinBy ( x => GetMatchedArgumentsPrecedence ( x . MethodInformation , pyArgCount , kwArgDict ? . Keys ) ) ;
790
+ }
775
791
776
792
var margs = bestMatch . ManagedArgs ;
777
793
var outs = bestMatch . Outs ;
@@ -1135,15 +1151,17 @@ private readonly struct MatchedMethod
1135
1151
public int KwargsMatched { get ; }
1136
1152
public object ? [ ] ManagedArgs { get ; }
1137
1153
public int Outs { get ; }
1154
+ public int ImplicitOperations { get ; }
1138
1155
public MethodInformation MethodInformation { get ; }
1139
1156
public MethodBase Method => MethodInformation . MethodBase ;
1140
1157
1141
- public MatchedMethod ( int kwargsMatched , object ? [ ] margs , int outs , MethodInformation methodInformation )
1158
+ public MatchedMethod ( int kwargsMatched , object ? [ ] margs , int outs , MethodInformation methodInformation , int implicitOperations )
1142
1159
{
1143
1160
KwargsMatched = kwargsMatched ;
1144
1161
ManagedArgs = margs ;
1145
1162
Outs = outs ;
1146
1163
MethodInformation = methodInformation ;
1164
+ ImplicitOperations = implicitOperations ;
1147
1165
}
1148
1166
}
1149
1167
0 commit comments