Skip to content

Commit

Permalink
Fix icsharpcode#2547: decimal const not removed from static constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Jan 1, 2022
1 parent d72937c commit 36e52a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ public Issue1743(int dummy1, int dummy2)
}
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

using System.Collections.Generic;
using System.Linq;
using dnlib.DotNet;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.TypeSystem;
using IField = ICSharpCode.Decompiler.TypeSystem.IField;
using IMethod = ICSharpCode.Decompiler.TypeSystem.IMethod;

namespace ICSharpCode.Decompiler.CSharp.Transforms
{
Expand Down Expand Up @@ -281,8 +284,8 @@ void HandleStaticFieldInitializers(IEnumerable<AstNode> members)
if (staticCtor != null) {
bool ctorIsUnsafe = staticCtor.HasModifier(Modifiers.Unsafe);
IMethod ctorMethod = staticCtor.GetSymbol() as IMethod;
dnlib.DotNet.MethodDef ctorMethodDef = ctorMethod?.MetadataToken as dnlib.DotNet.MethodDef;
if (ctorMethodDef != null && ctorMethodDef.DeclaringType.IsBeforeFieldInit) {
if (ctorMethod?.MetadataToken is MethodDef ctorMethodDef) {
bool declaringTypeIsBeforeFieldInit = ctorMethodDef.DeclaringType.IsBeforeFieldInit;
while (true) {
ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement;
if (es == null)
Expand All @@ -296,19 +299,30 @@ void HandleStaticFieldInitializers(IEnumerable<AstNode> members)
var fieldOrPropertyDecl = members.FirstOrDefault(f => f.GetSymbol() == fieldOrProperty) as EntityDeclaration;
if (fieldOrPropertyDecl == null)
break;
if (ctorIsUnsafe && IntroduceUnsafeModifier.IsUnsafe(assignment.Right)) {
if (ctorIsUnsafe && IntroduceUnsafeModifier.IsUnsafe(assignment.Right))
{
fieldOrPropertyDecl.Modifiers |= Modifiers.Unsafe;
}
if (fieldOrPropertyDecl is FieldDeclaration fd)
fd.Variables.Single().Initializer = assignment.Right.Detach();
else if (fieldOrPropertyDecl is PropertyDeclaration pd)
pd.Initializer = assignment.Right.Detach();
// 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)
pd.Initializer = assignment.Right.Detach();
else
break;
es.Remove();
}
else
{
break;
es.Remove();
}
}
if (staticCtor.Body.Statements.Count == 0)
if (declaringTypeIsBeforeFieldInit && staticCtor.Body.Statements.Count == 0)
{
staticCtor.Remove();
}
}
}
}
Expand Down

0 comments on commit 36e52a1

Please sign in to comment.