Skip to content

Refactorings on Names: forbid .name on Name and cleanups on the way #4834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object NameOps {
}
}

implicit class NameDecorator[N <: Name](val name: N) extends AnyVal {
implicit class NameDecorator[N <: Name](private val name: N) extends AnyVal {
import nme._

def testSimple(f: SimpleName => Boolean): Boolean = name match {
Expand All @@ -58,8 +58,8 @@ object NameOps {
case _ => false
}

def likeSpaced(n: PreName): N =
(if (name.isTermName) n.toTermName else n.toTypeName).asInstanceOf[N]
private def likeSpaced(n: PreName): N =
name.likeSpaced(n).asInstanceOf[N]

def isConstructorName = name == CONSTRUCTOR || name == TRAIT_CONSTRUCTOR
def isStaticConstructorName = name == STATIC_CONSTRUCTOR
Expand Down Expand Up @@ -233,12 +233,12 @@ object NameOps {
case nme.clone_ => nme.clone_
}

def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): name.ThisName = {
def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): N = {

val methodTags: Seq[Name] = (methodTargs zip methodTarsNames).sortBy(_._2).map(x => defn.typeTag(x._1))
val classTags: Seq[Name] = (classTargs zip classTargsNames).sortBy(_._2).map(x => defn.typeTag(x._1))

name.likeSpaced(name ++ nme.specializedTypeNames.prefix ++
likeSpaced(name ++ nme.specializedTypeNames.prefix ++
methodTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.separator ++
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
}
Expand Down Expand Up @@ -271,7 +271,7 @@ object NameOps {
}
}

implicit class TermNameDecorator(val name: TermName) extends AnyVal {
implicit class TermNameDecorator(private val name: TermName) extends AnyVal {
import nme._

def setterName: TermName = name.exclude(FieldName) ++ str.SETTER_SUFFIX
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object Names {
/** A name in the same (term or type) namespace as this name and
* with same characters as given `name`.
*/
def likeSpaced(name: Name): ThisName
def likeSpaced(name: PreName): ThisName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, there don't seems to be usages for likeSpaced(PreName) so I would keep the API as is. If you want to share the implementation with the one in NameOps, it seems you can actually replace PreName by Name


/** A derived name consisting of this name and the added info, unless it is
* already present in this name.
Expand Down Expand Up @@ -181,7 +181,7 @@ object Names {
_typeName
}

override def likeSpaced(name: Name): TermName = name.toTermName
override def likeSpaced(name: PreName): TermName = name.toTermName

def info: NameInfo = SimpleNameKind.info
def underlying: TermName = unsupported("underlying")
Expand Down Expand Up @@ -450,7 +450,7 @@ object Names {
override def mapLast(f: SimpleName => SimpleName) = toTermName.mapLast(f).toTypeName
override def mapParts(f: SimpleName => SimpleName) = toTermName.mapParts(f).toTypeName

override def likeSpaced(name: Name): TypeName = name.toTypeName
override def likeSpaced(name: PreName): TypeName = name.toTypeName

override def derived(info: NameInfo): TypeName = toTermName.derived(info).toTypeName
override def exclude(kind: NameKind): TypeName = toTermName.exclude(kind).toTypeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,26 +1260,26 @@ object messages {
|""".stripMargin
}

case class OverloadedOrRecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
case class OverloadedOrRecursiveMethodNeedsResultType(method: Names.TermName)(implicit ctx: Context)
extends Message(OverloadedOrRecursiveMethodNeedsResultTypeID) {
val kind = "Syntax"
val msg = hl"""overloaded or recursive method ${tree} needs return type"""
val msg = hl"""overloaded or recursive method ${method} needs return type"""
val explanation =
hl"""Case 1: ${tree} is overloaded
|If there are multiple methods named `${tree.name}` and at least one definition of
hl"""Case 1: ${method} is overloaded
|If there are multiple methods named `${method}` and at least one definition of
|it calls another, you need to specify the calling method's return type.
|
|Case 2: ${tree} is recursive
|If `${tree.name}` calls itself on any path, you need to specify its return type.
|Case 2: ${method} is recursive
|If `${method}` calls itself on any path, you need to specify its return type.
|""".stripMargin
}

case class RecursiveValueNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
case class RecursiveValueNeedsResultType(value: Names.TermName)(implicit ctx: Context)
extends Message(RecursiveValueNeedsResultTypeID) {
val kind = "Syntax"
val msg = hl"""recursive value ${tree.name} needs type"""
val msg = hl"""recursive value ${value} needs type"""
val explanation =
hl"""The definition of `${tree.name}` is recursive and you need to specify its type.
hl"""The definition of `${value}` is recursive and you need to specify its type.
|""".stripMargin
}

Expand Down