Browse Source

Merge pull request #3579 from ds5678/issue3576

Do not create object initializers for tuples
pull/3583/head
Siegfried Pammer 3 months ago committed by GitHub
parent
commit
16b74f6aec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 39
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3576.cs
  4. 3
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

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

@ -158,6 +158,7 @@ @@ -158,6 +158,7 @@
<Compile Include="TestCases\Pretty\Issue3571_C.cs" />
<Compile Include="TestCases\Pretty\Issue3571_B.cs" />
<Compile Include="TestCases\Pretty\Issue3571_A.cs" />
<Compile Include="TestCases\Pretty\Issue3576.cs" />
<None Include="TestCases\Ugly\NoLocalFunctions.Expected.cs" />
<None Include="TestCases\ILPretty\Issue3504.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -696,6 +696,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -696,6 +696,12 @@ namespace ICSharpCode.Decompiler.Tests
await RunForLibrary(cscOptions: cscOptions);
}
[Test]
public async Task Issue3576([ValueSource(nameof(roslyn2OrNewerWithNet40Options))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}
[Test]
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{

39
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3576.cs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
using System.Collections.Generic;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal static class Issue3576
{
public static Issue3576_Camera GetOrCreate(long key, int frameCount, Dictionary<long, (Issue3576_Camera, int)> cache)
{
if (!cache.TryGetValue(key, out var value))
{
Issue3576_GameObject issue3576_GameObject = new Issue3576_GameObject();
value = (issue3576_GameObject.AddComponent<Issue3576_Camera>(), frameCount);
value.Item1.Property = 1;
issue3576_GameObject.SetActive(value: false);
cache[key] = value;
}
else
{
value.Item2 = frameCount;
cache[key] = value;
}
return value.Item1;
}
}
internal sealed class Issue3576_Camera
{
public int Property { get; set; }
}
internal sealed class Issue3576_GameObject
{
public T AddComponent<T>()
{
throw null;
}
public void SetActive(bool value)
{
}
}
}

3
ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

@ -69,6 +69,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -69,6 +69,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// anon = new { A = 5 } { 3,4,5 } is invalid syntax.
if (newObjInst.Method.DeclaringType.ContainsAnonymousType())
return;
// Tuples cannot have initializers
if (TupleTransform.MatchTupleConstruction(newObjInst, out _))
return;
instType = newObjInst.Method.DeclaringType;
break;
case DefaultValue defaultVal:

Loading…
Cancel
Save