From e96605ca8721c096b9540d0d155f50657c4e54fc Mon Sep 17 00:00:00 2001 From: Siegfried Pammer <siegfriedpammer@gmail.com> Date: Thu, 24 Oct 2024 19:31:25 +0200 Subject: [PATCH] Fix #3310: Filter out copy-constructor only if it's an actual record type. --- .../TestCases/Pretty/ConstructorInitializers.cs | 10 ++++++++++ .../TransformFieldAndConstructorInitializers.cs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs index 7e8650ff6..9df380be9 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs @@ -137,5 +137,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } #endif + + public class NoRecordButCopyConstructorLike + { + private NoRecordButCopyConstructorLike parent; + + public NoRecordButCopyConstructorLike(NoRecordButCopyConstructorLike parent) + { + this.parent = parent; + } + } } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs index 8bcf924fa..bce3b1071 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs @@ -190,11 +190,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms bool ctorIsUnsafe = instanceCtorsNotChainingWithThis.All(c => c.HasModifier(Modifiers.Unsafe)); - if (!context.DecompileRun.RecordDecompilers.TryGetValue(ctorMethodDef.DeclaringTypeDefinition, out var record)) + if (!context.DecompileRun.RecordDecompilers.TryGetValue(declaringTypeDefinition, out var record)) record = null; // Filter out copy constructor of records - if (record != null) + if (record != null && declaringTypeDefinition.IsRecord) instanceCtorsNotChainingWithThis = instanceCtorsNotChainingWithThis.Where(ctor => !record.IsCopyConstructor(ctor.GetSymbol() as IMethod)).ToArray(); // Recognize field or property initializers: