Skip to content

Commit 09b1a31

Browse files
committed
Fixed stackoverflow problem when initializing l...
Fixed stackoverflow problem when initializing large scopes.
1 parent 17c0462 commit 09b1a31

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/compiler/scala/reflect/internal/Scopes.scala

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,18 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
9494
*
9595
* @param e ...
9696
*/
97-
def enter(e: ScopeEntry) {
97+
protected def enter(e: ScopeEntry) {
9898
elemsCache = null
99-
if (hashtable ne null) {
100-
val i = e.sym.name.start & HASHMASK
101-
elems.tail = hashtable(i)
102-
hashtable(i) = elems
103-
} else if (size >= MIN_HASH) {
99+
if (hashtable ne null)
100+
enterInHash(e)
101+
else if (size >= MIN_HASH)
104102
createHash()
105-
}
103+
}
104+
105+
private def enterInHash(e: ScopeEntry): Unit = {
106+
val i = e.sym.name.start & HASHMASK
107+
e.tail = hashtable(i)
108+
hashtable(i) = e
106109
}
107110

108111
/** enter a symbol
@@ -122,15 +125,23 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
122125

123126
private def createHash() {
124127
hashtable = new Array[ScopeEntry](HASHSIZE)
125-
enterInHash(elems)
128+
enterAllInHash(elems)
126129
}
127130

128-
private def enterInHash(e: ScopeEntry) {
131+
private def enterAllInHash(e: ScopeEntry, n: Int = 0) {
129132
if (e ne null) {
130-
enterInHash(e.next)
131-
val i = e.sym.name.start & HASHMASK
132-
e.tail = hashtable(i)
133-
hashtable(i) = e
133+
if (n < maxRecursions) {
134+
enterAllInHash(e.next, n + 1)
135+
enterInHash(e)
136+
} else {
137+
var entries: List[ScopeEntry] = List()
138+
var ee = e
139+
while (ee ne null) {
140+
entries = ee :: entries
141+
ee = ee.next
142+
}
143+
entries foreach enterInHash
144+
}
134145
}
135146
}
136147

@@ -334,5 +345,8 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
334345
/** The error scope.
335346
*/
336347
class ErrorScope(owner: Symbol) extends Scope(null: ScopeEntry)
348+
349+
private final val maxRecursions = 1000
350+
337351
}
338352

0 commit comments

Comments
 (0)