Skip to content

Commit 6fe3abc

Browse files
committed
Naming anon givens is a fix by renaming
1 parent 912fce1 commit 6fe3abc

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,13 +2428,22 @@ extends NamingMsg(DoubleDefinitionID):
24282428
def explain(using Context) =
24292429
def givenAddendum =
24302430
def isGivenName(sym: Symbol) = sym.name.startsWith("given_") // Desugar.inventGivenName
2431+
def print(tpe: Type): String =
2432+
def addParams(tpe: Type): List[String] = tpe match
2433+
case tpe: MethodType =>
2434+
val s = if tpe.isContextualMethod then i"(${tpe.paramInfos}%, %) =>" else ""
2435+
s :: addParams(tpe.resType)
2436+
case tpe: PolyType =>
2437+
i"[${tpe.paramNames}%, %] =>" :: addParams(tpe.resType)
2438+
case tpe =>
2439+
i"$tpe" :: Nil
2440+
addParams(tpe).mkString(" ")
24312441
if decl.is(Given) && previousDecl.is(Given) && isGivenName(decl) && isGivenName(previousDecl) then
2432-
i"""
2433-
|3. Provide an explicit, unique name to given definitions, since the names
2434-
| assigned to anonymous givens may clash. For example:
2435-
|
2436-
| given myGiven: ${atPhase(typerPhase)(decl.info)}
2437-
|"""
2442+
i"""| Provide an explicit, unique name to given definitions,
2443+
| since the names assigned to anonymous givens may clash. For example:
2444+
|
2445+
| given myGiven: ${print(atPhase(typerPhase)(decl.info))}
2446+
|"""
24382447
else ""
24392448
decl.signature.matchDegree(previousDecl.signature) match
24402449
case FullMatch =>
@@ -2458,15 +2467,15 @@ extends NamingMsg(DoubleDefinitionID):
24582467
|
24592468
|so the compiler cannot keep both: the generated bytecode symbols would collide.
24602469
|
2461-
|To fix this error, you need to disambiguate the two definitions. You can either:
2470+
|To fix this error, you must disambiguate the two definitions by doing one of the following:
24622471
|
2463-
|1. Rename one of the definitions, or
2472+
|1. Rename one of the definitions.$givenAddendum
24642473
|2. Keep the same names in source but give one definition a distinct
24652474
| bytecode-level name via `@targetName`; for example:
24662475
|
24672476
| @targetName("${decl.name.show}_2")
24682477
| ${atPhase(typerPhase)(decl.showDcl)}
2469-
|$givenAddendum
2478+
|
24702479
|Choose the `@targetName` argument carefully: it is the name that will be used
24712480
|when calling the method externally, so it should be unique and descriptive.
24722481
|"""

tests/neg/i23350.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
|
3232
| so the compiler cannot keep both: the generated bytecode symbols would collide.
3333
|
34-
| To fix this error, you need to disambiguate the two definitions. You can either:
34+
| To fix this error, you must disambiguate the two definitions by doing one of the following:
3535
|
36-
| 1. Rename one of the definitions, or
36+
| 1. Rename one of the definitions.
3737
| 2. Keep the same names in source but give one definition a distinct
3838
| bytecode-level name via `@targetName`; for example:
3939
|

tests/neg/i23402.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
|
3232
| so the compiler cannot keep both: the generated bytecode symbols would collide.
3333
|
34-
| To fix this error, you need to disambiguate the two definitions. You can either:
34+
| To fix this error, you must disambiguate the two definitions by doing one of the following:
3535
|
36-
| 1. Rename one of the definitions, or
36+
| 1. Rename one of the definitions.
3737
| 2. Keep the same names in source but give one definition a distinct
3838
| bytecode-level name via `@targetName`; for example:
3939
|

tests/neg/i23832a.check

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@
2727
|
2828
| so the compiler cannot keep both: the generated bytecode symbols would collide.
2929
|
30-
| To fix this error, you need to disambiguate the two definitions. You can either:
30+
| To fix this error, you must disambiguate the two definitions by doing one of the following:
31+
|
32+
| 1. Rename one of the definitions. Provide an explicit, unique name to given definitions,
33+
| since the names assigned to anonymous givens may clash. For example:
34+
|
35+
| given myGiven: Special[Option[Int]]
3136
|
32-
| 1. Rename one of the definitions, or
3337
| 2. Keep the same names in source but give one definition a distinct
3438
| bytecode-level name via `@targetName`; for example:
3539
|
3640
| @targetName("given_Special_Option_2")
3741
| final lazy given val given_Special_Option: Special[Option[Int]]
3842
|
39-
| 3. Provide an explicit, unique name to given definitions, since the names
40-
| assigned to anonymous givens may clash. For example:
41-
|
42-
| given myGiven: Special[Option[Int]]
43-
|
4443
| Choose the `@targetName` argument carefully: it is the name that will be used
4544
| when calling the method externally, so it should be unique and descriptive.
4645
---------------------------------------------------------------------------------------------------------------------

tests/neg/i23832b.check

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@
2727
|
2828
| so the compiler cannot keep both: the generated bytecode symbols would collide.
2929
|
30-
| To fix this error, you need to disambiguate the two definitions. You can either:
30+
| To fix this error, you must disambiguate the two definitions by doing one of the following:
31+
|
32+
| 1. Rename one of the definitions. Provide an explicit, unique name to given definitions,
33+
| since the names assigned to anonymous givens may clash. For example:
34+
|
35+
| given myGiven: [A] => Special[Option[A]]
3136
|
32-
| 1. Rename one of the definitions, or
3337
| 2. Keep the same names in source but give one definition a distinct
3438
| bytecode-level name via `@targetName`; for example:
3539
|
3640
| @targetName("given_Special_Option_2")
3741
| final given def given_Special_Option[A]: Special[Option[A]]
3842
|
39-
| 3. Provide an explicit, unique name to given definitions, since the names
40-
| assigned to anonymous givens may clash. For example:
41-
|
42-
| given myGiven: [A]: Special[Option[A]]
43-
|
4443
| Choose the `@targetName` argument carefully: it is the name that will be used
4544
| when calling the method externally, so it should be unique and descriptive.
4645
---------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)