Browse Source

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;
    }
```
pull/3598/head
sonyps5201314 2 months ago
parent
commit
16c2b8c1a9
  1. 12
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

12
ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

@ -196,7 +196,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -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 @@ -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 @@ -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 @@ -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;
}

Loading…
Cancel
Save