Skip to content

Commit 87e434a

Browse files
committed
Help renaming conflicting givens
1 parent 245a519 commit 87e434a

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
@@ -2125,8 +2125,27 @@ extends NamingMsg(AlreadyDefinedID):
21252125
i" in ${conflicting.associatedFile}"
21262126
else if conflicting.owner == owner then ""
21272127
else i" in ${conflicting.owner}"
2128+
def print(tpe: Type): String =
2129+
def addParams(tpe: Type): List[String] = tpe match
2130+
case tpe: MethodType =>
2131+
val s = if tpe.isContextualMethod then i"(${tpe.paramInfos}%, %) =>" else ""
2132+
s :: addParams(tpe.resType)
2133+
case tpe: PolyType =>
2134+
i"[${tpe.paramNames}%, %] =>" :: addParams(tpe.resType)
2135+
case tpe =>
2136+
i"$tpe" :: Nil
2137+
addParams(tpe).mkString(" ")
21282138
def note =
2129-
if owner.is(Method) || conflicting.is(Method) then
2139+
if conflicting.is(Given) && name.startsWith("given_") then
2140+
i"""|
2141+
|
2142+
|Provide an explicit, unique name to given definitions,
2143+
|since the names assigned to anonymous givens may clash. For example:
2144+
|
2145+
| given myGiven: ${print(atPhase(typerPhase)(conflicting.info))} // define an instance
2146+
| given myGiven @ ${print(atPhase(typerPhase)(conflicting.info))} // as a pattern variable
2147+
|"""
2148+
else if owner.is(Method) || conflicting.is(Method) then
21302149
"\n\nNote that overloaded methods must all be defined in the same group of toplevel definitions"
21312150
else ""
21322151
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)