icsharpcode / ILSpy

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

Wrong decompilation of null nullable int #3181

Open coxantohi opened 3 months ago

coxantohi commented 3 months ago

Input code

namespace NullForgivingOperatorNamespace
{
    public class NullForgivingOperatorClass
    {
        public void NullForgivingOperatorMethod()
        {
            int? x = null;
            string s = x?.ToString();
        }
    }
}

Decompiled code

namespace NullForgivingOperatorNamespace;

public class NullForgivingOperatorClass
{
    public void NullForgivingOperatorMethod()
    {
        string s = ((int)null?).ToString();
    }
}

the build errors are:

NullForgivingOperatorClass.cs(7,25): error CS1525: Invalid expression term ')'
NullForgivingOperatorClass.cs(7,25): error CS1003: Syntax error, ':' expected

Details

This issue was found when trying to write sample input code for https://github.com/icsharpcode/ILSpy/issues/3180.