From d449ba6158160b5907ec0806aef5fb87fda7d9b9 Mon Sep 17 00:00:00 2001 From: rameel Date: Fri, 20 Nov 2026 01:24:03 +0500 Subject: [PATCH] binding: do not stop overload resolution at the first ambiguous pair MethodResolver compared each candidate with the best one so far and returned null as soon as two candidates were equally specific, even if a later candidate was more specific than both. For a string[] argument, string.Concat(arr) was reported as an ambiguous match: Concat(params object[]) and Concat(IEnumerable) are equally specific for it, although Concat(params string[]) is better than both. Remember the ambiguity and continue, as System.DefaultBinder does. Unlike DefaultBinder, also check that the selected method is more specific than the candidates before it, which it may not have been compared with. As in C#, a call with no method better than all the others stays ambiguous. --- .../Internal/MethodResolver.cs | 42 ++++++++++++++++--- .../ExpressionParserTest.cs | 32 ++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) 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