Skip to content

Commit 3b43fa9

Browse files
committed
Use semantic names for augmentations
Also, display anonymous augmentation names in nicer form.
1 parent 3e10366 commit 3b43fa9

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import core._
66
import util.Positions._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
77
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._
88
import Decorators._, transform.SymUtils._
9-
import NameKinds.{UniqueName, EvidenceParamName, DefaultGetterName}
9+
import NameKinds.{UniqueName, EvidenceParamName, DefaultGetterName, AugmentName}
1010
import language.higherKinds
1111
import typer.FrontEnd
1212
import collection.mutable.ListBuffer
@@ -778,16 +778,16 @@ object desugar {
778778
*
779779
* where
780780
*
781+
* <deco-name> = <id>, if there is a `<id> @` binding
782+
* = unqiue, expanded name relative to top-level class of <deco-core>, otherwise
783+
* <deco-core> = "_augment_<from>_to_<to>" if <to> is nonempty
784+
* = "_augment_<from>" otherwise
785+
* <from> = underlying type name of <decorated>, or ""
786+
* <to> = underlying type name of first extended parent, or ""
787+
*
781788
* (<decorated>, <type-params0>) = decomposeTypePattern(<type-pattern>)
782789
* (<type-params>, <evidence-params>) = desugarTypeBindings(<type-params0>)
783790
* <combined-params> = <params> concatenated with <evidence-params> in one clause
784-
* <deconame> = <id> if there is a `id @` binding
785-
* = <from>To<parent>_in_<location>$$<n> where <parent> is first extended class name
786-
*
787-
* = <from>Augmentation_in_<location>$$<n> if no such <parent> exists
788-
* <from> = underlying type name of <decorated>
789-
* <location> = flat name of enclosing toplevel class
790-
* <n> = counter making prefix unique
791791
* <body1> = <body> with each occurrence of unqualified `this` substituted by `$this`.
792792
*
793793
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -800,7 +800,7 @@ object desugar {
800800
* where
801801
*
802802
* <body2> = <body1> where each method definition gets <combined-params> as last parameter section.
803-
* <deconame>, <type-params> are as above.
803+
* <deco-name>, <type-params> are as above.
804804
*/
805805
def augmentation(tree: Augment)(implicit ctx: Context): Tree = {
806806
val Augment(id, augmented, impl) = tree
@@ -818,11 +818,11 @@ object desugar {
818818
def clsName(tree: Tree): String = leadingName("", tree)
819819
val fromName = clsName(augmented)
820820
val toName = impl.parents match {
821-
case parent :: _ if !clsName(parent).isEmpty => "To" + clsName(parent)
822-
case _ => "Augmentation"
821+
case parent :: _ if !clsName(parent).isEmpty => "_to_" + clsName(parent)
822+
case _ => ""
823823
}
824-
val prefix = s"${fromName}${toName}_in_${ctx.owner.topLevelClass.flatName}"
825-
UniqueName.fresh(prefix.toTermName).toTypeName
824+
val core = s"${str.AUGMENT}$fromName$toName".toTermName
825+
AugmentName.fresh(core.expandedName(ctx.owner.topLevelClass)).toTypeName
826826
}
827827

828828
val firstParam = ValDef(nme.SELF, decorated, EmptyTree).withFlags(Private | Local | ParamAccessor)

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ object NameKinds {
287287
val SkolemName = new UniqueNameKind("?")
288288
val LiftedTreeName = new UniqueNameKind("liftedTree")
289289
val SuperArgName = new UniqueNameKind("$superArg$")
290+
val AugmentName = new UniqueNameKind("_")
290291

291292
/** A kind of unique extension methods; Unlike other unique names, these can be
292293
* unmangled.

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ object StdNames {
2828
final val LOCALDUMMY_PREFIX = "<local " // owner of local blocks
2929
final val ANON_CLASS = "$anon"
3030
final val ANON_FUN = "$anonfun"
31+
final val AUGMENT = "_augment_"
3132

3233
final val REPL_SESSION_LINE = "rs$line$"
3334
final val REPL_ASSIGN_SUFFIX = "$assign"

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import Flags._
88
import Names._
99
import Symbols._
1010
import NameOps._
11+
import NameKinds.{AugmentName, ExpandedName}
1112
import Constants._
1213
import TypeErasure.ErasedValueType
1314
import Contexts.Context
1415
import Scopes.Scope
1516
import Denotations._
1617
import SymDenotations._
1718
import Annotations.Annotation
18-
import StdNames.{nme, tpnme}
19+
import StdNames.{nme, tpnme, str}
1920
import ast.{Trees, tpd, untpd}
2021
import typer.{Implicits, Inliner, Namer}
2122
import typer.ProtoTypes._
@@ -721,6 +722,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
721722
case info: ImportType => return s"import $info.expr.show"
722723
case _ =>
723724
}
725+
sym.name.toTermName match {
726+
case AugmentName(ExpandedName(_, core), n) =>
727+
assert(core.startsWith(str.AUGMENT))
728+
return Str(s"augmentation ${core.drop(str.AUGMENT.length)}") ~ (Str(s"/$n") provided n > 1)
729+
case _ =>
730+
}
731+
724732
if (sym.is(ModuleClass))
725733
kindString(sym) ~~ (nameString(sym.name.stripModuleClassSuffix) + idString(sym))
726734
else

tests/neg/augment.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ object augments {
7373
this.width == that.width &&
7474
this.height == that.height
7575
}
76+
77+
augment List[List[type U]] {
78+
def flattened: List[U] = (this :\ (Nil: List[U]))(_ ++ _)
79+
}
7680
}
7781

7882
object augments2 {
@@ -108,8 +112,7 @@ object Test extends App {
108112
println(r1 === r1)
109113
println(r1 === r2)
110114
println(List(1, 2, 3).second)
111-
println(List(List(1), List(2, 3)).flattened)
112-
println(List(List(1), List(2, 3)).flattened.maxx)
115+
println(List(List(1), List(2, 3)).flattened) // error: type error + note that implicit conversions are ambiguous
113116
println(Array(1, 2, 3).maxx)
114117
println((2, 3).isSame)
115118
println((3, 3).isSame)

tests/run/augment.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ object augments {
7878
this.width == that.width &&
7979
this.height == that.height
8080
}
81-
8281
}
8382

8483
object augments2 {

0 commit comments

Comments
 (0)