Skip to content

Commit b927f66

Browse files
committed
Merge pull request #841 from dotty-staging/fix-#831-object-self
Fix #831 object self
2 parents e92668c + b482db8 commit b927f66

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/dotty/tools/dotc/core/SymDenotations.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,11 @@ object SymDenotations {
736736

737737
/** The module implemented by this module class, NoSymbol if not applicable. */
738738
final def sourceModule(implicit ctx: Context): Symbol = myInfo match {
739-
case ClassInfo(_, _, _, _, selfType: TermRef) if this is ModuleClass =>
740-
selfType.symbol
739+
case ClassInfo(_, _, _, _, selfType) if this is ModuleClass =>
740+
selfType match {
741+
case selfType: TermRef => selfType.symbol
742+
case selfType: Symbol => selfType.info.asInstanceOf[TermRef].symbol
743+
}
741744
case info: LazyType =>
742745
info.sourceModule
743746
case _ =>

src/dotty/tools/dotc/typer/Namer.scala

+24-17
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,17 @@ class Namer { typer: Typer =>
216216
}
217217
}
218218

219+
/** Record `sym` as the symbol defined by `tree` */
220+
def recordSym(sym: Symbol, tree: Tree)(implicit ctx: Context): Symbol = {
221+
val refs = tree.attachmentOrElse(References, Nil)
222+
if (refs.nonEmpty) {
223+
tree.removeAttachment(References)
224+
refs foreach (_.pushAttachment(OriginalSymbol, sym))
225+
}
226+
tree.pushAttachment(SymOfTree, sym)
227+
sym
228+
}
229+
219230
/** If this tree is a member def or an import, create a symbol of it
220231
* and store in symOfTree map.
221232
*/
@@ -224,16 +235,6 @@ class Namer { typer: Typer =>
224235
def privateWithinClass(mods: Modifiers) =
225236
enclosingClassNamed(mods.privateWithin, mods.pos)
226237

227-
def record(sym: Symbol): Symbol = {
228-
val refs = tree.attachmentOrElse(References, Nil)
229-
if (refs.nonEmpty) {
230-
tree.removeAttachment(References)
231-
refs foreach (_.pushAttachment(OriginalSymbol, sym))
232-
}
233-
tree.pushAttachment(SymOfTree, sym)
234-
sym
235-
}
236-
237238
def checkFlags(flags: FlagSet) =
238239
if (flags.isEmpty) flags
239240
else {
@@ -274,10 +275,10 @@ class Namer { typer: Typer =>
274275
case tree: TypeDef if tree.isClassDef =>
275276
val name = checkNoConflict(tree.name.encode).asTypeName
276277
val flags = checkFlags(tree.mods.flags &~ Implicit)
277-
val cls = record(ctx.newClassSymbol(
278+
val cls = recordSym(ctx.newClassSymbol(
278279
ctx.owner, name, flags | inSuperCall,
279280
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
280-
privateWithinClass(tree.mods), tree.pos, ctx.source.file))
281+
privateWithinClass(tree.mods), tree.pos, ctx.source.file), tree)
281282
cls.completer.asInstanceOf[ClassCompleter].init()
282283
cls
283284
case tree: MemberDef =>
@@ -304,13 +305,13 @@ class Namer { typer: Typer =>
304305
// have no implementation.
305306
val cctx = if (tree.name == nme.CONSTRUCTOR && !(tree.mods is JavaDefined)) ctx.outer else ctx
306307

307-
record(ctx.newSymbol(
308+
recordSym(ctx.newSymbol(
308309
ctx.owner, name, flags | deferred | method | higherKinded | inSuperCall1,
309310
adjustIfModule(new Completer(tree)(cctx), tree),
310-
privateWithinClass(tree.mods), tree.pos))
311+
privateWithinClass(tree.mods), tree.pos), tree)
311312
case tree: Import =>
312-
record(ctx.newSymbol(
313-
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos))
313+
recordSym(ctx.newSymbol(
314+
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos), tree)
314315
case _ =>
315316
NoSymbol
316317
}
@@ -579,7 +580,13 @@ class Namer { typer: Typer =>
579580

580581
val selfInfo =
581582
if (self.isEmpty) NoType
582-
else if (cls is Module) cls.owner.thisType select sourceModule
583+
else if (cls.is(Module)) {
584+
val moduleType = cls.owner.thisType select sourceModule
585+
if (self.name == nme.WILDCARD) moduleType
586+
else recordSym(
587+
ctx.newSymbol(cls, self.name, self.mods.flags, moduleType, coord = self.pos),
588+
self)
589+
}
583590
else createSymbol(self)
584591

585592
// pre-set info, so that parent types can refer to type params

tests/pos/i831.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test { self =>
2+
def a = 5
3+
self.a
4+
}

0 commit comments

Comments
 (0)