scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Unhelpful error message for type erasure

Open threeseed opened this issue 6 months ago • 0 comments

Compiler version

3.7.0

Minimized example

import scala.scalajs.js

abstract class A:
  type Props
  def apply(p: Props) = ()

type UndefOr2[A] = A | Unit
object D extends A:
  case class Props()
  def apply(a: UndefOr2[String]) = ()

Output Error/Warning message

Name clash between defined and inherited member:
def apply(p: A.this.Props): Unit in class A at line 7 and
def apply(a: Playground.UndefOr2[String]): Unit in object D at line 27
have the same type after erasure.

Consider adding a @targetName annotation to one of the conflicting definitions
for disambiguation 

Why this Error/Warning was not helpful

It doesn't tell me what the "same type" actually is.

Suggested improvement

Ideally it would be something like this:

We need to have the class be the "least specific" type it can be in the bytecode. For A: there's an invisible >: Nothing <: Any, meaning the least specific type is Any for which the class is Object. For D: the type is SomeCaseClass | Unit, for which the shared class is Object As both A and D are of type Object they are in conflict.

Consider adding a @targetName annotation to either A or D.

threeseed avatar Jun 10 '25 23:06 threeseed