Skip to content

Commit 59824d9

Browse files
committed
Fix and activate package scopes
Prevviously they were actually unused.
1 parent 6b575ab commit 59824d9

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ class Definitions {
934934
private def makeScalaSpecial()(implicit ctx: Context) = {
935935
val oldInfo = ScalaPackageClass.classInfo
936936
val oldDecls = oldInfo.decls
937-
val newDecls = new MutableScope(oldDecls) {
937+
val newDecls = new PackageScope(oldDecls) {
938938
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
939939
val res = super.lookupEntry(name)
940940
if (res == null && name.isTypeName && name.isSyntheticFunction)

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ object Scopes {
144144
final def toText(printer: Printer): Text = printer.toText(this)
145145

146146
def checkConsistent()(implicit ctx: Context) = ()
147+
148+
/** Hook for transforming a name before it is used in a lookup or creation.
149+
* Used to mangle names in package scopes.
150+
*/
151+
protected def normalize(name: Name): Name = name
147152
}
148153

149154
/** A subclass of Scope that defines methods for entering and
@@ -155,6 +160,7 @@ object Scopes {
155160
class MutableScope protected[Scopes](initElems: ScopeEntry, initSize: Int, val nestingLevel: Int = 0)
156161
extends Scope {
157162

163+
/** Scope shares elements with `base` */
158164
protected[Scopes] def this(base: Scope)(implicit ctx: Context) = {
159165
this(base.lastEntry, base.size, base.nestingLevel + 1)
160166
ensureCapacity(MinHash)(ctx) // WTH? it seems the implicit is not in scope for a secondary constructor call.
@@ -178,6 +184,8 @@ object Scopes {
178184
*/
179185
private var elemsCache: List[Symbol] = null
180186

187+
protected def newScopeLikeThis() = new MutableScope()
188+
181189
/** Clone scope, taking care not to force the denotations of any symbols in the scope.
182190
*/
183191
def cloneScope(implicit ctx: Context): MutableScope = {
@@ -187,7 +195,7 @@ object Scopes {
187195
entries += e
188196
e = e.prev
189197
}
190-
val scope = newScope
198+
val scope = newScopeLikeThis()
191199
for (i <- entries.length - 1 to 0 by -1) {
192200
val e = entries(i)
193201
scope.newScopeEntry(e.name, e.sym)
@@ -198,7 +206,7 @@ object Scopes {
198206
/** create and enter a scope entry with given name and symbol */
199207
protected def newScopeEntry(name: Name, sym: Symbol)(implicit ctx: Context): ScopeEntry = {
200208
ensureCapacity(if (hashTable ne null) hashTable.length else MinHash)
201-
val e = new ScopeEntry(name, sym, this)
209+
val e = new ScopeEntry(normalize(name), sym, this)
202210
e.prev = lastEntry
203211
lastEntry = e
204212
if (hashTable ne null) enterInHash(e)
@@ -234,7 +242,7 @@ object Scopes {
234242
enter(sym)
235243
}
236244

237-
private def ensureCapacity(tableSize: Int)(implicit ctx: Context): Unit =
245+
protected def ensureCapacity(tableSize: Int)(implicit ctx: Context): Unit =
238246
if (size >= tableSize * FillFactor) createHash(tableSize * 2)
239247

240248
private def createHash(tableSize: Int)(implicit ctx: Context): Unit =
@@ -310,15 +318,16 @@ object Scopes {
310318
/** Lookup a symbol entry matching given name.
311319
*/
312320
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
321+
val normalized = normalize(name)
313322
var e: ScopeEntry = null
314323
if (hashTable ne null) {
315-
e = hashTable(name.hashCode & (hashTable.length - 1))
316-
while ((e ne null) && e.name != name) {
324+
e = hashTable(normalized.hashCode & (hashTable.length - 1))
325+
while ((e ne null) && e.name != normalized) {
317326
e = e.tail
318327
}
319328
} else {
320329
e = lastEntry
321-
while ((e ne null) && e.name != name) {
330+
while ((e ne null) && e.name != normalized) {
322331
e = e.prev
323332
}
324333
}
@@ -394,12 +403,23 @@ object Scopes {
394403
}
395404
}
396405

397-
class PackageScope extends MutableScope {
398-
override final def newScopeEntry(name: Name, sym: Symbol)(implicit ctx: Context): ScopeEntry =
399-
super.newScopeEntry(name.mangled, sym)
406+
/** The scope of a package. This is different from a normal scope
407+
* in that names of scope entries are kept in mangled form.
408+
*/
409+
class PackageScope protected[Scopes](initElems: ScopeEntry, initSize: Int, nestingLevel: Int)
410+
extends MutableScope(initElems, initSize, nestingLevel) {
411+
412+
/** Scope shares elements with `base` */
413+
def this(base: Scope)(implicit ctx: Context) = {
414+
this(base.lastEntry, base.size, base.nestingLevel + 1)
415+
ensureCapacity(MinHash)(ctx) // WTH? it seems the implicit is not in scope for a secondary constructor call.
416+
}
417+
418+
def this() = this(null, 0, 0)
419+
420+
override def newScopeLikeThis() = new PackageScope()
400421

401-
override final def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry =
402-
super.lookupEntry(name.mangled)
422+
override protected def normalize(name: Name) = name.mangled
403423
}
404424

405425
/** Create a new scope */

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ object StdNames {
453453
val ne: N = "ne"
454454
val newFreeTerm: N = "newFreeTerm"
455455
val newFreeType: N = "newFreeType"
456-
val newNestedSymbol: N = "newNestedSymbol"
457456
val newScopeWith: N = "newScopeWith"
458457
val next: N = "next"
459458
val nmeNewTermName: N = "newTermName"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class SymbolLoaders {
148148
override def sourceModule(implicit ctx: Context) = _sourceModule
149149
def description = "package loader " + classpath.name
150150

151-
private[core] val currentDecls: MutableScope = newScope
151+
private[core] val currentDecls: MutableScope = new PackageScope()
152152

153153
def doComplete(root: SymDenotation)(implicit ctx: Context): Unit = {
154154
assert(root is PackageClass, root)

0 commit comments

Comments
 (0)