Browse Source

Fix false positive in struct initializers with default.value init

pull/927/head
Siegfried Pammer 8 years ago
parent
commit
08dcead162
  1. 14
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs
  2. 7
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

14
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs

@ -338,7 +338,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
InitializerTests.X(InitializerTests.Y(), new Data { InitializerTests.X(InitializerTests.Y(), new Data {
MoreData = { MoreData = {
a = MyEnum.a, a = MyEnum.a,
[2] = (Data)null [2] = null
} }
}); });
} }
@ -348,11 +348,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
InitializerTests.X(InitializerTests.Y(), new Data { InitializerTests.X(InitializerTests.Y(), new Data {
MoreData = { MoreData = {
a = MyEnum.a, a = MyEnum.a,
[GetInt()] = { [InitializerTests.GetInt()] = {
a = MyEnum.b, a = MyEnum.b,
FieldList = { MyEnum2.c }, FieldList = {
[GetInt(), GetString()] = new Data(), MyEnum2.c
[2] = (Data)null },
[InitializerTests.GetInt(), InitializerTests.GetString()] = new Data(),
[2] = null
} }
} }
}); });
@ -397,7 +399,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public static void NotAStructInitializer_DefaultConstructor() public static void NotAStructInitializer_DefaultConstructor()
{ {
StructData structData = new StructData(); StructData structData = default(StructData);
structData.Field = 1; structData.Field = 1;
structData.Property = 2; structData.Property = 2;
InitializerTests.X(InitializerTests.Y(), structData); InitializerTests.X(InitializerTests.Y(), structData);

7
ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

@ -66,6 +66,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms
instType = newObjInst.Method.DeclaringType; instType = newObjInst.Method.DeclaringType;
break; break;
case DefaultValue defaultVal: case DefaultValue defaultVal:
if (defaultVal.ILStackWasEmpty && v.Kind == VariableKind.Local && !context.Function.Method.IsConstructor) {
// on statement level (no other expressions on IL stack),
// prefer to keep local variables (but not stack slots),
// unless we are in a constructor (where inlining object initializers might be critical
// for the base ctor call)
return false;
}
instType = defaultVal.Type; instType = defaultVal.Type;
break; break;
case Block existingInitializer: case Block existingInitializer:

Loading…
Cancel
Save