Skip to content

Commit

Permalink
Merge pull request scala#841 from dotty-staging/fix-#831-object-self
Browse files Browse the repository at this point in the history
Fix scala#831 object self
  • Loading branch information
odersky committed Oct 22, 2015
2 parents e92668c + b482db8 commit b927f66
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,11 @@ object SymDenotations {

/** The module implemented by this module class, NoSymbol if not applicable. */
final def sourceModule(implicit ctx: Context): Symbol = myInfo match {
case ClassInfo(_, _, _, _, selfType: TermRef) if this is ModuleClass =>
selfType.symbol
case ClassInfo(_, _, _, _, selfType) if this is ModuleClass =>
selfType match {
case selfType: TermRef => selfType.symbol
case selfType: Symbol => selfType.info.asInstanceOf[TermRef].symbol
}
case info: LazyType =>
info.sourceModule
case _ =>
Expand Down
41 changes: 24 additions & 17 deletions src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ class Namer { typer: Typer =>
}
}

/** Record `sym` as the symbol defined by `tree` */
def recordSym(sym: Symbol, tree: Tree)(implicit ctx: Context): Symbol = {
val refs = tree.attachmentOrElse(References, Nil)
if (refs.nonEmpty) {
tree.removeAttachment(References)
refs foreach (_.pushAttachment(OriginalSymbol, sym))
}
tree.pushAttachment(SymOfTree, sym)
sym
}

/** If this tree is a member def or an import, create a symbol of it
* and store in symOfTree map.
*/
Expand All @@ -224,16 +235,6 @@ class Namer { typer: Typer =>
def privateWithinClass(mods: Modifiers) =
enclosingClassNamed(mods.privateWithin, mods.pos)

def record(sym: Symbol): Symbol = {
val refs = tree.attachmentOrElse(References, Nil)
if (refs.nonEmpty) {
tree.removeAttachment(References)
refs foreach (_.pushAttachment(OriginalSymbol, sym))
}
tree.pushAttachment(SymOfTree, sym)
sym
}

def checkFlags(flags: FlagSet) =
if (flags.isEmpty) flags
else {
Expand Down Expand Up @@ -274,10 +275,10 @@ class Namer { typer: Typer =>
case tree: TypeDef if tree.isClassDef =>
val name = checkNoConflict(tree.name.encode).asTypeName
val flags = checkFlags(tree.mods.flags &~ Implicit)
val cls = record(ctx.newClassSymbol(
val cls = recordSym(ctx.newClassSymbol(
ctx.owner, name, flags | inSuperCall,
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
privateWithinClass(tree.mods), tree.pos, ctx.source.file))
privateWithinClass(tree.mods), tree.pos, ctx.source.file), tree)
cls.completer.asInstanceOf[ClassCompleter].init()
cls
case tree: MemberDef =>
Expand All @@ -304,13 +305,13 @@ class Namer { typer: Typer =>
// have no implementation.
val cctx = if (tree.name == nme.CONSTRUCTOR && !(tree.mods is JavaDefined)) ctx.outer else ctx

record(ctx.newSymbol(
recordSym(ctx.newSymbol(
ctx.owner, name, flags | deferred | method | higherKinded | inSuperCall1,
adjustIfModule(new Completer(tree)(cctx), tree),
privateWithinClass(tree.mods), tree.pos))
privateWithinClass(tree.mods), tree.pos), tree)
case tree: Import =>
record(ctx.newSymbol(
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos))
recordSym(ctx.newSymbol(
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos), tree)
case _ =>
NoSymbol
}
Expand Down Expand Up @@ -579,7 +580,13 @@ class Namer { typer: Typer =>

val selfInfo =
if (self.isEmpty) NoType
else if (cls is Module) cls.owner.thisType select sourceModule
else if (cls.is(Module)) {
val moduleType = cls.owner.thisType select sourceModule
if (self.name == nme.WILDCARD) moduleType
else recordSym(
ctx.newSymbol(cls, self.name, self.mods.flags, moduleType, coord = self.pos),
self)
}
else createSymbol(self)

// pre-set info, so that parent types can refer to type params
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i831.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Test { self =>
def a = 5
self.a
}

0 comments on commit b927f66

Please sign in to comment.