Browse Source

Fix #3524: Property without backing field cannot have an initializer.

pull/3529/head
Siegfried Pammer 5 months ago
parent
commit
ddb7171a2a
  1. 2
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs
  3. 14
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.cs
  4. 77
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.il
  5. 16
      ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs
  6. 2
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

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

@ -100,6 +100,7 @@ @@ -100,6 +100,7 @@
<None Include="TestCases\ILPretty\Issue3442.il" />
<None Include="TestCases\ILPretty\Issue3466.il" />
<None Include="testcases\ilpretty\Issue3504.il" />
<None Include="testcases\ilpretty\Issue3524.il" />
<None Include="TestCases\ILPretty\MonoFixed.il" />
<None Include="TestCases\Correctness\NonGenericConstrainedCallVirt.il" />
<None Include="TestCases\ILPretty\UnknownTypes.cs" />
@ -142,6 +143,7 @@ @@ -142,6 +143,7 @@
<Compile Include="TestCases\ILPretty\Issue3421.cs" />
<Compile Include="TestCases\ILPretty\Issue3442.cs" />
<Compile Include="TestCases\ILPretty\Issue3466.cs" />
<Compile Include="TestCases\ILPretty\Issue3524.cs" />
<None Include="TestCases\ILPretty\Issue3504.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />

6
ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs

@ -231,6 +231,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -231,6 +231,12 @@ namespace ICSharpCode.Decompiler.Tests
await Run();
}
[Test]
public async Task Issue3524()
{
await Run();
}
[Test]
public async Task Issue2260SwitchString()
{

14
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.cs

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
public class C
{
public static int X {
get {
return 32;
}
set {
}
}
static C()
{
X = 1;
}
}

77
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.il

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
.assembly _
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = (
01 00 08 00 00 00 00 00
)
.custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = (
01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78
63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01
)
.custom instance void [System.Runtime]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggableAttribute/DebuggingModes) = (
01 00 07 01 00 00 00 00
)
.hash algorithm 0x00008004 // SHA1
.ver 0:0:0:0
}
.class public auto ansi beforefieldinit C
extends [System.Runtime]System.Object
{
// Methods
.method public hidebysig specialname static
int32 get_X () cil managed
{
// Method begins at RVA 0x2050
// Code size 6 (0x6)
.maxstack 8
IL_0000: ldc.i4 32
IL_0005: ret
} // end of method C::get_X
.method public hidebysig specialname static
void set_X (
int32 'value'
) cil managed
{
// Method begins at RVA 0x2058
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method C::set_X
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2068
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method C::.ctor
.method private hidebysig specialname rtspecialname static
void .cctor () cil managed
{
// Method begins at RVA 0x2074
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldc.i4 1
IL_0005: call void C::set_X(int32)
IL_000a: ret
} // end of method C::.cctor
// Properties
.property int32 X()
{
.get int32 C::get_X()
.set void C::set_X(int32)
}
} // end of class C

16
ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs

@ -82,6 +82,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -82,6 +82,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
set { SetChildByRole(ExpressionBodyRole, value); }
}
public bool IsAutomaticProperty {
get {
if (!Getter.IsNull && !Getter.Body.IsNull)
{
return false;
}
if (!Setter.IsNull && !Setter.Body.IsNull)
{
return false;
}
return true;
}
}
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitPropertyDeclaration(this);

2
ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

@ -382,7 +382,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -382,7 +382,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
}
}
else if (fieldOrPropertyDecl is PropertyDeclaration pd)
else if (fieldOrPropertyDecl is PropertyDeclaration { IsAutomaticProperty: true } pd)
{
pd.Initializer = assignment.Right.Detach();
}

Loading…
Cancel
Save