Skip to content

Commit 7baead9

Browse files
committed
Add companion link symbols early only if companion actually exists
Otherwise we'll trigger early creation of companions that could shadow something.
1 parent 25af814 commit 7baead9

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,6 @@ object desugar {
330330
.withMods(synthetic))
331331
.withPos(cdef.pos).toList
332332

333-
def companionClassGeterMethod =
334-
DefDef(nme.COMPANION_CLASS_METHOD, Nil, Nil, Ident(name),
335-
/*Select(Select(
336-
Select(Ident(nme.ROOTPKG), nme.scala_),
337-
nme.Predef), nme.???)*/ Ident(nme.???))
338-
.withMods(synthetic | Private)
339-
340333
// The companion object defifinitions, if a companion is needed, Nil otherwise.
341334
// companion definitions include:
342335
// 1. If class is a case class case class C[Ts](p1: T1, ..., pN: TN)(moreParams):
@@ -366,13 +359,11 @@ object desugar {
366359
DefDef(nme.unapply, derivedTparams, (unapplyParam :: Nil) :: Nil, TypeTree(), unapplyRHS)
367360
.withMods(synthetic)
368361
}
369-
companionDefs(parent, companionClassGeterMethod :: applyMeths ::: unapplyMeth :: defaultGetters)
362+
companionDefs(parent, applyMeths ::: unapplyMeth :: defaultGetters)
370363
}
371364
else {
372-
val methods = if (!(mods is Synthetic | Module)) companionClassGeterMethod :: defaultGetters else defaultGetters
373-
374-
if(methods.nonEmpty)
375-
companionDefs(anyRef, methods)
365+
if (defaultGetters.nonEmpty)
366+
companionDefs(anyRef, defaultGetters)
376367
else Nil
377368
}
378369

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,32 +395,72 @@ class Namer { typer: Typer =>
395395
/** Create top-level symbols for statements and enter them into symbol table */
396396
def index(stats: List[Tree])(implicit ctx: Context): Context = {
397397

398+
val classDef = mutable.Map[TypeName, TypeDef]()
399+
val moduleDef = mutable.Map[TypeName, TypeDef]()
400+
398401
/** Merge the definitions of a synthetic companion generated by a case class
399402
* and the real companion, if both exist.
400403
*/
401404
def mergeCompanionDefs() = {
402-
val classDef = mutable.Map[TypeName, TypeDef]()
403405
for (cdef @ TypeDef(name, _) <- stats)
404-
if (cdef.isClassDef) classDef(name) = cdef
405-
for (mdef @ ModuleDef(name, _) <- stats)
406+
if (cdef.isClassDef) {
407+
classDef(name) = cdef
408+
cdef.attachmentOrElse(ExpandedTree, cdef) match {
409+
case Thicket(cls :: mval :: (mcls @ TypeDef(_, _: Template)) :: crest) =>
410+
moduleDef(name) = mcls
411+
case _ =>
412+
}
413+
}
414+
for (mdef @ ModuleDef(name, _) <- stats) {
415+
val typName = name.toTypeName
416+
val Thicket(vdef :: (mcls @ TypeDef(_, impl: Template)) :: Nil) = mdef.attachment(ExpandedTree)
417+
moduleDef(typName) = mcls
406418
classDef get name.toTypeName match {
407419
case Some(cdef) =>
408-
val Thicket(vdef :: (mcls @ TypeDef(_, impl: Template)) :: Nil) = mdef.attachment(ExpandedTree)
409420
cdef.attachmentOrElse(ExpandedTree, cdef) match {
410421
case Thicket(cls :: mval :: TypeDef(_, compimpl: Template) :: crest) =>
411422
val mcls1 = cpy.TypeDef(mcls)(
412423
rhs = cpy.Template(impl)(body = compimpl.body ++ impl.body))
413424
mdef.putAttachment(ExpandedTree, Thicket(vdef :: mcls1 :: Nil))
425+
moduleDef(typName) = mcls1
414426
cdef.putAttachment(ExpandedTree, Thicket(cls :: crest))
415427
case _ =>
416428
}
417429
case none =>
418430
}
431+
}
432+
}
433+
434+
def createLinks(classTree: TypeDef, moduleTree: TypeDef)(implicit ctx: Context) = {
435+
val claz = ctx.denotNamed(classTree.name.encode)
436+
val modl = ctx.denotNamed(moduleTree.name.encode)
437+
ctx.newSymbol(
438+
owner = modl.symbol,
439+
name = nme.COMPANION_CLASS_METHOD,
440+
flags = Flags.Synthetic | Flags.Private,
441+
info = ExprType(claz.symbol.typeRef)).entered
442+
ctx.newSymbol(
443+
owner = claz.symbol,
444+
name = nme.COMPANION_MODULE_METHOD,
445+
flags = Flags.Synthetic | Flags.Private,
446+
info = ExprType(modl.symbol.typeRef)).entered
447+
}
448+
449+
def createCompanionLinks(implicit ctx: Context): Unit = {
450+
for (cdef @ TypeDef(name, _) <- classDef.values) {
451+
moduleDef.getOrElse(name, EmptyTree) match {
452+
case t: TypeDef =>
453+
createLinks(cdef, t)
454+
case EmptyTree =>
455+
}
456+
}
419457
}
420458

421459
stats foreach expand
422460
mergeCompanionDefs()
423-
(ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
461+
val ctxWithStats = (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
462+
createCompanionLinks(ctxWithStats)
463+
ctxWithStats
424464
}
425465

426466
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */

0 commit comments

Comments
 (0)