From 16c2b8c1a9a901a636ba85d68d40e695186d482b Mon Sep 17 00:00:00 2001 From: sonyps5201314 Date: Thu, 30 Oct 2025 06:56:58 +0800 Subject: [PATCH] The logic was temporarily adjusted so that the `StructWithDefaultCtor` type in the unit test could pass the test. In fact, the member initialization statement in its constructor could be moved. Here's an example: when `IsPrimaryConstructor` return `false`: ```cs struct StructWithDefaultCtor { int X = 42; public StructWithDefaultCtor() { } } ``` when `IsPrimaryConstructor` return `true`: ```cs struct StructWithDefaultCtor() { int X = 42; } ``` --- .../TransformFieldAndConstructorInitializers.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs index eb9c845d0..4d9f994ed 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs @@ -196,7 +196,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms { var ctorMethodDef = instanceCtorsNotChainingWithThis[0].GetSymbol() as IMethod; ITypeDefinition declaringTypeDefinition = ctorMethodDef?.DeclaringTypeDefinition; - if (ctorMethodDef != null && declaringTypeDefinition?.IsReferenceType == false && !declaringTypeDefinition.IsRecord && declaringTypeDefinition.Kind != TypeKind.Struct) + var isStruct = declaringTypeDefinition?.Kind == TypeKind.Struct; + if (ctorMethodDef != null && declaringTypeDefinition?.IsReferenceType == false && !declaringTypeDefinition.IsRecord && !isStruct) return; bool ctorIsUnsafe = instanceCtorsNotChainingWithThis.All(c => c.HasModifier(Modifiers.Unsafe)); @@ -211,7 +212,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms // Recognize field or property initializers: // Translate first statement in all ctors (if all ctors have the same statement) into an initializer. bool allSame; - bool isStructPrimaryCtor = false; + bool hasPrimaryCtor = record?.PrimaryConstructor != null; do { Match m = fieldInitializerPattern.Match(instanceCtorsNotChainingWithThis[0].Body.FirstOrDefault()); @@ -274,8 +275,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } if (assignFromParameters) { - isStructPrimaryCtor = record?.PrimaryConstructor != null; - if (!isStructPrimaryCtor) + if (!hasPrimaryCtor) { // When there is no primary constructor, assignments from constructor parameters to class members cannot be transferred. break; @@ -288,8 +288,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms // cannot transform if member is not found if (fieldOrPropertyOrEventDecl == null) break; - // or if this is a struct record, but not the primary ctor - if (declaringTypeDefinition.IsRecord && declaringTypeDefinition.IsReferenceType == false && !isStructPrimaryCtor) + // or if this is a struct, but not the primary ctor + if (isStruct && !hasPrimaryCtor) break; }