minicover icon indicating copy to clipboard operation
minicover copied to clipboard

Using Records with `with` keyword results in "Common Language Runtime detected an invalid program"

Open ejustice opened this issue 2 years ago • 1 comments

So we use have simple tests using records and with statement:

   [Fact]
    public void GivenValidate_WhenSourceContractIsDefault_ThenItShouldBeInvalid()
    {
        var validationResult = new AddLeaseChangeModelValidator().Validate(
            GetValidModel(m => m with { SourceContractVersion = 0 })
        );

        validationResult.IsValid.Should().BeFalse();
    }

GetValidModel looks like this:

    private static LeaseChangeModel GetValidModel(Func<LeaseChangeModel, LeaseChangeModel>? func = null)
    {
        var model = new LeaseChangeModel
        (
            IndividualLeaseAgreementNumber: "dummy_IndividualLeaseAgreementNumber",
           //...
        );

        if (func is not null)
        {
            return func(model);
        }
        return model;
    }

This works perfectly with good old classes with public getter / setter. But breaks using records.

System.InvalidProgramException : Common Language Runtime detected an invalid program.

On our CI/CD we run minicover 3.4.4

I tried locally with 3.4.8 and get the same result.

ejustice avatar Oct 14 '22 14:10 ejustice

I wonder if this is perhaps a Mono.Cecil limitation.

ffMathy avatar Oct 14 '22 14:10 ffMathy