From daa0b9caff7d48b298ecbaa0f7f875fb7b1b303b Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 7 Mar 2022 23:21:15 +0100 Subject: [PATCH] Fix #2640: Invalid implicit enum value for unordered items (cherry picked from commit 1ee3384a056a7ef908bf1bc49c77e0ed6196a393) --- ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs | 6 ++++++ ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs index 169d1c001..4d3a13fc9 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs @@ -28,6 +28,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Item2 } + public enum OutOfOrderMembers + { + Item1 = 1, + Item0 = 0 + } + public enum LongBasedEnum : long { Item1, diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index f9e8594a8..43d54cae2 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -1405,7 +1405,12 @@ namespace ICSharpCode.Decompiler.CSharp firstValue = currentValue; first = false; } - else if (!allConsecutive && !allPowersOfTwo) + else if (currentValue < previousValue) + { + // If the values are out of order, we fallback to displaying all values. + return EnumValueDisplayMode.All; + } + else if ((!allConsecutive && !allPowersOfTwo)) { // We already know that the values are neither consecutive nor all powers of 2, // so we can abort, and just display all values as-is.