Skip to content

Commit 62c11a7

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 82721bc commit 62c11a7

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
@@ -1227,7 +1227,7 @@ object Denotations {
12271227
}
12281228
recurSimple(path.length, wrap)
12291229
}
1230-
recur(path.unmangleClassName)
1230+
recur(path)
12311231
}
12321232

12331233
/** 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

@@ -1204,12 +1211,12 @@ object SymDenotations {
12041211
class ClassDenotation private[SymDenotations] (
12051212
symbol: Symbol,
12061213
ownerIfExists: Symbol,
1207-
name: Name,
1214+
initName: Name,
12081215
initFlags: FlagSet,
12091216
initInfo: Type,
12101217
initPrivateWithin: Symbol,
12111218
initRunId: RunId)
1212-
extends SymDenotation(symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
1219+
extends SymDenotation(symbol, ownerIfExists, initName, initFlags, initInfo, initPrivateWithin) {
12131220

12141221
import util.LRUCache
12151222

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
@@ -421,6 +421,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
421421
val tag = readByte()
422422
val end = readEnd()
423423
var name: Name = readName()
424+
val sname = name.toSimpleName
424425
if (tag == TYPEDEF || tag == TYPEPARAM) name = name.toTypeName
425426
skipParams()
426427
val ttag = nextUnsharedTag
@@ -437,9 +438,10 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
437438
def adjustIfModule(completer: LazyType) =
438439
if (flags is Module) ctx.adjustModuleCompleter(completer, name) else completer
439440
val sym =
440-
roots.find(root => (root.owner eq ctx.owner) && root.name == name) match {
441+
roots.find(root => (root.owner eq ctx.owner) && root.name.toSimpleName == sname && root.isType == name.isTypeName) match {
441442
case Some(rootd) =>
442443
pickling.println(i"overwriting ${rootd.symbol} # ${rootd.hashCode}")
444+
rootd.name = name
443445
rootd.info = adjustIfModule(
444446
new Completer(ctx.owner, subReader(start, end)) with SymbolLoaders.SecondCompleter)
445447
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
@@ -446,15 +446,18 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
446446
flags = flags &~ Scala2SuperAccessor
447447
}
448448

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

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

457459
def completeRoot(denot: ClassDenotation, completer: LazyType): Symbol = {
460+
denot.name = name
458461
denot.setFlag(flags)
459462
denot.resetFlag(Touched) // allow one more completion
460463
denot.info = completer

0 commit comments

Comments
 (0)