icsharpcode / ILSpy

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

Decompiler: consider MathF constants #2717

Open Zastai opened 2 years ago

Zastai commented 2 years ago

As part of some testing I had a method call taking 3 floats called with the 3 constants from MathF: PI, E and Tau.

ILSpy's C# view showed that call as taking ((float) Math.PI, (float)Math.E, (float)Math.PI * 2.0f). So not only does it seem to be unaware of the Tau constant (which also exists in Math), it also does not seem to be aware of the float version of Math.

siegfriedpammer commented 2 years ago

ILSpy recognizes MathF as can be seen here:

https://github.com/icsharpcode/ILSpy/blob/a6721df5909cbf638105dfc4b04bae67a6d029ff/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs#L1403-L1410

However, it only uses MathF if it can be resolved. We would have to take a look at the assembly and see which version of mscorlib is resolved in your case.

Math.Tau seems to be a relatively new addition to .NET: https://github.com/dotnet/runtime/issues/24678 which was completed about half a year after we implemented the "constant unfolding" feature in ILSpy. You are the first person making me aware that it even exists... :-)

However, as the "constant unfolding" feature is somewhat brittle and buggy (see #2445 and #2615 for example), I doubt that we will add even more features to it.

Zastai commented 2 years ago

It's reproducible for me with dotnet new console and then changing Program.cs to have Console.WriteLine("Hello, World! - {0} {1} {2}", MathF.E, MathF.PI, MathF.Tau); using the current .NET 6 SDK.

If I drop the reference to Tau and change the project to have <TargetFrameworks>netstandard2.1;netcoreapp2.1;netcoreapp3.1;net6.0</TargetFrameworks> and <LangVersion>10</LangVersion>, then I can see that the decompilation only produces MathF references for .NET Standard 2.1, not for any of the .NET Core flavors.

Looking at the references, there's [netstandard]MethF for .NET Standard, [System.Runtime.Extensions]MathF for the Core and [System.Runtime]MathF for .NET 5+.