Skip to content

Commit 3100c7e

Browse files
som-snytttgodzik
authored andcommitted
Help renaming conflicting givens
[Cherry-picked 87e434a]
1 parent a41af9d commit 3100c7e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,27 @@ extends NamingMsg(AlreadyDefinedID):
20872087
i" in ${conflicting.associatedFile}"
20882088
else if conflicting.owner == owner then ""
20892089
else i" in ${conflicting.owner}"
2090+
def print(tpe: Type): String =
2091+
def addParams(tpe: Type): List[String] = tpe match
2092+
case tpe: MethodType =>
2093+
val s = if tpe.isContextualMethod then i"(${tpe.paramInfos}%, %) =>" else ""
2094+
s :: addParams(tpe.resType)
2095+
case tpe: PolyType =>
2096+
i"[${tpe.paramNames}%, %] =>" :: addParams(tpe.resType)
2097+
case tpe =>
2098+
i"$tpe" :: Nil
2099+
addParams(tpe).mkString(" ")
20902100
def note =
2091-
if owner.is(Method) || conflicting.is(Method) then
2101+
if conflicting.is(Given) && name.startsWith("given_") then
2102+
i"""|
2103+
|
2104+
|Provide an explicit, unique name to given definitions,
2105+
|since the names assigned to anonymous givens may clash. For example:
2106+
|
2107+
| given myGiven: ${print(atPhase(typerPhase)(conflicting.info))} // define an instance
2108+
| given myGiven @ ${print(atPhase(typerPhase)(conflicting.info))} // as a pattern variable
2109+
|"""
2110+
else if owner.is(Method) || conflicting.is(Method) then
20922111
"\n\nNote that overloaded methods must all be defined in the same group of toplevel definitions"
20932112
else ""
20942113
if conflicting.isTerm != name.isTermName then

tests/neg/i23119.check

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
-- [E161] Naming Error: tests/neg/i23119.scala:7:4 ---------------------------------------------------------------------
2-
7 | given Option[List[Int]] = Some(List(x)) // error
1+
-- [E161] Naming Error: tests/neg/i23119.scala:8:4 ---------------------------------------------------------------------
2+
8 | given Option[List[Int]] = Some(List(x)) // error
33
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
| given_Option_List is already defined as given instance given_Option_List
55
|
6-
| Note that overloaded methods must all be defined in the same group of toplevel definitions
6+
| Provide an explicit, unique name to given definitions,
7+
| since the names assigned to anonymous givens may clash. For example:
8+
|
9+
| given myGiven: Option[List[String]] // define an instance
10+
| given myGiven @ Option[List[String]] // as a pattern variable
11+
-- [E161] Naming Error: tests/neg/i23119.scala:18:8 --------------------------------------------------------------------
12+
18 | given [A] => List[A] = ??? // error
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| given_List_A is already defined as given instance given_List_A
15+
|
16+
| Provide an explicit, unique name to given definitions,
17+
| since the names assigned to anonymous givens may clash. For example:
18+
|
19+
| given myGiven: [A] => List[A] // define an instance
20+
| given myGiven @ [A] => List[A] // as a pattern variable

tests/neg/i23119.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//> using options -explain
12

23
@main def test = println:
34
for x <- 1 to 2
@@ -11,3 +12,8 @@
1112
// given_Option_List is already defined as given instance given_Option_List
1213
// Previously the naming clash was noticed when extracting values in the map or do function:
1314
// duplicate pattern variable: given_Option_List
15+
16+
def also =
17+
given [A] => List[A] = ???
18+
given [A] => List[A] = ??? // error
19+
()

0 commit comments

Comments
 (0)