Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static class MethodResolver
/// <param name="argumentTypes">The argument types used to locate a matching method.</param>
/// <returns>
/// The method that best matches the argument types, or <see langword="null" /> 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.
/// </returns>
public static MethodBase? ResolveMethod(IReadOnlyList<MethodBase> match, Type[] argumentTypes)
{
Expand Down Expand Up @@ -199,23 +199,53 @@ 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],
candidates[i],
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];
Expand Down
32 changes: 32 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<string> _) => "IEnumerable<string>";
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
Expand Down