From ae0e83f0c417f24d6e712317d03d3413e9567536 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 3 Nov 2022 23:38:42 +0100 Subject: [PATCH] Records: Support new EqualityContract pattern. --- .../PrettyTestRunner.cs | 2 +- .../TestCases/Pretty/Records.cs | 4 ++++ .../CSharp/RecordDecompiler.cs | 18 +++++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index 549273f56..ec433a7b4 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -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); } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs index 30c0402da..934d4a6fe 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs @@ -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 } } +#if ROSLYN4 internal class RecordStructs { public record struct Base(string A); @@ -227,6 +230,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } } +#endif } namespace System.Runtime.CompilerServices { diff --git a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs index 455c8b21b..2d832dfd7 100644 --- a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs @@ -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))