Skip to content

Spurious override error when defining a class that refers to a class type parameter in a secondary constructor #12632

Closed
@jphmrst

Description

@jphmrst

Compiler version

Via print scalaVersion from sbt: 3.0.0

Minimized code

class CCC[S](val i: Int) {
  def this() =
    this(
      {
        val z = new Ordering[S] {
          override def compare(x: S, y: S): Int = ???
        }
        3
      })
}

Output

[info] compiling 1 Scala source to /home/jm/tmp/test/target/scala-3.0.0/classes ...
[error] -- [E120] Naming Error: /home/jm/tmp/test/src/main/scala/Main.scala:6:16 ------
[error] 6 |        val z = new Ordering[S] {
[error]   |                ^
[error]   |Name clash between defined and inherited member:
[error]   |def compare(x: T, y: T): Int in trait Ordering and
[error]   |override def compare(x: S, y: S): Int in anonymous class Object with Ordering[S] {...} at line 7
[error]   |have the same type after erasure.
[error]   |
[error]   |Consider adding a @targetName annotation to one of the conflicting definitions
[error]   |for disambiguation.
[error] one error found
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed May 27, 2021, 2:35:52 PM

Expectation

I would expect the compile to match the overridden method to the inherited abstract trait method, and for the code to compile without warning. (This is boiled down from some code I'm moving from Scala2 to 3, and which does make nontrivial use of the val-binding.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions