diff --git a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
index 24db1b7..cd916b9 100644
--- a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
+++ b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
@@ -21,7 +21,7 @@ internal static class MethodResolver
/// The argument types used to locate a matching method.
///
/// The method that best matches the argument types, or if no candidate
- /// accepts the argument types or several candidates match them equally well.
+ /// accepts the argument types or none of them matches better than all the others.
///
public static MethodBase? ResolveMethod(IReadOnlyList match, Type[] argumentTypes)
{
@@ -199,10 +199,17 @@ internal static class MethodResolver
if (currentIndex == 1)
return candidates[0];
+ //
+ // As in C#, the method to invoke must be more specific than every other candidate.
+ // Such a method wins each comparison, so the walk below always ends on it. A candidate
+ // that ties with the current winner does not stop the walk, since a later candidate
+ // can be more specific than both.
+ //
var min = 0;
+ var ambiguous = false;
+
for (var i = 1; i < currentIndex; i++)
{
- // Walk all of the methods looking the most specific method to invoke
var newMin = FindMostSpecificMethod(
candidates[min],
parameterArrayTypes[min],
@@ -210,12 +217,35 @@ internal static class MethodResolver
parameterArrayTypes[i],
argumentTypes);
- // Ambiguous match found
if (newMin == 0)
- return null;
-
- if (newMin == 2)
+ {
+ ambiguous = true;
+ }
+ else if (newMin == 2)
+ {
min = i;
+ ambiguous = false;
+ }
+ }
+
+ if (ambiguous)
+ return null;
+
+ //
+ // The candidates before the winner may not have been compared with it,
+ // so check that the winner is also more specific than each of them.
+ //
+ for (var i = 0; i < min; i++)
+ {
+ var result = FindMostSpecificMethod(
+ candidates[min],
+ parameterArrayTypes[min],
+ candidates[i],
+ parameterArrayTypes[i],
+ argumentTypes);
+
+ if (result != 1)
+ return null;
}
return candidates[min];
diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
index ce8b7b4..8fa0a9c 100644
--- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
+++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
@@ -388,6 +388,21 @@ public void Parse_OverloadsWithDefaultArguments_ReturnsAmbiguityError(string exp
Assert.That(result.ErrorMessage, Does.StartWith("Ambiguous match found"));
}
+ [TestCase("Later(Words)", "string[]")]
+ [TestCase("string.Concat(Words)", "ab")]
+ public void Evaluate_AmbiguousPairBeforeBestOverload_SelectsBestOverload(string expression, string expected) =>
+ Assert.That(ExpressionParser.Evaluate(expression, new OverloadContext()), Is.EqualTo(expected));
+
+ [Test]
+ public void Parse_NoOverloadBetterThanAllOthers_ReturnsAmbiguityError()
+ {
+ var parameter = Expression.Parameter(typeof(OverloadContext), "c");
+ var result = ExpressionParser.Parse("""NoBest("a", "b")""", new ContextAwareBinder(parameter));
+
+ Assert.That(result.Success, Is.False);
+ Assert.That(result.ErrorMessage, Does.StartWith("Ambiguous match found"));
+ }
+
[Test]
public void Parse_ByteArgumentForCharParameter_ReturnsError()
{
@@ -827,6 +842,23 @@ public class OverloadContext
public static string ExpandedBothDefaults(int a, int b = 0, params int[] rest) => "(int, int = 0, params int[])";
public static string ExpandedBothDefaults(int a, int b = 0, int c = 0, params int[] rest) => "(int, int = 0, int = 0, params int[])";
+
+ public string[] Words { get; } = ["a", "b"];
+
+ //
+ // The first two overloads are ambiguous for a string array, and the last one is better than both.
+ //
+ public static string Later(object[] _) => "object[]";
+ public static string Later(IEnumerable _) => "IEnumerable";
+ public static string Later(string[] _) => "string[]";
+
+ //
+ // The last overload is better than the first one, but neither it nor any other overload is better
+ // than all the others, so a call with two strings is ambiguous.
+ //
+ public static string NoBest(object a, string b) => "(object, string)";
+ public static string NoBest(string a, object b) => "(string, object)";
+ public static string NoBest(IComparable a, string b) => "(IComparable, string)";
}
public class IndexerContext