Skip to content

Commit a462df4

Browse files
committed
Fix #560 - refactor flatName
Merge with fullNameSeparated. Take into account that static nested classes are separated only with a single '$' from the enclosing object. Previous scheme took the module class as owner, which led to a double '$$'.
1 parent 4b82e83 commit a462df4

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{
554554
def fullName: String = sym.showFullName
555555
def simpleName: Name = sym.name
556556
def javaSimpleName: Name = toDenot(sym).name // addModuleSuffix(simpleName.dropLocal)
557-
def javaBinaryName: Name = toDenot(sym).fullNameSeparated('/') // addModuleSuffix(fullNameInternal('/'))
557+
def javaBinaryName: Name = toDenot(sym).fullNameSeparated("/") // addModuleSuffix(fullNameInternal('/'))
558558
def javaClassName: String = toDenot(sym).fullName.toString// addModuleSuffix(fullNameInternal('.')).toString
559559
def name: Name = sym.name
560560
def rawname: Name = sym.name // todo ????

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ object NameOps {
150150
/** The expanded name of `name` relative to given class `base`.
151151
*/
152152
def expandedName(base: Symbol)(implicit ctx: Context): N =
153-
expandedName(if (base is Flags.ExpandedName) base.name else base.fullNameSeparated('$'))
153+
expandedName(if (base is Flags.ExpandedName) base.name else base.fullNameSeparated("$"))
154154

155155
/** The expanded name of `name` relative to `basename` with given `separator`
156156
*/

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -289,39 +289,40 @@ object SymDenotations {
289289
}
290290

291291
/** The encoded full path name of this denotation, where outer names and inner names
292-
* are separated by `separator` characters.
292+
* are separated by `separator` strings.
293293
* Never translates expansions of operators back to operator symbol.
294-
* Drops package objects. Represents terms in the owner chain by a simple `separator`.
294+
* Drops package objects. Represents terms in the owner chain by a simple `~`.
295+
* A separator "" means "flat name"; the real separator in this case is "$" and
296+
* enclosing packages do not form part of the name.
295297
*/
296-
def fullNameSeparated(separator: Char)(implicit ctx: Context): Name =
297-
if (symbol == NoSymbol || owner == NoSymbol || owner.isEffectiveRoot) name
298+
def fullNameSeparated(separator: String)(implicit ctx: Context): Name = {
299+
var sep = separator
300+
var stopAtPackage = false
301+
if (sep.isEmpty) {
302+
sep = "$"
303+
stopAtPackage = true
304+
}
305+
if (symbol == NoSymbol ||
306+
owner == NoSymbol ||
307+
owner.isEffectiveRoot ||
308+
stopAtPackage && owner.is(PackageClass)) name
298309
else {
299-
var owner = this
300-
var sep = ""
301-
do {
302-
owner = owner.owner
303-
sep += separator
304-
} while (!owner.isClass && !owner.isPackageObject)
305-
val fn = owner.fullNameSeparated(separator) ++ sep ++ name
310+
var encl = owner
311+
while (!encl.isClass && !encl.isPackageObject) {
312+
encl = encl.owner
313+
sep += "~"
314+
}
315+
if (owner.is(ModuleClass) && sep == "$") sep = "" // duplicate scalac's behavior: don't write a double '$$' for module class members.
316+
val fn = encl.fullNameSeparated(separator) ++ sep ++ name
306317
if (isType) fn.toTypeName else fn.toTermName
307318
}
319+
}
308320

309321
/** The encoded flat name of this denotation, where joined names are separated by `separator` characters. */
310-
def flatName(separator: Char = '$')(implicit ctx: Context): Name =
311-
if (symbol == NoSymbol || owner == NoSymbol || owner.isEffectiveRoot || (owner is PackageClass)) name
312-
else {
313-
var owner = this
314-
var sep = ""
315-
do {
316-
owner = owner.owner
317-
sep += separator
318-
} while (!owner.isClass && !owner.isPackageObject)
319-
val fn = owner.flatName(separator) ++ sep ++ name
320-
if (isType) fn.toTypeName else fn.toTermName
321-
}
322+
def flatName(implicit ctx: Context): Name = fullNameSeparated("")
322323

323324
/** `fullName` where `.' is the separator character */
324-
def fullName(implicit ctx: Context): Name = fullNameSeparated('.')
325+
def fullName(implicit ctx: Context): Name = fullNameSeparated(".")
325326

326327
// ----- Tests -------------------------------------------------
327328

@@ -1367,7 +1368,7 @@ object SymDenotations {
13671368

13681369
/** Enter a symbol in given `scope` without potentially replacing the old copy. */
13691370
def enterNoReplace(sym: Symbol, scope: MutableScope)(implicit ctx: Context): Unit = {
1370-
require((sym.denot.flagsUNSAFE is Private) || !(this is Frozen))
1371+
require((sym.denot.flagsUNSAFE is Private) || !(this is Frozen) || (unforcedDecls ne scope))
13711372
scope.enter(sym)
13721373

13731374
if (myMemberFingerPrint != FingerPrint.unknown)
@@ -1572,8 +1573,8 @@ object SymDenotations {
15721573
}
15731574
}
15741575

1575-
private[this] var fullNameCache: SimpleMap[Character, Name] = SimpleMap.Empty
1576-
override final def fullNameSeparated(separator: Char)(implicit ctx: Context): Name = {
1576+
private[this] var fullNameCache: SimpleMap[String, Name] = SimpleMap.Empty
1577+
override final def fullNameSeparated(separator: String)(implicit ctx: Context): Name = {
15771578
val cached = fullNameCache(separator)
15781579
if (cached != null) cached
15791580
else {

src/dotty/tools/dotc/transform/Flatten.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import DenotTransformers.SymTransformer
66
import Phases.Phase
77
import Contexts.Context
88
import Flags._
9-
import SymUtils._
109
import SymDenotations.SymDenotation
1110
import collection.mutable
1211
import TreeTransforms.MiniPhaseTransform
@@ -19,7 +18,7 @@ class Flatten extends MiniPhaseTransform with SymTransformer { thisTransform =>
1918
def transformSym(ref: SymDenotation)(implicit ctx: Context) = {
2019
if (ref.isClass && !ref.is(Package) && !ref.owner.is(Package)) {
2120
ref.copySymDenotation(
22-
name = ref.flatName(),
21+
name = ref.flatName,
2322
owner = ref.enclosingPackageClass)
2423
}
2524
else ref

src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ class SymUtils(val self: Symbol) extends AnyVal {
8585
def field(implicit ctx: Context): Symbol =
8686
self.owner.info.decl(self.asTerm.name.fieldName).suchThat(!_.is(Method)).symbol
8787

88-
/** `fullName` where `$' is the separator character */
89-
def flatName(implicit ctx: Context): Name = self.flatName('$')
90-
9188
def initializer(implicit ctx: Context): TermSymbol =
9289
self.owner.info.decl(InitializerName(self.asTerm.name)).symbol.asTerm
9390

0 commit comments

Comments
 (0)