Browse Source

Fix #2547: decimal const not removed from static constructor.

pull/2549/head
Siegfried Pammer 4 years ago
parent
commit
ea1cea96c4
  1. 19
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs
  2. 15
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

19
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs

@ -36,6 +36,25 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -36,6 +36,25 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public class ClassWithConstant
{
// using decimal constants has the effect that there is a cctor
// generated containing the explicit initialization of this field.
// The type is marked beforefieldinit
private const decimal a = 1.0m;
}
public class ClassWithConstantAndStaticCtor
{
// The type is not marked beforefieldinit
private const decimal a = 1.0m;
static ClassWithConstantAndStaticCtor()
{
}
}
public struct SimpleStruct
{
public int Field1;

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

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
@ -304,8 +305,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -304,8 +305,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var metadata = context.TypeSystem.MainModule.PEFile.Metadata;
SRM.MethodDefinition ctorMethodDef = metadata.GetMethodDefinition((SRM.MethodDefinitionHandle)ctorMethod.MetadataToken);
SRM.TypeDefinition declaringType = metadata.GetTypeDefinition(ctorMethodDef.GetDeclaringType());
if (declaringType.HasFlag(System.Reflection.TypeAttributes.BeforeFieldInit))
{
bool declaringTypeIsBeforeFieldInit = declaringType.HasFlag(TypeAttributes.BeforeFieldInit);
while (true)
{
ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement;
@ -324,6 +324,9 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -324,6 +324,9 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{
fieldOrPropertyDecl.Modifiers |= Modifiers.Unsafe;
}
// Only move fields that are constants, if the declaring type is not marked beforefieldinit.
if (declaringTypeIsBeforeFieldInit || fieldOrProperty is IField { IsConst: true })
{
if (fieldOrPropertyDecl is FieldDeclaration fd)
fd.Variables.Single().Initializer = assignment.Right.Detach();
else if (fieldOrPropertyDecl is PropertyDeclaration pd)
@ -332,7 +335,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -332,7 +335,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
break;
es.Remove();
}
if (staticCtor.Body.Statements.Count == 0)
else
{
break;
}
}
if (declaringTypeIsBeforeFieldInit && staticCtor.Body.Statements.Count == 0)
{
staticCtor.Remove();
}
}

Loading…
Cancel
Save