Skip to content

Commit bf74728

Browse files
committed
Keep package member names mangled
Once we start using unencoded operators internally, we will face the problem that one cannot decode realiably a class file filename. We therefore turn things around, keeping members of package scopes in mangled and encoded form. This is compensated by (1) mangling names for lookup of such members and (2) when unpickling from Scala 2 info or Tasty, comparing mangled names when matching a read class or module object against a root.
1 parent d56ce10 commit bf74728

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ object Denotations {
12071207
}
12081208
recurSimple(path.length, wrap)
12091209
}
1210-
recur(path.unmangleClassName)
1210+
recur(path)
12111211
}
12121212

12131213
/** If we are looking for a non-existing term name in a package,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ object Scopes {
394394
}
395395
}
396396

397+
class PackageScope extends MutableScope {
398+
override final def newScopeEntry(name: Name, sym: Symbol)(implicit ctx: Context): ScopeEntry =
399+
super.newScopeEntry(name.toSimpleName, sym)
400+
401+
override final def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry =
402+
super.lookupEntry(name.toSimpleName)
403+
}
404+
397405
/** Create a new scope */
398406
def newScope: MutableScope = new MutableScope()
399407

@@ -408,7 +416,7 @@ object Scopes {
408416
}
409417

410418
/** Create new scope for the members of package `pkg` */
411-
def newPackageScope(pkgClass: Symbol): MutableScope = newScope
419+
def newPackageScope(pkgClass: Symbol): MutableScope = new PackageScope()
412420

413421
/** Transform scope of members of `owner` using operation `op`
414422
* This is overridden by the reflective compiler to avoid creating new scopes for packages

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ object SymDenotations {
107107
class SymDenotation private[SymDenotations] (
108108
symbol: Symbol,
109109
ownerIfExists: Symbol,
110-
final val name: Name,
110+
initName: Name,
111111
initFlags: FlagSet,
112112
initInfo: Type,
113113
initPrivateWithin: Symbol = NoSymbol) extends SingleDenotation(symbol) {
@@ -125,11 +125,18 @@ object SymDenotations {
125125

126126
// ------ Getting and setting fields -----------------------------
127127

128+
private[this] var myName = initName
128129
private[this] var myFlags: FlagSet = adaptFlags(initFlags)
129130
private[this] var myInfo: Type = initInfo
130131
private[this] var myPrivateWithin: Symbol = initPrivateWithin
131132
private[this] var myAnnotations: List[Annotation] = Nil
132133

134+
/** The name of the symbol */
135+
def name = myName
136+
137+
/** Update the name; only called when unpickling top-level classes */
138+
def name_=(n: Name) = myName = n
139+
133140
/** The owner of the symbol; overridden in NoDenotation */
134141
def owner: Symbol = ownerIfExists
135142

@@ -1208,12 +1215,12 @@ object SymDenotations {
12081215
class ClassDenotation private[SymDenotations] (
12091216
symbol: Symbol,
12101217
ownerIfExists: Symbol,
1211-
name: Name,
1218+
initName: Name,
12121219
initFlags: FlagSet,
12131220
initInfo: Type,
12141221
initPrivateWithin: Symbol,
12151222
initRunId: RunId)
1216-
extends SymDenotation(symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
1223+
extends SymDenotation(symbol, ownerIfExists, initName, initFlags, initInfo, initPrivateWithin) {
12171224

12181225
import util.LRUCache
12191226

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SymbolLoaders {
3939
def enterClass(
4040
owner: Symbol, name: PreName, completer: SymbolLoader,
4141
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(implicit ctx: Context): Symbol = {
42-
val cls = ctx.newClassSymbol(owner, name.toTypeName.unmangleClassName, flags, completer, assocFile = completer.sourceFileOrNull)
42+
val cls = ctx.newClassSymbol(owner, name.toTypeName, flags, completer, assocFile = completer.sourceFileOrNull)
4343
enterNew(owner, cls, completer, scope)
4444
}
4545

@@ -163,7 +163,7 @@ class SymbolLoaders {
163163
initializeFromClassPath(root.symbol, classRep)
164164
for (classRep <- classpath.classes)
165165
if (maybeModuleClass(classRep) &&
166-
!root.unforcedDecls.lookup(classRep.name.toTypeName.unmangleClassName).exists)
166+
!root.unforcedDecls.lookup(classRep.name.toTypeName).exists)
167167
initializeFromClassPath(root.symbol, classRep)
168168
}
169169
if (!root.isEmptyPackage)

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
416416
val tag = readByte()
417417
val end = readEnd()
418418
var name: Name = readName()
419+
val sname = name.toSimpleName
419420
if (tag == TYPEDEF || tag == TYPEPARAM) name = name.toTypeName
420421
skipParams()
421422
val ttag = nextUnsharedTag
@@ -432,9 +433,10 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
432433
def adjustIfModule(completer: LazyType) =
433434
if (flags is Module) ctx.adjustModuleCompleter(completer, name) else completer
434435
val sym =
435-
roots.find(root => (root.owner eq ctx.owner) && root.name == name) match {
436+
roots.find(root => (root.owner eq ctx.owner) && root.name.toSimpleName == sname && root.isType == name.isTypeName) match {
436437
case Some(rootd) =>
437438
pickling.println(i"overwriting ${rootd.symbol} # ${rootd.hashCode}")
439+
rootd.name = name
438440
rootd.info = adjustIfModule(
439441
new Completer(ctx.owner, subReader(start, end)) with SymbolLoaders.SecondCompleter)
440442
rootd.flags = flags &~ Touched // allow one more completion

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,18 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
448448
flags = flags &~ Scala2SuperAccessor
449449
}
450450

451-
def isClassRoot = (name == classRoot.name) && (owner == classRoot.owner) && !(flags is ModuleClass)
452-
def isModuleClassRoot = (name == moduleClassRoot.name) && (owner == moduleClassRoot.owner) && (flags is Module)
453-
def isModuleRoot = (name == moduleClassRoot.name.sourceModuleName) && (owner == moduleClassRoot.owner) && (flags is Module)
451+
val sname = name.toSimpleName
452+
def nameMatches(rootName: Name) = sname == rootName.toSimpleName
453+
def isClassRoot = nameMatches(classRoot.name) && (owner == classRoot.owner) && !(flags is ModuleClass)
454+
def isModuleClassRoot = nameMatches(moduleClassRoot.name) && (owner == moduleClassRoot.owner) && (flags is Module)
455+
def isModuleRoot = nameMatches(moduleClassRoot.name.sourceModuleName) && (owner == moduleClassRoot.owner) && (flags is Module)
454456

455457
//if (isClassRoot) println(s"classRoot of $classRoot found at $readIndex, flags = $flags") // !!! DEBUG
456458
//if (isModuleRoot) println(s"moduleRoot of $moduleRoot found at $readIndex, flags = $flags") // !!! DEBUG
457459
//if (isModuleClassRoot) println(s"moduleClassRoot of $moduleClassRoot found at $readIndex, flags = $flags") // !!! DEBUG
458460

459461
def completeRoot(denot: ClassDenotation, completer: LazyType): Symbol = {
462+
denot.name = name
460463
denot.setFlag(flags)
461464
denot.resetFlag(Touched) // allow one more completion
462465
denot.info = completer

0 commit comments

Comments
 (0)