Browse Source

Fix regression in decompiling local functions with default parameters

pull/3560/head
ds5678 10 months ago committed by Siegfried Pammer
parent
commit
1d964bce8e
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 14
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 15
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3541.cs
  4. 7
      ICSharpCode.Decompiler/IL/Transforms/LocalFunctionDecompiler.cs
  5. 4
      ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

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

@ -154,6 +154,7 @@
<Compile Include="TestCases\ILPretty\Issue3552.cs" /> <Compile Include="TestCases\ILPretty\Issue3552.cs" />
<Compile Include="TestCases\Pretty\ExpandParamsArgumentsDisabled.cs" /> <Compile Include="TestCases\Pretty\ExpandParamsArgumentsDisabled.cs" />
<Compile Include="TestCases\Pretty\ExtensionProperties.cs" /> <Compile Include="TestCases\Pretty\ExtensionProperties.cs" />
<Compile Include="TestCases\Pretty\Issue3541.cs" />
<None Include="TestCases\ILPretty\Issue3504.cs" /> <None Include="TestCases\ILPretty\Issue3504.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" /> <Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" /> <Compile Include="TestCases\Pretty\Comparisons.cs" />

14
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -133,6 +133,14 @@ namespace ICSharpCode.Decompiler.Tests
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest, CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest,
}; };
static readonly CompilerOptions[] roslyn4OrNewerWithNet40Options =
{
CompilerOptions.UseRoslynLatest | CompilerOptions.TargetNet40,
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest | CompilerOptions.TargetNet40,
CompilerOptions.UseRoslynLatest,
CompilerOptions.Optimize | CompilerOptions.UseRoslynLatest,
};
static readonly CompilerOptions[] roslyn4OrNewerOptions = static readonly CompilerOptions[] roslyn4OrNewerOptions =
{ {
CompilerOptions.UseRoslynLatest, CompilerOptions.UseRoslynLatest,
@ -664,6 +672,12 @@ namespace ICSharpCode.Decompiler.Tests
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.CheckForOverflowUnderflow, configureDecompiler: settings => settings.CheckForOverflowUnderflow = true); await RunForLibrary(cscOptions: cscOptions | CompilerOptions.CheckForOverflowUnderflow, configureDecompiler: settings => settings.CheckForOverflowUnderflow = true);
} }
[Test]
public async Task Issue3541([ValueSource(nameof(roslyn4OrNewerWithNet40Options))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}
[Test] [Test]
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{ {

15
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3541.cs

@ -0,0 +1,15 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class Issue3541
{
private void Test(string format)
{
TestLocal();
void TestLocal(int a = 0)
{
a.ToString(format);
}
}
}
}

7
ICSharpCode.Decompiler/IL/Transforms/LocalFunctionDecompiler.cs

@ -566,13 +566,18 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
internal static bool IsClosureParameter(IParameter parameter, ITypeResolveContext context) internal static bool IsClosureParameter(IParameter parameter, ITypeResolveContext context)
{
return IsClosureParameter(parameter, context.CurrentTypeDefinition);
}
internal static bool IsClosureParameter(IParameter parameter, ITypeDefinition currentTypeDefinition)
{ {
if (parameter.Type is not ByReferenceType brt) if (parameter.Type is not ByReferenceType brt)
return false; return false;
var type = brt.ElementType.GetDefinition(); var type = brt.ElementType.GetDefinition();
return type != null return type != null
&& type.Kind == TypeKind.Struct && type.Kind == TypeKind.Struct
&& TransformDisplayClassUsage.IsPotentialClosure(context.CurrentTypeDefinition, type); && TransformDisplayClassUsage.IsPotentialClosure(currentTypeDefinition, type);
} }
LocalFunctionMethod ReduceToLocalFunction(IMethod method, int typeParametersToRemove) LocalFunctionMethod ReduceToLocalFunction(IMethod method, int typeParametersToRemove)

4
ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

@ -21,6 +21,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;
@ -681,6 +682,9 @@ namespace ICSharpCode.Decompiler.TypeSystem
if (otherParameter == parameter) if (otherParameter == parameter)
break; break;
if (LocalFunctionDecompiler.IsClosureParameter(otherParameter, otherParameter.Owner.DeclaringTypeDefinition))
continue;
if (DefaultValueAssignmentAllowedIndividual(otherParameter) || otherParameter.IsParams) if (DefaultValueAssignmentAllowedIndividual(otherParameter) || otherParameter.IsParams)
continue; continue;

Loading…
Cancel
Save