Skip to content

Commit b63dd6a

Browse files
oderskybishabosha
authored andcommitted
Change naming scheme for anonymous extensions
They now start with `extension_` instead of `given_`.
1 parent 90492d7 commit b63dd6a

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,14 @@ object desugar {
954954
else tree
955955
}
956956

957-
/** Invent a name for an anonympus given of type or template `impl`. */
958-
def inventGivenName(impl: Tree)(implicit ctx: Context): SimpleName =
959-
s"given_${inventName(impl)}".toTermName.asSimpleName
960-
961957
/** The normalized name of `mdef`. This means
962958
* 1. Check that the name does not redefine a Scala core class.
963959
* If it does redefine, issue an error and return a mangled name instead of the original one.
964960
* 2. If the name is missing (this can be the case for instance definitions), invent one instead.
965961
*/
966962
def normalizeName(mdef: MemberDef, impl: Tree)(implicit ctx: Context): Name = {
967963
var name = mdef.name
968-
if (name.isEmpty) name = name.likeSpaced(inventGivenName(impl))
964+
if (name.isEmpty) name = name.likeSpaced(inventGivenOrExtensionName(impl))
969965
if (ctx.owner == defn.ScalaPackageClass && defn.reservedScalaClassNames.contains(name.toTypeName)) {
970966
def kind = if (name.isTypeName) "class" else "object"
971967
ctx.error(em"illegal redefinition of standard $kind $name", mdef.sourcePos)
@@ -974,27 +970,26 @@ object desugar {
974970
name
975971
}
976972

977-
/** Invent a name for an anonymous instance with template `impl`.
978-
*/
979-
private def inventName(impl: Tree)(implicit ctx: Context): String = impl match {
980-
case impl: Template =>
981-
if (impl.parents.isEmpty)
982-
impl.body.find {
983-
case dd: DefDef if dd.mods.is(Extension) => true
984-
case _ => false
985-
}
986-
match {
987-
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
988-
s"${name}_of_${inventTypeName(vparam.tpt)}"
989-
case _ =>
990-
ctx.error(i"anonymous instance must implement a type or have at least one extension method", impl.sourcePos)
991-
nme.ERROR.toString
992-
}
993-
else
994-
impl.parents.map(inventTypeName(_)).mkString("_")
995-
case impl: Tree =>
996-
inventTypeName(impl)
997-
}
973+
/** Invent a name for an anonympus given or extension of type or template `impl`. */
974+
def inventGivenOrExtensionName(impl: Tree)(given ctx: Context): SimpleName =
975+
val str = impl match
976+
case impl: Template =>
977+
if impl.parents.isEmpty then
978+
impl.body.find {
979+
case dd: DefDef if dd.mods.is(Extension) => true
980+
case _ => false
981+
}
982+
match
983+
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
984+
s"extension_${name}_${inventTypeName(vparam.tpt)}"
985+
case _ =>
986+
ctx.error(i"anonymous instance must implement a type or have at least one extension method", impl.sourcePos)
987+
nme.ERROR.toString
988+
else
989+
impl.parents.map(inventTypeName(_)).mkString("given_", "_", "")
990+
case impl: Tree =>
991+
"given_" ++ inventTypeName(impl)
992+
str.toTermName.asSimpleName
998993

999994
private class NameExtractor(followArgs: Boolean) extends UntypedTreeAccumulator[String] {
1000995
private def extractArgs(args: List[Tree])(implicit ctx: Context): String =

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ class Typer extends Namer
15411541
var name = tree.name
15421542
if (name == nme.WILDCARD && tree.mods.is(Given)) {
15431543
val Typed(_, tpt): @unchecked = tree.body
1544-
name = desugar.inventGivenName(tpt)
1544+
name = desugar.inventGivenOrExtensionName(tpt)
15451545
}
15461546
if (name == nme.WILDCARD) body1
15471547
else {

docs/docs/reference/contextual/extension-methods.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ given listOps: AnyRef {
158158
def [T](xs: List[T]) second = xs.tail.head
159159
def [T](xs: List[T]) third: T = xs.tail.tail.head
160160
}
161-
given given_largest_of_List_T: AnyRef {
161+
given extension_largest_List_T: AnyRef {
162162
def [T](xs: List[T]) largest (given Ordering[T])(n: Int) =
163163
xs.sorted.takeRight(n)
164164
}

docs/docs/reference/contextual/relationship-implicits.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ The synthesized type names are formed from
6161
Tuples are treated as transparent, i.e. a type `F[(X, Y)]` would get the synthesized name
6262
`F_X_Y`. Directly implemented function types `A => B` are represented as `A_to_B`. Function types used as arguments to other type constructors are represented as `Function`.
6363

64-
Anonymous given instances that define extension methods
65-
get their name from the name of the first extension method and the toplevel type
66-
constructor of its first parameter. For example, the given instance
64+
### Anonymous Collective Extensions
65+
66+
Anonymous collective extensions also get compiler synthesized names, which are formed from
67+
68+
- the prefix `extension_`
69+
- the name of the first defined extension method
70+
- the simple name of the first parameter type of this extension method
71+
- the simple name(s) of the toplevel argument type constructors to this type.
72+
73+
For example, the extension
6774
```scala
6875
extension of [T] (xs: List[T]) with {
6976
def second = ...
7077
}
7178
```
72-
gets the synthesized name `given_second_of_List_T`.
79+
gets the synthesized name `extension_second_List_T`.
80+
7381

7482
### Given Clauses
7583

0 commit comments

Comments
 (0)