Skip to content

Overriding copy of case class results in unhelpful error messages #4564

Closed
@gsps

Description

@gsps
case class A(x: Int) {
  def copy(x: Int = x) = A(x)
}

results in merge errors and friends:

-- Error: try/CaseClassCopyMergeError.scala:2:20 -------------------------------
2 |  def copy(x: Int = this.x) = A(x)
  |                    ^^^^^^
  |             cannot merge
  |               method copy: (x: Int): A  and
  |               method copy: (x: Int): A;
  |             they are both defined in class A but have matching signatures
  |               (x: Int): A and
  |               (x: Int): A
  |             when seen as members of A(A.this)
-- Error: try/CaseClassCopyMergeError.scala:1:19 -------------------------------
1 |case class A(x: Int) {
  |                   ^
  |             cannot merge
  |               method copy: (x: Int): A  and
  |               method copy: (x: Int): A;
  |             they are both defined in class A but have matching signatures
  |               (x: Int): A and
  |               (x: Int): A
  |             when seen as members of A(A.this)
-- [E119] Duplicate Symbol Error: try/CaseClassCopyMergeError.scala:2:6 --------
2 |  def copy(x: Int = this.x) = A(x)
  |      ^
  |method copy in class A is already defined as def copy(x: Int): A at line 1.
  |The definitions have matching type signatures after erasure.
-- Error: try/CaseClassCopyMergeError.scala:1:0 --------------------------------
1 |case class A(x: Int) {
  |^
  |two or more overloaded variants of method copy have default arguments

We have a use case for this in inox (where we override copy to also also copy over some metadata), but I see how one would like to enforce some semantics for copy on case classes. In any case, this should produce a more intelligible error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions