Skip to content

The equality and hash code methods do not work properly for non-empty array builders #25139

@plokhotnyuk

Description

@plokhotnyuk

Compiler version

Both Scala 2.13.x and Scala 3.x are affected

Minimized code

@main def test = {
  println(Array.newBuilder[Int].addOne(1) == Array.newBuilder[Int].addOne(1))
  println(Array.newBuilder[Int].addOne(1).hashCode == Array.newBuilder[Int].addOne(1).hashCode)
}

Output

false
false

Expectation

true
true

Details

The implementations have overridden equals, but in a wrong way, like here for ArrayBuilder.ofInt:

    override def equals(other: Any): Boolean = other match {
      case x: ofInt => (size == x.size) && (elems == x.elems)
      case _ => false
    }

Also, the hashCode implementations are missing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions