Singulink / RuntimeNullables

Automatic null check injection for runtime C# 8+ Nullable Reference Type (NRT) contract validation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returning Task.FromResult(false) throws NullReferenceException

CyberAndrii opened this issue · comments

Unhandled exception. System.NullReferenceException: Task result nullability contract was broken.
   at RuntimeNullables.ThrowHelpers.ThrowOutputNull(String message)
   at FodyTest.Program.DoSomethingAsync() in /home/andrii/development/FodyTest/FodyTest/Program.cs:line 17
   at FodyTest.Program.Main(String[] args) in /home/andrii/development/FodyTest/FodyTest/Program.cs:line 9
   at FodyTest.Program.<Main>(String[] args)

Minimal reproducible example:

using System.Threading.Tasks;

namespace FodyTest
{
    class Program
    {
        static Task Main(string[] args)
        {
            return DoSomethingAsync();
        }

        static Task<bool> DoSomethingAsync()
        {
            // Returning False throws the exception
            // Returning True works fine
            return Task.FromResult(false);
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <PropertyGroup>
        <WeaverConfiguration>
            <Weavers>
                <RuntimeNullables CheckOutputs="true" CheckNonPublic="true" />
            </Weavers>
        </WeaverConfiguration>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="RuntimeNullables.Fody" Version="0.9.3" />
    </ItemGroup>

</Project>

Thank you for the report, I have reproduced and traced the problem. Please give me a few minutes here, I will let you know when a fix is posted.

Version 0.9.4 is publishing to nuget right now, should be available in a few minutes - please give it a try and let me know if you have any other problems.

Thanks for quick fix. These problems are fixed but I still have another. I think it's something related to using ref and foreach in the same method.

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at FodyTest.Program.PreProcessYaml(String& yaml)
   at FodyTest.Program.Main(String[] args) in /home/andrii/development/FodyTest/FodyTest/Program.cs:line 16
    class Program
    {
        static readonly Dictionary<string, string> vars = new()
        {
            {"foo", "bar"},
        };

        static void Main(string[] args)
        {
            string str = "foo";
            PreProcessYaml(ref str);
        }

        static void PreProcessYaml(ref string yaml)
        {
            foreach (var variable in vars)
            {
            }
        }
    }

Interesting, traced the issue - has to do with the finally block added by the foreach right before the return. Let me see what I can do here.

Can you try something for me real quick - if you add any dummy code like Console.Write(""); after the foreach block, does it work?

Yes - adding Console.Write(""); or even return; fixes the issue

Thank you for your concise minimal repros that made tracing this issue a piece of cake :)

The return; trick only works in debug builds - release builds will optimize out the redundant return and fail. Fix for this issue incoming shortly!

I may have come across a Mono Cecil bug - tracking the issue here jbevain/cecil#714.

Investigating workarounds in the meantime.

Fixed via 0654c80.

New nuget package going up right now, should be available to update in a few minutes.

Seems like the same issue happens when using Enums, is that fixed too?

@Trojaner Give the updated package a try, it should be fixed.

This issue happens with 0.9.5, which appears to be the latest version.

I think it happens when enums return 0.

image

image

image

The return value can not be null here

Ah, I see the issue, I fixed it for sync task results but not async. Update coming shortly, give me a bit.

Package is published, give er a go :)

Thank you, I can confirm that the issue is resolved now.