diegofrata / Generator.Equals

A source code generator for automatically implementing IEquatable<T> using only attributes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Indexers on classes lead to syntactically invalid generated code

bastianeicher opened this issue · comments

First of: Very useful generator. Thanks for building this! :)

I ran into a small issue with classes that have indexers:

[Equatable]
partial class Sample
{
    public string Property { get; set; }
    
    public string this[int index] => index.ToString();
}

The generated code looks like this: (shortened for readability)

partial class Sample : IEquatable<Sample> {

// ...

public bool Equals(Sample? other) {
return !ReferenceEquals(other, null) && this.GetType() == other.GetType()
&& EqualityComparer<String>.Default.Equals(Property!, other.Property!)
&& EqualityComparer<String>.Default.Equals(this!, other.this!) // <- invalid
;
}

// ...

public override int GetHashCode() {
                var hashCode = new global::System.HashCode();
            
hashCode.Add(this.GetType());
hashCode.Add(this.Property!, EqualityComparer<String>.Default);
hashCode.Add(this.this!, EqualityComparer<String>.Default); // <- invalid
return hashCode.ToHashCode();
}

which is then rejected by the compiler with CS1001 and CS1003.

It seems that Generator.Equals thinks that this is the name of a property here, rather than interpreting it as the keyword indicating an indexer.

As a workaround, I can mark the indexer with [IgnoreEquality]. However, I think it would be better if Generator.Equals simply ignored indexers by default.

Fixed in 2.2.2. Thanks for reporting!