Skip to content

Commit 1d964bc

Browse files
ds5678siegfriedpammer
authored andcommitted
Fix regression in decompiling local functions with default parameters
1 parent cce3e3b commit 1d964bc

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
<Compile Include="TestCases\ILPretty\Issue3552.cs" />
155155
<Compile Include="TestCases\Pretty\ExpandParamsArgumentsDisabled.cs" />
156156
<Compile Include="TestCases\Pretty\ExtensionProperties.cs" />
157+
<Compile Include="TestCases\Pretty\Issue3541.cs" />
157158
<None Include="TestCases\ILPretty\Issue3504.cs" />
158159
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
159160
<Compile Include="TestCases\Pretty\Comparisons.cs" />

ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ public void AllFilesHaveTests()
133133
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest,
134134
};
135135

136+
static readonly CompilerOptions[] roslyn4OrNewerWithNet40Options =
137+
{
138+
CompilerOptions.UseRoslynLatest | CompilerOptions.TargetNet40,
139+
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest | CompilerOptions.TargetNet40,
140+
CompilerOptions.UseRoslynLatest,
141+
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest,
142+
};
143+
136144
static readonly CompilerOptions[] roslyn4OrNewerOptions =
137145
{
138146
CompilerOptions.UseRoslynLatest,
@@ -664,6 +672,12 @@ public async Task Issue3483([ValueSource(nameof(defaultOptions))] CompilerOption
664672
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.CheckForOverflowUnderflow, configureDecompiler: settings => settings.CheckForOverflowUnderflow = true);
665673
}
666674

675+
[Test]
676+
public async Task Issue3541([ValueSource(nameof(roslyn4OrNewerWithNet40Options))] CompilerOptions cscOptions)
677+
{
678+
await RunForLibrary(cscOptions: cscOptions);
679+
}
680+
667681
[Test]
668682
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
669683
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
2+
{
3+
internal class Issue3541
4+
{
5+
private void Test(string format)
6+
{
7+
TestLocal();
8+
9+
void TestLocal(int a = 0)
10+
{
11+
a.ToString(format);
12+
}
13+
}
14+
}
15+
}

ICSharpCode.Decompiler/IL/Transforms/LocalFunctionDecompiler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,18 @@ static T FindCommonAncestorInstruction<T>(ILInstruction a, ILInstruction b)
566566
}
567567

568568
internal static bool IsClosureParameter(IParameter parameter, ITypeResolveContext context)
569+
{
570+
return IsClosureParameter(parameter, context.CurrentTypeDefinition);
571+
}
572+
573+
internal static bool IsClosureParameter(IParameter parameter, ITypeDefinition currentTypeDefinition)
569574
{
570575
if (parameter.Type is not ByReferenceType brt)
571576
return false;
572577
var type = brt.ElementType.GetDefinition();
573578
return type != null
574579
&& type.Kind == TypeKind.Struct
575-
&& TransformDisplayClassUsage.IsPotentialClosure(context.CurrentTypeDefinition, type);
580+
&& TransformDisplayClassUsage.IsPotentialClosure(currentTypeDefinition, type);
576581
}
577582

578583
LocalFunctionMethod ReduceToLocalFunction(IMethod method, int typeParametersToRemove)

ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Linq;
2222
using System.Reflection.Metadata;
2323

24+
using ICSharpCode.Decompiler.IL.Transforms;
2425
using ICSharpCode.Decompiler.Metadata;
2526
using ICSharpCode.Decompiler.Semantics;
2627
using ICSharpCode.Decompiler.TypeSystem.Implementation;
@@ -681,6 +682,9 @@ public static bool IsDefaultValueAssignmentAllowed(this IParameter parameter)
681682
if (otherParameter == parameter)
682683
break;
683684

685+
if (LocalFunctionDecompiler.IsClosureParameter(otherParameter, otherParameter.Owner.DeclaringTypeDefinition))
686+
continue;
687+
684688
if (DefaultValueAssignmentAllowedIndividual(otherParameter) || otherParameter.IsParams)
685689
continue;
686690

0 commit comments

Comments
 (0)