@@ -2326,7 +2326,7 @@ class JavaSymbolIsNotAValue(symbol: Symbol)(using Context) extends TypeMsg(JavaS
23262326}
23272327
23282328class DoubleDefinition (decl : Symbol , previousDecl : Symbol , base : Symbol )(using Context )
2329- extends NamingMsg (DoubleDefinitionID ) {
2329+ extends NamingMsg (DoubleDefinitionID ):
23302330 import Signature .MatchDegree .*
23312331
23322332 private def erasedType : Type =
@@ -2388,6 +2388,25 @@ extends NamingMsg(DoubleDefinitionID) {
23882388 } + details
23892389 }
23902390 def explain (using Context ) =
2391+ def givenAddendum =
2392+ 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(" " )
2403+ if decl.is(Given ) && previousDecl.is(Given ) && isGivenName(decl) && isGivenName(previousDecl) then
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+ | """
2409+ else " "
23912410 decl.signature.matchDegree(previousDecl.signature) match
23922411 case FullMatch =>
23932412 i """
@@ -2401,30 +2420,29 @@ extends NamingMsg(DoubleDefinitionID) {
24012420 |
24022421 |In your code the two declarations
24032422 |
2404- | ${previousDecl.showDcl}
2405- | ${decl.showDcl}
2423+ | ${atPhase(typerPhase)( previousDecl.showDcl) }
2424+ | ${atPhase(typerPhase)( decl.showDcl) }
24062425 |
24072426 |erase to the identical signature
24082427 |
24092428 | ${erasedType}
24102429 |
24112430 |so the compiler cannot keep both: the generated bytecode symbols would collide.
24122431 |
2413- |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 :
24142433 |
2415- |1. Rename one of the definitions, or
2434+ |1. Rename one of the definitions. $givenAddendum
24162435 |2. Keep the same names in source but give one definition a distinct
2417- | bytecode-level name via `@targetName` for example:
2436+ | bytecode-level name via `@targetName`; for example:
24182437 |
24192438 | @targetName(" ${decl.name.show}_2")
2420- | ${decl.showDcl}
2439+ | ${atPhase(typerPhase)( decl.showDcl) }
24212440 |
24222441 |Choose the `@targetName` argument carefully: it is the name that will be used
24232442 |when calling the method externally, so it should be unique and descriptive.
2424- """
2443+ | """
24252444 case _ => " "
2426-
2427- }
2445+ end DoubleDefinition
24282446
24292447class ImportRenamedTwice (ident : untpd.Ident )(using Context ) extends SyntaxMsg (ImportRenamedTwiceID ) {
24302448 def msg (using Context ) = s " ${ident.show} is renamed twice on the same import line. "
0 commit comments