Browse Source

Fix #1281: Fix CallBuilder: use expanded form only if overload resolution allows it.

pull/1317/head
Siegfried Pammer 7 years ago
parent
commit
c9b74865fc
  1. 19
      ICSharpCode.Decompiler.Tests/TestCases/Correctness/OverloadResolution.cs
  2. 18
      ICSharpCode.Decompiler/CSharp/CallBuilder.cs

19
ICSharpCode.Decompiler.Tests/TestCases/Correctness/OverloadResolution.cs

@ -32,6 +32,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness @@ -32,6 +32,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
Generics();
ConstructorTest();
TestIndexer();
Issue1281();
}
#region ConstructorTest
@ -86,6 +87,24 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness @@ -86,6 +87,24 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{
Console.WriteLine("TestCall with params: " + (p2 == null ? "null" : p2.Length.ToString()));
}
static void Issue1281()
{
var arg = new object[0];
TestCallIssue1281(arg);
TestCallIssue1281((object)arg);
TestCallIssue1281(new[] { arg });
}
static void TestCallIssue1281(params object[] args)
{
Console.Write("TestCallIssue1281: count = " + args.Length + ": ");
foreach (var arg in args) {
Console.Write(arg);
Console.Write(", ");
}
Console.WriteLine();
}
#endregion
#region Simple Overloaded Method

18
ICSharpCode.Decompiler/CSharp/CallBuilder.cs

@ -405,7 +405,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -405,7 +405,7 @@ namespace ICSharpCode.Decompiler.CSharp
return new ExpressionWithResolveResult(((AssignmentExpression)assignment).Left.Detach());
}
private bool IsInterpolatedStringCreation(IMethod method)
private static bool IsInterpolatedStringCreation(IMethod method)
{
return method.IsStatic && (
(method.DeclaringType.IsKnownType(KnownTypeCode.String) && method.Name == "Format") ||
@ -643,7 +643,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -643,7 +643,10 @@ namespace ICSharpCode.Decompiler.CSharp
expandedArguments.Add(expressionBuilder.GetDefaultValueExpression(elementType).WithoutILInstruction());
}
}
if (IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, Empty<IType>.Array, expandedArguments, null, -1, out _) == OverloadResolutionErrors.None) {
if (IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, Empty<IType>.Array,
expandedArguments, argumentNames: null, firstOptionalArgumentIndex: -1, out _,
out var bestCandidateIsExpandedForm) == OverloadResolutionErrors.None && bestCandidateIsExpandedForm)
{
expectedParameters = expandedParameters;
arguments = expandedArguments.SelectList(a => new TranslatedExpression(a.Expression.Detach()));
return true;
@ -758,7 +761,8 @@ namespace ICSharpCode.Decompiler.CSharp @@ -758,7 +761,8 @@ namespace ICSharpCode.Decompiler.CSharp
bool argumentsCasted = false;
OverloadResolutionErrors errors;
while ((errors = IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, typeArguments,
argumentList.Arguments, argumentList.ArgumentNames, argumentList.FirstOptionalArgumentIndex, out foundMethod)) != OverloadResolutionErrors.None)
argumentList.Arguments, argumentList.ArgumentNames, argumentList.FirstOptionalArgumentIndex, out foundMethod,
out var bestCandidateIsExpandedForm)) != OverloadResolutionErrors.None && bestCandidateIsExpandedForm == argumentList.IsExpandedForm)
{
switch (errors) {
case OverloadResolutionErrors.TypeInferenceFailed:
@ -923,9 +927,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -923,9 +927,10 @@ namespace ICSharpCode.Decompiler.CSharp
OverloadResolutionErrors IsUnambiguousCall(ExpectedTargetDetails expectedTargetDetails, IMethod method,
ResolveResult target, IType[] typeArguments, IList<TranslatedExpression> arguments,
string[] argumentNames, int firstOptionalArgumentIndex,
out IParameterizedMember foundMember)
out IParameterizedMember foundMember, out bool bestCandidateIsExpandedForm)
{
foundMember = null;
bestCandidateIsExpandedForm = false;
var lookup = new MemberLookup(resolver.CurrentTypeDefinition, resolver.CurrentTypeDefinition.ParentModule);
var or = new OverloadResolution(resolver.Compilation,
firstOptionalArgumentIndex < 0 ? arguments.SelectArray(a => a.ResolveResult) : arguments.Take(firstOptionalArgumentIndex).Select(a => a.ResolveResult).ToArray(),
@ -964,6 +969,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -964,6 +969,7 @@ namespace ICSharpCode.Decompiler.CSharp
return OverloadResolutionErrors.AmbiguousMatch;
or.AddMethodLists(result.MethodsGroupedByDeclaringType.ToArray());
}
bestCandidateIsExpandedForm = or.BestCandidateIsExpandedForm;
if (or.BestCandidateErrors != OverloadResolutionErrors.None)
return or.BestCandidateErrors;
if (or.IsAmbiguous)
@ -1132,7 +1138,9 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1132,7 +1138,9 @@ namespace ICSharpCode.Decompiler.CSharp
));
} else {
while (IsUnambiguousCall(expectedTargetDetails, method, null, Empty<IType>.Array, argumentList.Arguments,
argumentList.ArgumentNames, argumentList.FirstOptionalArgumentIndex, out _) != OverloadResolutionErrors.None) {
argumentList.ArgumentNames, argumentList.FirstOptionalArgumentIndex, out _,
out var bestCandidateIsExpandedForm) != OverloadResolutionErrors.None && bestCandidateIsExpandedForm == argumentList.IsExpandedForm)
{
if (argumentList.FirstOptionalArgumentIndex >= 0) {
argumentList.FirstOptionalArgumentIndex = -1;
continue;

Loading…
Cancel
Save