8000 Refactor lookupCompanion by retronym · Pull Request #5700 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

Refactor lookupCompanion #5700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1193,27 +1193,33 @@ trait Contexts { self: Analyzer =>
res
}

final def lookupCompanionOf(original: Symbol): Symbol = {
if (original.isModuleClass) original.sourceModule
else lookupScopeEntry(original) match {
case null => NoSymbol
case entry => entry.owner.lookupCompanion(original)
final def lookupCompanionInIncompleteOwner(original: Symbol): Symbol = {
/* Search scopes in current and enclosing contexts for the definition of `symbol` */
def lookupScopeEntry(symbol: Symbol): ScopeEntry = {
var res: ScopeEntry = null
var ctx = this
while (res == null && ctx.outer != ctx) {
val s = ctx.scope lookupSymbolEntry symbol
if (s != null)
res = s
else
ctx = ctx.outer
}
res
}
}

/** Search scopes in current and enclosing contexts for the definition of `symbol` */
private def lookupScopeEntry(symbol: Symbol): ScopeEntry = {
var res: ScopeEntry = null
var ctx = this
while (res == null && ctx.outer != ctx) {
val s = ctx.scope lookupSymbolEntry symbol
if (s != null)
res = s
else
ctx = ctx.outer
// 1) Must be owned by the same Scope, to ensure that in
// `{ class C; { ...; object C } }`, the class is not seen as a companion of the object.
// 2) Must be a class and module symbol, so that `{ class C; def C }` or `{ type T; object T }` are not companions.
lookupScopeEntry(original) match {
case null => NoSymbol
case entry =>
def isCompanion(sym: Symbol): Boolean =
(original.isModule && sym.isClass || sym.isModule && original.isClass) && sym.isCoDefinedWith(original)
entry.owner.lookupNameInSameScopeAs(original, original.name.companionName).filter(isCompanion)
}
res
}

} //class Context

/** A `Context` focussed on an `Import` tree */
Expand Down
9 changes: 6 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1955,9 +1955,12 @@ trait Namers extends MethodSynthesis {
// Doing this generally would trigger cycles; that's what we also
// use the lower-level scan through the current Context as a fall back.
if (!currentRun.compiles(owner)) owner.initialize
original.companionSymbol orElse {
ctx.lookupCompanionOf(original)
}

if (original.isModuleClass) original.sourceModule
else if (!owner.isTerm && owner.hasCompleteInfo)
original.companionSymbol
else
ctx.lookupCompanionInIncompleteOwner(original)
}

/** A version of `Symbol#linkedClassOfClass` that works with local companions, ala `companionSymbolOf`. */
Expand Down
33 changes: 14 additions & 19 deletions src/reflect/scala/reflect/internal/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,25 +291,6 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
null
}

final def lookupCompanion(original: Symbol): Symbol = {
lookupSymbolEntry(original) match {
case null =>
case entry =>
var e = lookupEntry(original.name.companionName)
while (e != null) {
// 1) Must be owned by the same Scope, to ensure that in
// `{ class C; { ...; object C } }`, the class is not seen as a companion of the object.
// 2) Must be a class and module symbol, so that `{ class C; def C }` or `{ type T; object T }` are not companions.
def isClassAndModule(sym1: Symbol, sym2: Symbol) = sym1.isClass && sym2.isModule
if ((e.owner eq entry.owner) && (isClassAndModule(original, e.sym) || isClassAndModule(e.sym, original))) {
return if (e.sym.isCoDefinedWith(original)) e.sym else NoSymbol
}
e = lookupNextEntry(e)
}
}
NoSymbol
}

/** lookup a symbol entry matching given name.
* @note from Martin: I believe this is a hotspot or will be one
* in future versions of the type system. I have reverted the previous
Expand Down Expand Up @@ -345,6 +326,20 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
e
}

final def lookupNameInSameScopeAs(original: Symbol, companionName: Name): Symbol = {
lookupSymbolEntry(original) match {
case null =>
case entry =>
var e = lookupEntry(companionName)
while (e != null) {
if (e.owner eq entry.owner) return e.sym
e = lookupNextEntry(e)
}
}
NoSymbol
}


/** TODO - we can test this more efficiently than checking isSubScope
* in both directions. However the size test might be enough to quickly
* rule out most failures.
Expand Down
0