icsharpcode / ILSpy

.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
21.45k stars 3.35k forks source link

Issue when decompiling enum. #1286

Closed mkrueger closed 6 years ago

mkrueger commented 6 years ago

copied from #1111 - it's still an issue today.

mscorlib that shows the issue:

https://www.dropbox.com/s/sdbeyc6ds7yjem0/mscorlib.dll?dl=0

Got that on mono - however I can't open the decompiler project myself for some reason it doesn't load on linux - so I just report that one:

Given the System.DayOfWeek enum which is decompiled as :

using System.Runtime.InteropServices;

namespace System
{
    [Serializable]
    [ComVisible(true)]
    public enum DayOfWeek
    {
        Friday = 5,
        Monday = 1,
        Saturday = 6,
        Sunday = 0,
        Thursday = 4,
        Tuesday = 2,
        Wednesday
    }
}

The IL for the mono version of that looks a bit strange because the value sorting is wrong :

.class public auto ansi sealed System.DayOfWeek
    extends System.Enum
{
    // Fields
    .field public specialname rtspecialname int32 value__
    .field public static literal valuetype System.DayOfWeek Friday = int32(5)
    .field public static literal valuetype System.DayOfWeek Monday = int32(1)
    .field public static literal valuetype System.DayOfWeek Saturday = int32(6)
    .field public static literal valuetype System.DayOfWeek Sunday = int32(0)
    .field public static literal valuetype System.DayOfWeek Thursday = int32(4)
    .field public static literal valuetype System.DayOfWeek Tuesday = int32(2)
    .field public static literal valuetype System.DayOfWeek Wednesday = int32(3)

} // end of class System.DayOfWeek

Note that when using debug builds an assertion is hit:

FailFast: Should not insert using declaration for namespace that is missing from the superset: System.Runtime.InteropServices

   at System.Diagnostics.Debug.Assert(Boolean condition, String message, String detailMessage)
   at ICSharpCode.Decompiler.CSharp.Transforms.IntroduceUsingDeclarations.Run(AstNode rootNode, TransformContext context) in C:\projects\ilspy\ICSharpCode.Decompiler\CSharp\Transforms\IntroduceUsingDeclarations.cs:line 50
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.RunTransforms(AstNode rootNode, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in C:\projects\ilspy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 299
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileType(FullTypeName fullTypeName) in C:\projects\ilspy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 764
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileTypeAsString(FullTypeName fullTypeName) in C:\projects\ilspy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 776
   at ICSharpCode.Decompiler.Console.Program.Decompile(String assemblyFileName, TextWriter output, String typeName) in /home/mkrueger/work/ILSpy/ICSharpCode.Decompiler.Console/Program.cs:line 109
   at ICSharpCode.Decompiler.Console.Program.<>c__DisplayClass0_0.<Main>b__1() in /home/mkrueger/work/ILSpy/ICSharpCode.Decompiler.Console/Program.cs:line 70
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at ICSharpCode.Decompiler.Console.Program.Main(String[] args) in /home/mkrueger/work/ILSpy/ICSharpCode.Decompiler.Console/Program.cs:line 75

   at System.Environment.FailFast(System.String, System.Exception)
   at ICSharpCode.Decompiler.CSharp.Transforms.IntroduceUsingDeclarations.Run(ICSharpCode.Decompiler.CSharp.Syntax.AstNode, ICSharpCode.Decompiler.CSharp.Transforms.TransformContext)
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.RunTransforms(ICSharpCode.Decompiler.CSharp.Syntax.AstNode, ICSharpCode.Decompiler.DecompileRun, ICSharpCode.Decompiler.TypeSystem.ITypeResolveContext)
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileType(ICSharpCode.Decompiler.TypeSystem.FullTypeName)
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileTypeAsString(ICSharpCode.Decompiler.TypeSystem.FullTypeName)
   at ICSharpCode.Decompiler.Console.Program.Decompile(System.String, System.IO.TextWriter, System.String)
   at ICSharpCode.Decompiler.Console.Program+<>c__DisplayClass0_0.<Main>b__1()
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(System.String[])
   at ICSharpCode.Decompiler.Console.Program.Main(System.String[])
siegfriedpammer commented 6 years ago

Which version of the decompiler engine are you using? Using the latest master and the assembly you provided, I get the following output: grafik

This should be the desired output, I think. Also the assertion is no longer failing, because the missing using System has been added.

siegfriedpammer commented 6 years ago

@mkrueger any feedback?

mkrueger commented 6 years ago

3.1.0.3652 updating to 3.2 solves the issue - thanks.