Skip to content

Commit 8333a53

Browse files
som-snytttgodzik
authored andcommitted
Naming anon givens is a fix by renaming
[Cherry-picked 6fe3abc]
1 parent 3b5bd33 commit 8333a53

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
@@ -2390,13 +2390,22 @@ extends NamingMsg(DoubleDefinitionID):
23902390
def explain(using Context) =
23912391
def givenAddendum =
23922392
def isGivenName(sym: Symbol) = sym.name.startsWith("given_") // Desugar.inventGivenName
2393+
def print(tpe: Type): String =
2394+
def addParams(tpe: Type): List[String] = tpe match
2395+
case tpe: MethodType =>
2396+
val s = if tpe.isContextualMethod then i"(${tpe.paramInfos}%, %) =>" else ""
2397+
s :: addParams(tpe.resType)
2398+
case tpe: PolyType =>
2399+
i"[${tpe.paramNames}%, %] =>" :: addParams(tpe.resType)
2400+
case tpe =>
2401+
i"$tpe" :: Nil
2402+
addParams(tpe).mkString(" ")
23932403
if decl.is(Given) && previousDecl.is(Given) && isGivenName(decl) && isGivenName(previousDecl) then
2394-
i"""
2395-
|3. Provide an explicit, unique name to given definitions, since the names
2396-
| assigned to anonymous givens may clash. For example:
2397-
|
2398-
| given myGiven: ${atPhase(typerPhase)(decl.info)}
2399-
|"""
2404+
i"""| Provide an explicit, unique name to given definitions,
2405+
| since the names assigned to anonymous givens may clash. For example:
2406+
|
2407+
| given myGiven: ${print(atPhase(typerPhase)(decl.info))}
2408+
|"""
24002409
else ""
24012410
decl.signature.matchDegree(previousDecl.signature) match
24022411
case FullMatch =>
@@ -2420,15 +2429,15 @@ extends NamingMsg(DoubleDefinitionID):
24202429
|
24212430
|so the compiler cannot keep both: the generated bytecode symbols would collide.
24222431
|
2423-
|To fix this error, you need to disambiguate the two definitions. You can either:
2432+
|To fix this error, you must disambiguate the two definitions by doing one of the following:
24242433
|
2425-
|1. Rename one of the definitions, or
2434+
|1. Rename one of the definitions.$givenAddendum
24262435
|2. Keep the same names in source but give one definition a distinct
24272436
| bytecode-level name via `@targetName`; for example:
24282437
|
24292438
| @targetName("${decl.name.show}_2")
24302439
| ${atPhase(typerPhase)(decl.showDcl)}
2431-
|$givenAddendum
2440+
|
24322441
|Choose the `@targetName` argument carefully: it is the name that will be used
24332442
|when calling the method externally, so it should be unique and descriptive.
24342443
|"""

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)