Skip to content

Commit 5266af5

Browse files
authored
Refactorings on Names: forbid .name on Name and cleanups on the way (#4834)
* Simplify type of NameOps.specializedFor The current type is inconsistent with other uses of likeSpaced. And mentioning `name` in a return type would prevent making it private as in next commit. * Stop accidental support for `.name` on Name This is easy for `TermNameDecorator`, and last commit makes it easy for `NameDecorator`. * Avoid misleading `tree: Name` This lets you think that `tree.name` is sensible tho it isn't, and for more than an year (since ce333b1). * Generalize Name.likeSpaced and hide the one from NameDecorator * Rename NameOps.likeSpaced to prevent confusion * Address review: Make likeSpaced* take Name, not PreName
1 parent d79b57e commit 5266af5

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object NameOps {
4949
}
5050
}
5151

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

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

61-
def likeSpaced(n: PreName): N =
62-
(if (name.isTermName) n.toTermName else n.toTypeName).asInstanceOf[N]
61+
private def likeSpacedN(n: Name): N =
62+
name.likeSpaced(n).asInstanceOf[N]
6363

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

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

124124
/** Revert the expanded name. */
125-
def unexpandedName: N = likeSpaced {
125+
def unexpandedName: N = likeSpacedN {
126126
name.rewrite { case ExpandedName(_, unexp) => unexp }
127127
}
128128

129129
/** Remove the variance from the name. */
130-
def invariantName: N = likeSpaced {
130+
def invariantName: N = likeSpacedN {
131131
name.rewrite { case VariantName(invariant, _) => invariant }
132132
}
133133

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

136136
def traitOfImplClassName: N = {
137137
val suffix = tpnme.IMPL_CLASS_SUFFIX.toString
138138
assert(name.endsWith(suffix), name)
139-
likeSpaced(name.mapLast(_.dropRight(suffix.length)))
139+
likeSpacedN(name.mapLast(_.dropRight(suffix.length)))
140140
}
141141

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

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

163-
def freshened(implicit ctx: Context): N = likeSpaced {
163+
def freshened(implicit ctx: Context): N = likeSpacedN {
164164
name.toTermName match {
165165
case ModuleClassName(original) => ModuleClassName(original.freshened)
166166
case name => UniqueName.fresh(name)
@@ -233,12 +233,12 @@ object NameOps {
233233
case nme.clone_ => nme.clone_
234234
}
235235

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

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

241-
name.likeSpaced(name ++ nme.specializedTypeNames.prefix ++
241+
likeSpacedN(name ++ nme.specializedTypeNames.prefix ++
242242
methodTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.separator ++
243243
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
244244
}
@@ -249,11 +249,11 @@ object NameOps {
249249
def unmangleClassName: N = name.toTermName match {
250250
case name: SimpleName
251251
if name.endsWith(str.MODULE_SUFFIX) && !nme.falseModuleClassNames.contains(name) =>
252-
likeSpaced(name.dropRight(str.MODULE_SUFFIX.length).moduleClassName)
252+
likeSpacedN(name.dropRight(str.MODULE_SUFFIX.length).moduleClassName)
253253
case _ => name
254254
}
255255

256-
def unmangle(kind: NameKind): N = likeSpaced {
256+
def unmangle(kind: NameKind): N = likeSpacedN {
257257
name rewrite {
258258
case unmangled: SimpleName =>
259259
kind.unmangle(unmangled)
@@ -271,7 +271,7 @@ object NameOps {
271271
}
272272
}
273273

274-
implicit class TermNameDecorator(val name: TermName) extends AnyVal {
274+
implicit class TermNameDecorator(private val name: TermName) extends AnyVal {
275275
import nme._
276276

277277
def setterName: TermName = name.exclude(FieldName) ++ str.SETTER_SUFFIX

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,26 +1260,26 @@ object messages {
12601260
|""".stripMargin
12611261
}
12621262

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

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

0 commit comments

Comments
 (0)