Skip to content

Commit ddb7171

Browse files
Fix #3524: Property without backing field cannot have an initializer.
1 parent fccd9e2 commit ddb7171

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<None Include="TestCases\ILPretty\Issue3442.il" />
101101
<None Include="TestCases\ILPretty\Issue3466.il" />
102102
<None Include="testcases\ilpretty\Issue3504.il" />
103+
<None Include="testcases\ilpretty\Issue3524.il" />
103104
<None Include="TestCases\ILPretty\MonoFixed.il" />
104105
<None Include="TestCases\Correctness\NonGenericConstrainedCallVirt.il" />
105106
<None Include="TestCases\ILPretty\UnknownTypes.cs" />
@@ -142,6 +143,7 @@
142143
<Compile Include="TestCases\ILPretty\Issue3421.cs" />
143144
<Compile Include="TestCases\ILPretty\Issue3442.cs" />
144145
<Compile Include="TestCases\ILPretty\Issue3466.cs" />
146+
<Compile Include="TestCases\ILPretty\Issue3524.cs" />
145147
<None Include="TestCases\ILPretty\Issue3504.cs" />
146148
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
147149
<Compile Include="TestCases\Pretty\Comparisons.cs" />

ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ public async Task Issue3504()
231231
await Run();
232232
}
233233

234+
[Test]
235+
public async Task Issue3524()
236+
{
237+
await Run();
238+
}
239+
234240
[Test]
235241
public async Task Issue2260SwitchString()
236242
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class C
2+
{
3+
public static int X {
4+
get {
5+
return 32;
6+
}
7+
set {
8+
}
9+
}
10+
static C()
11+
{
12+
X = 1;
13+
}
14+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.assembly _
2+
{
3+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = (
4+
01 00 08 00 00 00 00 00
5+
)
6+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = (
7+
01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78
8+
63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01
9+
)
10+
.custom instance void [System.Runtime]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggableAttribute/DebuggingModes) = (
11+
01 00 07 01 00 00 00 00
12+
)
13+
.hash algorithm 0x00008004 // SHA1
14+
.ver 0:0:0:0
15+
}
16+
17+
.class public auto ansi beforefieldinit C
18+
extends [System.Runtime]System.Object
19+
{
20+
// Methods
21+
.method public hidebysig specialname static
22+
int32 get_X () cil managed
23+
{
24+
// Method begins at RVA 0x2050
25+
// Code size 6 (0x6)
26+
.maxstack 8
27+
28+
IL_0000: ldc.i4 32
29+
IL_0005: ret
30+
} // end of method C::get_X
31+
32+
.method public hidebysig specialname static
33+
void set_X (
34+
int32 'value'
35+
) cil managed
36+
{
37+
// Method begins at RVA 0x2058
38+
// Code size 1 (0x1)
39+
.maxstack 8
40+
41+
IL_0000: ret
42+
} // end of method C::set_X
43+
44+
.method public hidebysig specialname rtspecialname
45+
instance void .ctor () cil managed
46+
{
47+
// Method begins at RVA 0x2068
48+
// Code size 8 (0x8)
49+
.maxstack 8
50+
51+
IL_0000: ldarg.0
52+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
53+
IL_0006: nop
54+
IL_0007: ret
55+
} // end of method C::.ctor
56+
57+
.method private hidebysig specialname rtspecialname static
58+
void .cctor () cil managed
59+
{
60+
// Method begins at RVA 0x2074
61+
// Code size 11 (0xb)
62+
.maxstack 8
63+
64+
IL_0000: ldc.i4 1
65+
IL_0005: call void C::set_X(int32)
66+
IL_000a: ret
67+
} // end of method C::.cctor
68+
69+
// Properties
70+
.property int32 X()
71+
{
72+
.get int32 C::get_X()
73+
.set void C::set_X(int32)
74+
}
75+
76+
} // end of class C
77+

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public Expression ExpressionBody {
8282
set { SetChildByRole(ExpressionBodyRole, value); }
8383
}
8484

85+
public bool IsAutomaticProperty {
86+
get {
87+
if (!Getter.IsNull && !Getter.Body.IsNull)
88+
{
89+
return false;
90+
}
91+
92+
if (!Setter.IsNull && !Setter.Body.IsNull)
93+
{
94+
return false;
95+
}
96+
97+
return true;
98+
}
99+
}
100+
85101
public override void AcceptVisitor(IAstVisitor visitor)
86102
{
87103
visitor.VisitPropertyDeclaration(this);

ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ void HandleStaticFieldInitializers(IEnumerable<AstNode> members)
382382
}
383383
}
384384
}
385-
else if (fieldOrPropertyDecl is PropertyDeclaration pd)
385+
else if (fieldOrPropertyDecl is PropertyDeclaration { IsAutomaticProperty: true } pd)
386386
{
387387
pd.Initializer = assignment.Right.Detach();
388388
}

0 commit comments

Comments
 (0)