Skip to content

Commit c657425

Browse files
mboveltgodzik
authored andcommitted
Add regression test for scala#23350
[Cherry-picked 3c892d3]
1 parent 8b9a34d commit c657425

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

tests/neg/i23350.check

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i23350.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//> using options -explain
2+
3+
abstract class A:
4+
type Props
5+
def apply(p: Props) = ()
6+
7+
type UndefOr2[A] = A | Unit
8+
object D extends A: // error
9+
case class Props()
10+
def apply(a: UndefOr2[String]) = ()

0 commit comments

Comments
 (0)