|
| 1 | +-- [E120] Naming Error: tests/neg/i23350.scala:8:7 --------------------------------------------------------------------- |
| 2 | +8 |object D extends A: // error |
| 3 | + | ^ |
| 4 | + | Name clash between defined and inherited member: |
| 5 | + | def apply(p: A.this.Props): Unit in class A at line 5 and |
| 6 | + | def apply(a: UndefOr2[String]): Unit in object D at line 10 |
| 7 | + | have the same type (a: Object): Unit after erasure. |
| 8 | + | |
| 9 | + | Consider adding a @targetName annotation to one of the conflicting definitions |
| 10 | + | for disambiguation. |
| 11 | + |--------------------------------------------------------------------------------------------------------------------- |
| 12 | + | Explanation (enabled by `-explain`) |
| 13 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 14 | + | |
| 15 | + | As part of the Scala compilation pipeline every type is reduced to its erased |
| 16 | + | (runtime) form. In this phase, among other transformations, generic parameters |
| 17 | + | disappear and separate parameter-list boundaries are flattened. |
| 18 | + | |
| 19 | + | For example, both `f[T](x: T)(y: String): Unit` and `f(x: Any, z: String): Unit` |
| 20 | + | erase to the same runtime signature `f(x: Object, y: String): Unit`. Note that |
| 21 | + | parameter names are irrelevant. |
| 22 | + | |
| 23 | + | In your code the two declarations |
| 24 | + | |
| 25 | + | def apply(p: A.this.Props): Unit |
| 26 | + | def apply(a: UndefOr2[String]): Unit |
| 27 | + | |
| 28 | + | erase to the identical signature |
| 29 | + | |
| 30 | + | (a: Object): Unit |
| 31 | + | |
| 32 | + | so the compiler cannot keep both: the generated bytecode symbols would collide. |
| 33 | + | |
| 34 | + | To fix this error, you need to disambiguate the two definitions. You can either: |
| 35 | + | |
| 36 | + | 1. Rename one of the definitions, or |
| 37 | + | 2. Keep the same names in source but give one definition a distinct |
| 38 | + | bytecode-level name via `@targetName` for example: |
| 39 | + | |
| 40 | + | @targetName("apply_2") |
| 41 | + | def apply(a: UndefOr2[String]): Unit |
| 42 | + | |
| 43 | + | Choose the `@targetName` argument carefully: it is the name that will be used |
| 44 | + | when calling the method externally, so it should be unique and descriptive. |
| 45 | + | |
| 46 | + --------------------------------------------------------------------------------------------------------------------- |
0 commit comments