Browse Source

Fix #2165: DeclareVariables step must update ResolveResult annotation when using out var.

pull/3529/head
Siegfried Pammer 5 months ago
parent
commit
e56f12cf93
  1. 44
      ICSharpCode.Decompiler.Tests/TestCases/Correctness/OverloadResolution.cs
  2. 8
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

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

@ -42,6 +42,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
#endif #endif
Issue2444.M2(); Issue2444.M2();
Issue2741.B.Test(new Issue2741.C()); Issue2741.B.Test(new Issue2741.C());
ExtensionMethodDemo.Issue2165.Test();
} }
#region ConstructorTest #region ConstructorTest
@ -550,3 +551,46 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
} }
} }
} }
namespace ExtensionMethodDemo
{
// First extension class with an out int parameter
public static class StringExtensions
{
public static bool TryParseCustom(this string input, out int result)
{
return int.TryParse(input, out result);
}
}
// Second extension class with an out double parameter
public static class StringDoubleExtensions
{
public static bool TryParseCustom(this string input, out double result)
{
return double.TryParse(input, out result);
}
}
class Issue2165
{
public static void Test()
{
string value1 = "123";
string value2 = "123.45";
#if CS70
// Use the int version with extension method syntax
if (value1.TryParseCustom(out int intResult))
{
Console.WriteLine("Parsed int: " + intResult);
}
// Use the double version with extension method syntax
if (value2.TryParseCustom(out double doubleResult))
{
Console.WriteLine("Parsed double: " + doubleResult);
}
#endif
}
}
}

8
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -622,13 +622,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
// 'T v; SomeCall(out v);' can be combined to 'SomeCall(out T v);' // 'T v; SomeCall(out v);' can be combined to 'SomeCall(out T v);'
AstType type; AstType type;
bool isOutVar = false;
if (context.Settings.AnonymousTypes && v.Type.ContainsAnonymousType()) if (context.Settings.AnonymousTypes && v.Type.ContainsAnonymousType())
{ {
type = new SimpleType("var"); type = new SimpleType("var");
isOutVar = true;
} }
else if (dirExpr.Annotation<UseImplicitlyTypedOutAnnotation>() != null) else if (dirExpr.Annotation<UseImplicitlyTypedOutAnnotation>() != null)
{ {
type = new SimpleType("var"); type = new SimpleType("var");
isOutVar = true;
} }
else else
{ {
@ -652,6 +655,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var ovd = new OutVarDeclarationExpression(type, name); var ovd = new OutVarDeclarationExpression(type, name);
ovd.Variable.AddAnnotation(new ILVariableResolveResult(ilVariable)); ovd.Variable.AddAnnotation(new ILVariableResolveResult(ilVariable));
ovd.CopyAnnotationsFrom(dirExpr); ovd.CopyAnnotationsFrom(dirExpr);
if (isOutVar)
{
ovd.RemoveAnnotations<ResolveResult>();
ovd.AddAnnotation(new OutVarResolveResult(v.Type));
}
replacements.Add((dirExpr, ovd)); replacements.Add((dirExpr, ovd));
} }
else else

Loading…
Cancel
Save