Browse Source

Records: Support new EqualityContract pattern.

pull/2832/head
Siegfried Pammer 3 years ago
parent
commit
ae0e83f0c4
  1. 2
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  2. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs
  3. 18
      ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -518,7 +518,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -518,7 +518,7 @@ namespace ICSharpCode.Decompiler.Tests
}
[Test]
public async Task Records([ValueSource(nameof(roslynLatestOnlyOptions))] CompilerOptions cscOptions)
public async Task Records([ValueSource(nameof(roslyn3OrNewerOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.NullableEnable);
}

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
using System;
#if ROSLYN4
using System.Runtime.InteropServices;
#endif
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
@ -127,6 +129,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -127,6 +129,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
#if ROSLYN4
internal class RecordStructs
{
public record struct Base(string A);
@ -227,6 +230,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -227,6 +230,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
}
#endif
}
namespace System.Runtime.CompilerServices
{

18
ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs

@ -445,11 +445,23 @@ namespace ICSharpCode.Decompiler.CSharp @@ -445,11 +445,23 @@ namespace ICSharpCode.Decompiler.CSharp
var getter = property.Getter;
if (!(getter != null && !property.CanSet))
return false;
if (property.GetAttributes().Any())
return false;
var attrs = property.GetAttributes().ToList();
switch (attrs.Count)
{
case 0:
// Roslyn 3.x does not emit a CompilerGeneratedAttribute on the property itself.
break;
case 1:
// Roslyn 4.4 started doing so.
if (!attrs[0].AttributeType.IsKnownType(KnownAttribute.CompilerGenerated))
return false;
break;
default:
return false;
}
if (getter.GetReturnTypeAttributes().Any())
return false;
var attrs = getter.GetAttributes().ToList();
attrs = getter.GetAttributes().ToList();
if (attrs.Count != 1)
return false;
if (!attrs[0].AttributeType.IsKnownType(KnownAttribute.CompilerGenerated))

Loading…
Cancel
Save