diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt index ef07400ce2e..4e65a836a01 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt @@ -287,23 +287,21 @@ inline fun LexicalScope.findFirstFromImportingScopes(fetch: (ImportingS return findFirstFromMeAndParent { if (it is ImportingScope) fetch(it) else null } } -//TODO: more efficient -fun LexicalScope.addImportScopes(importScopes: Collection): LexicalScope { - return importScopes.fold(this) { scope, importingScope -> scope.addImportScope(importingScope) } -} - -fun LexicalScope.addImportScope(importScope: ImportingScope): LexicalScope { - assert(importScope.parent == null) +fun LexicalScope.addImportScopes(importScopes: List): LexicalScope { if (this is ImportingScope) { - return importScope.withParent(this) + return chainImportingScopes(importScopes, this)!! } else { val lastNonImporting = parentsWithSelf.last { it !is ImportingScope } val firstImporting = lastNonImporting.parent as ImportingScope? - return LexicalScopeWrapper(this, importScope.withParent(firstImporting)) + val newFirstImporting = chainImportingScopes(importScopes, firstImporting) + return LexicalScopeWrapper(this, newFirstImporting) } } +fun LexicalScope.addImportScope(importScope: ImportingScope): LexicalScope + = addImportScopes(listOf(importScope)) + fun ImportingScope.withParent(newParent: ImportingScope?): ImportingScope { // TODO: it's a hack for Repl if (this is MemberScopeToImportingScopeAdapter) { @@ -337,9 +335,9 @@ private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingSc } } -fun chainImportingScopes(scopes: List): ImportingScope? { +fun chainImportingScopes(scopes: List, tail: ImportingScope? = null): ImportingScope? { return scopes.asReversed() - .fold(null) { current, scope -> + .fold(tail) { current, scope -> assert(scope.parent == null) scope.withParent(current) }