Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract Equals on record causes "BadImageFormatException" #75957

Open
mgaffigan opened this issue Nov 18, 2024 · 2 comments
Open

Abstract Equals on record causes "BadImageFormatException" #75957

mgaffigan opened this issue Nov 18, 2024 · 2 comments

Comments

@mgaffigan
Copy link

Description

Record allows a abstract bool Equals(BaseType) method, but does not give a compiler error if it is not implemented on derived types.

Reproduction Steps

On a net8.0 or net9rc2 console project:

_ = new DerivedRecord() == new DerivedRecord();

abstract record BaseRecord()
{
    public abstract bool Equals(BaseRecord? other);
    public override int GetHashCode() => 0;
}

record DerivedRecord() : BaseRecord
{
}

Expected behavior

Compiler error requiring implementation of an Equals method on the derived type
Or generation of a default equals.
Or compiler error prohibiting abstract equals.

Actual behavior

Unhandled exception. System.BadImageFormatException: Bad IL format.
   at DerivedRecord.Equals(DerivedRecord other)
   at DerivedRecord.op_Equality(DerivedRecord left, DerivedRecord right)
   at Program.<Main>$(String[] args)

Regression?

I don't think so.

❌ net6.0-windows
❌ net7.0-windows
❌ net8.0-windows
❌ net9rc2-linux

Known Workarounds

Remove the abstract equals, or implement equals on all derived types:

record DerivedRecord() : BaseRecord
{
    public virtual bool Equals(DerivedRecord? other) => false;
}

Configuration

.Net 8.0.403 on W11 23H2 (22631) on x64
AnyCPU, x86, and x64 all produce same results

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 18, 2024
@huoyaoyuan
Copy link
Member

IL produced for derived class:

    .method public final hidebysig virtual 
        instance bool Equals (
            class BaseRecord other
        ) cil managed 
    {
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
            01 00 02 00 00
        )
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2197
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: callvirt instance bool [System.Runtime]System.Object::Equals(object)
        IL_0007: ret
    } // end of method DerivedRecord::Equals

    .method public hidebysig newslot virtual 
        instance bool Equals (
            class DerivedRecord other
        ) cil managed 
    {
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
            01 00 02 00 00
        )
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x21a0
        // Code size 14 (0xe)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: beq.s IL_000c

        IL_0004: ldarg.0
        IL_0005: ldarg.1
        IL_0006: call instance bool BaseRecord::Equals(class BaseRecord)
        IL_000b: ret

        IL_000c: ldc.i4.1
        IL_000d: ret
    } // end of method DerivedRecord::Equals

It uses call instead of callvirt to call the abstract BaseRecord.Equals method.

The problem should belong to https://github.com/dotnet/roslyn since C# compiler generates invalid IL.

@mgaffigan
Copy link
Author

@huoyaoyuan, thank you for your quick analysis! Should I re-submit there? Or will it be transferred.

@jkotas jkotas transferred this issue from dotnet/runtime Nov 18, 2024
@jcouv jcouv self-assigned this Nov 19, 2024
@jcouv jcouv added this to the 17.13 milestone Nov 19, 2024
@jcouv jcouv added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants