summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2017-02-21 10:23:00 +0100
committerGitHub <noreply@github.com>2017-02-21 10:23:00 +0100
commitdabec1a262fafd26bf6976ac3f0b81445ddb5f29 (patch)
tree7e9b0c965db1b41bb74977d83f1e13a6a9033e70 /src/compiler/scala/tools/nsc/typechecker/Contexts.scala
parent2f1e0c22ecd3b6e3886bd9bc94f27725e08324b8 (diff)
parent06eee798f581ffa21dc4a69974315a7c2bc7abe1 (diff)
downloadscala-dabec1a262fafd26bf6976ac3f0b81445ddb5f29.tar.gz
scala-dabec1a262fafd26bf6976ac3f0b81445ddb5f29.tar.bz2
scala-dabec1a262fafd26bf6976ac3f0b81445ddb5f29.zip
Merge pull request #5700 from retronym/ticket/10154-refactor
Refactor lookupCompanion
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/Contexts.scala')
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Contexts.scala40
1 files changed, 23 insertions, 17 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
index d142cdb84c..503f64a44f 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala
@@ -1196,27 +1196,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 */