The Issue is posted in the NullGuard.Fody repository with nr. 598.
When run with NullGuard the record below named FooRecordWithInit
returns a System.InvalidProgramException: 'Common Language Runtime detected an invalid program.'
.
With the setup for NullGuard Mode="NullableReferenceTypes"
and IncludeDebugAssert="false"/>
.
public record FooRecordWithInit
{
private int _id;
public int Id
{
init => _id = value; //< ERROR: Common Language Runtime detected an invalid program.
}
}
The underlying issue seems to be the invalid IL weaved by Fody as seen here:
public int Id
{
init
{
_id = value;
if ((int)/*Error near IL_0007: Stack underflow*/ == 0)
{
throw new InvalidOperationException("[NullGuard] Return value of method 'System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Bug.FooRecordWithInit::set_Id(System.Int32)' is null.");
}
}
}
In other situations a record works fine with a setter instead of init:
public record FooRecordWithSet
{
private int _id;
public int Id
{
set => _id = value; //< Works
}
}
A minimal Solution to reproduce the issue can be found here.
- Clone the repository.
- Build the project.
- Use ILSpy to view the output.
Operating System: Windows 11 Enterprise 64-Bit (10.0, Build 22621)
Framework Version: .NET 6
Packages: Fody 6.6.4, NullGuard.Fody 3.1.0
x64