Skip to content

Commit 16b74f6

Browse files
Merge pull request #3579 from ds5678/issue3576
Do not create object initializers for tuples
2 parents 7b03606 + dc3ed43 commit 16b74f6

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<Compile Include="TestCases\Pretty\Issue3571_C.cs" />
159159
<Compile Include="TestCases\Pretty\Issue3571_B.cs" />
160160
<Compile Include="TestCases\Pretty\Issue3571_A.cs" />
161+
<Compile Include="TestCases\Pretty\Issue3576.cs" />
161162
<None Include="TestCases\Ugly\NoLocalFunctions.Expected.cs" />
162163
<None Include="TestCases\ILPretty\Issue3504.cs" />
163164
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />

ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,12 @@ public async Task Issue3571_C([ValueSource(nameof(roslyn2OrNewerWithNet40Options
696696
await RunForLibrary(cscOptions: cscOptions);
697697
}
698698

699+
[Test]
700+
public async Task Issue3576([ValueSource(nameof(roslyn2OrNewerWithNet40Options))] CompilerOptions cscOptions)
701+
{
702+
await RunForLibrary(cscOptions: cscOptions);
703+
}
704+
699705
[Test]
700706
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
701707
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Collections.Generic;
2+
3+
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
4+
{
5+
internal static class Issue3576
6+
{
7+
public static Issue3576_Camera GetOrCreate(long key, int frameCount, Dictionary<long, (Issue3576_Camera, int)> cache)
8+
{
9+
if (!cache.TryGetValue(key, out var value))
10+
{
11+
Issue3576_GameObject issue3576_GameObject = new Issue3576_GameObject();
12+
value = (issue3576_GameObject.AddComponent<Issue3576_Camera>(), frameCount);
13+
value.Item1.Property = 1;
14+
issue3576_GameObject.SetActive(value: false);
15+
cache[key] = value;
16+
}
17+
else
18+
{
19+
value.Item2 = frameCount;
20+
cache[key] = value;
21+
}
22+
return value.Item1;
23+
}
24+
}
25+
internal sealed class Issue3576_Camera
26+
{
27+
public int Property { get; set; }
28+
}
29+
internal sealed class Issue3576_GameObject
30+
{
31+
public T AddComponent<T>()
32+
{
33+
throw null;
34+
}
35+
public void SetActive(bool value)
36+
{
37+
}
38+
}
39+
}

ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con
6969
// anon = new { A = 5 } { 3,4,5 } is invalid syntax.
7070
if (newObjInst.Method.DeclaringType.ContainsAnonymousType())
7171
return;
72+
// Tuples cannot have initializers
73+
if (TupleTransform.MatchTupleConstruction(newObjInst, out _))
74+
return;
7275
instType = newObjInst.Method.DeclaringType;
7376
break;
7477
case DefaultValue defaultVal:

0 commit comments

Comments
 (0)