Skip to content

Fix #831 object self #841

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 2 commits into from
Oct 22, 2015
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
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
}