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 all 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
36 changes: 18 additions & 18 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 likeSpacedN(n: Name): N =
name.likeSpaced(n).asInstanceOf[N]

def isConstructorName = name == CONSTRUCTOR || name == TRAIT_CONSTRUCTOR
def isStaticConstructorName = name == STATIC_CONSTRUCTOR
Expand Down Expand Up @@ -100,7 +100,7 @@ object NameOps {
* method needs to work on mangled as well as unmangled names because
* it is also called from the backend.
*/
def stripModuleClassSuffix: N = likeSpaced {
def stripModuleClassSuffix: N = likeSpacedN {
val semName = name.toTermName match {
case name: SimpleName if name.endsWith("$") => name.unmangleClassName
case _ => name
Expand All @@ -109,7 +109,7 @@ object NameOps {
}

/** If flags is a ModuleClass but not a Package, add module class suffix */
def adjustIfModuleClass(flags: Flags.FlagSet): N = likeSpaced {
def adjustIfModuleClass(flags: Flags.FlagSet): N = likeSpacedN {
if (flags is (ModuleClass, butNot = Package)) name.asTypeName.moduleClassName
else name.toTermName.exclude(AvoidClashName)
}
Expand All @@ -119,27 +119,27 @@ object NameOps {
* followed by `kind` and the name.
*/
def expandedName(base: Symbol, kind: QualifiedNameKind = ExpandedName)(implicit ctx: Context): N =
likeSpaced { base.fullNameSeparated(ExpandPrefixName, kind, name) }
likeSpacedN { base.fullNameSeparated(ExpandPrefixName, kind, name) }

/** Revert the expanded name. */
def unexpandedName: N = likeSpaced {
def unexpandedName: N = likeSpacedN {
name.rewrite { case ExpandedName(_, unexp) => unexp }
}

/** Remove the variance from the name. */
def invariantName: N = likeSpaced {
def invariantName: N = likeSpacedN {
name.rewrite { case VariantName(invariant, _) => invariant }
}

def implClassName: N = likeSpaced(name ++ tpnme.IMPL_CLASS_SUFFIX)
def implClassName: N = likeSpacedN(name ++ tpnme.IMPL_CLASS_SUFFIX)

def traitOfImplClassName: N = {
val suffix = tpnme.IMPL_CLASS_SUFFIX.toString
assert(name.endsWith(suffix), name)
likeSpaced(name.mapLast(_.dropRight(suffix.length)))
likeSpacedN(name.mapLast(_.dropRight(suffix.length)))
}

def errorName: N = likeSpaced(name ++ nme.ERROR)
def errorName: N = likeSpacedN(name ++ nme.ERROR)

/** Map variance value -1, +1 to 0, 1 */
private def varianceToNat(v: Int) = (v + 1) / 2
Expand All @@ -150,7 +150,7 @@ object NameOps {
/** Name with variance prefix: `+` for covariant, `-` for contravariant */
def withVariance(v: Int): N = {
val underlying = name.exclude(VariantName)
likeSpaced(
likeSpacedN(
if (v == 0) underlying
else VariantName(underlying.toTermName, varianceToNat(v)))
}
Expand All @@ -160,7 +160,7 @@ object NameOps {
*/
def variance = name.collect { case VariantName(_, n) => natToVariance(n) }.getOrElse(0)

def freshened(implicit ctx: Context): N = likeSpaced {
def freshened(implicit ctx: Context): N = likeSpacedN {
name.toTermName match {
case ModuleClassName(original) => ModuleClassName(original.freshened)
case name => UniqueName.fresh(name)
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 ++
likeSpacedN(name ++ nme.specializedTypeNames.prefix ++
methodTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.separator ++
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
}
Expand All @@ -249,11 +249,11 @@ object NameOps {
def unmangleClassName: N = name.toTermName match {
case name: SimpleName
if name.endsWith(str.MODULE_SUFFIX) && !nme.falseModuleClassNames.contains(name) =>
likeSpaced(name.dropRight(str.MODULE_SUFFIX.length).moduleClassName)
likeSpacedN(name.dropRight(str.MODULE_SUFFIX.length).moduleClassName)
case _ => name
}

def unmangle(kind: NameKind): N = likeSpaced {
def unmangle(kind: NameKind): N = likeSpacedN {
name rewrite {
case unmangled: SimpleName =>
kind.unmangle(unmangled)
Expand All @@ -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
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