Skip to content

Commit 6c0d40d

Browse files
committed
Fix #831
Need to create a self symbols for modules with explicit self type, but need to take care it is already typed, or sourceModule risks running into CyclicReferences.
1 parent 2b5b064 commit 6c0d40d

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

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

Lines changed: 24 additions & 17 deletions
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
/** Add moduleClass/sourceModule to completer if it is for a module val or class */
238239
def adjustIfModule(completer: LazyType, tree: MemberDef) =
239240
if (tree.mods is Module) ctx.adjustModuleCompleter(completer, tree.name.encode)
@@ -261,10 +262,10 @@ class Namer { typer: Typer =>
261262
tree match {
262263
case tree: TypeDef if tree.isClassDef =>
263264
val name = checkNoConflict(tree.name.encode).asTypeName
264-
val cls = record(ctx.newClassSymbol(
265+
val cls = recordSym(ctx.newClassSymbol(
265266
ctx.owner, name, tree.mods.flags | inSuperCall,
266267
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
267-
privateWithinClass(tree.mods), tree.pos, ctx.source.file))
268+
privateWithinClass(tree.mods), tree.pos, ctx.source.file), tree)
268269
cls.completer.asInstanceOf[ClassCompleter].init()
269270
cls
270271
case tree: MemberDef =>
@@ -290,13 +291,13 @@ class Namer { typer: Typer =>
290291
// have no implementation.
291292
val cctx = if (tree.name == nme.CONSTRUCTOR && !(tree.mods is JavaDefined)) ctx.outer else ctx
292293

293-
record(ctx.newSymbol(
294+
recordSym(ctx.newSymbol(
294295
ctx.owner, name, tree.mods.flags | deferred | method | higherKinded | inSuperCall1,
295296
adjustIfModule(new Completer(tree)(cctx), tree),
296-
privateWithinClass(tree.mods), tree.pos))
297+
privateWithinClass(tree.mods), tree.pos), tree)
297298
case tree: Import =>
298-
record(ctx.newSymbol(
299-
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos))
299+
recordSym(ctx.newSymbol(
300+
ctx.owner, nme.IMPORT, Synthetic, new Completer(tree), NoSymbol, tree.pos), tree)
300301
case _ =>
301302
NoSymbol
302303
}
@@ -564,7 +565,13 @@ class Namer { typer: Typer =>
564565

565566
val selfInfo =
566567
if (self.isEmpty) NoType
567-
else if (cls is Module) cls.owner.thisType select sourceModule
568+
else if (cls.is(Module)) {
569+
val moduleType = cls.owner.thisType select sourceModule
570+
if (self.name == nme.WILDCARD) moduleType
571+
else recordSym(
572+
ctx.newSymbol(cls, self.name, self.mods.flags, moduleType, coord = self.pos),
573+
self)
574+
}
568575
else createSymbol(self)
569576

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

tests/pos/i831.scala

Lines changed: 4 additions & 0 deletions
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)