summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2016-11-29 21:55:14 +1000
committerJason Zaugg <jzaugg@gmail.com>2016-11-29 21:55:14 +1000
commit9502a061fa007ceb1d4e550fdb386a3645c67b1c (patch)
treed4cff2c254b1eb93978a32154de465fcea89658a
parent37037fe70651eee7a1e8a77a2f8b6e74968836b6 (diff)
downloadscala-9502a061fa007ceb1d4e550fdb386a3645c67b1c.tar.gz
scala-9502a061fa007ceb1d4e550fdb386a3645c67b1c.tar.bz2
scala-9502a061fa007ceb1d4e550fdb386a3645c67b1c.zip
Refactor companion lookup methods after code review
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Contexts.scala2
-rw-r--r--src/reflect/scala/reflect/internal/Scopes.scala11
2 files changed, 6 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
index cbe9e522a2..7988ac9f16 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
@@ -1196,7 +1196,7 @@ trait Contexts { self: Analyzer =>
final def lookupCompanionOf(original: Symbol): Symbol = {
lookupScopeEntry(original) match {
case null => NoSymbol
- case entry => entry.owner.lookupCompanion(original).filter(_.isCoDefinedWith(original))
+ case entry => entry.owner.lookupCompanion(original)
}
}
diff --git a/src/reflect/scala/reflect/internal/Scopes.scala b/src/reflect/scala/reflect/internal/Scopes.scala
index 0df8358d7d..32ce02bdd6 100644
--- a/src/reflect/scala/reflect/internal/Scopes.scala
+++ b/src/reflect/scala/reflect/internal/Scopes.scala
@@ -294,19 +294,18 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
final def lookupCompanion(original: Symbol): Symbol = {
lookupSymbolEntry(original) match {
case null =>
- case entry if entry.sym == original =>
+ 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 comaniopn of the object.
- // 2) Must be in exactly the same scope, so that `{ class C; def C }` are not companions
- // TODO Exclude type aliases as companions of terms?
- if ((e.owner eq entry.owner) && (original.isTerm || e.sym.hasModuleFlag)) {
- return e.sym
+ // 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)
}
- case _ =>
}
NoSymbol
}