Abstract `Equals` on record causes "BadImageFormatException"
@jcouv is already working on this.
Since Nov 19, 2024.
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.