Skip to content

Commit 9c8d1e4

Browse files
Many thanks to @sonyps5201314 for providing the test cases and a suggested fix, which inspired these changes.
Various improvements regarding primary constructor decompilation, including: - introduce `HasPrimaryConstructor` property in the AST, as there is a difference between no primary constructor and a parameterless primary constructor - improved support for inherited records and forwarded ctor calls - exclude non-public fields and properties in IsPrintedMember - introduce an option to always make the decompiler emit primary constructors, when possible
1 parent ac8503c commit 9c8d1e4

File tree

12 files changed

+957
-55
lines changed

12 files changed

+957
-55
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@
163163
<Compile Include="TestCases\Pretty\Issue3571_B.cs" />
164164
<Compile Include="TestCases\Pretty\Issue3571_A.cs" />
165165
<Compile Include="TestCases\Pretty\Issue3576.cs" />
166+
<Compile Include="TestCases\Pretty\PlaystationPreferPrimary.cs" />
167+
<Compile Include="TestCases\Pretty\Playstation.cs" />
166168
<None Include="TestCases\Ugly\NoLocalFunctions.Expected.cs" />
167169
<None Include="TestCases\ILPretty\Issue3504.cs" />
168170
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />

ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ public async Task Records([ValueSource(nameof(roslyn3OrNewerOptions))] CompilerO
535535
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.NullableEnable);
536536
}
537537

538+
[Test]
539+
public async Task Playstation([ValueSource(nameof(roslyn4OrNewerOptions))] CompilerOptions cscOptions)
540+
{
541+
// see https://github.com/icsharpcode/ILSpy/pull/3598#issuecomment-3465151525
542+
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.NullableEnable);
543+
}
544+
545+
[Test]
546+
public async Task PlaystationPreferPrimary([ValueSource(nameof(roslyn4OrNewerOptions))] CompilerOptions cscOptions)
547+
{
548+
// see https://github.com/icsharpcode/ILSpy/pull/3598#issuecomment-3465151525
549+
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.NullableEnable, configureDecompiler: settings => {
550+
settings.PreferPrimaryConstructorIfPossible = true;
551+
});
552+
}
553+
538554
[Test]
539555
public async Task ExtensionProperties([ValueSource(nameof(roslyn4OrNewerOptions))] CompilerOptions cscOptions)
540556
{

ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ public void Print()
127127
}
128128
}
129129

130+
131+
132+
public class ClassWithPrimaryCtorUsingGlobalParameterInExpressionAssignedToProperty(int a)
133+
{
134+
public int A { get; set; } = (int)Math.Abs(Math.PI * (double)a);
135+
136+
public void Print()
137+
{
138+
Console.WriteLine(A);
139+
}
140+
}
141+
130142
public class ClassWithPrimaryCtorUsingGlobalParameterAssignedToEvent(EventHandler a)
131143
{
132144
public event EventHandler A = a;

0 commit comments

Comments
 (0)