Skip to content

Commit 57d05d3

Browse files
committed
Merge pull request #958 from dotty-staging/companion-methods
Links between companions after unpickling are added
2 parents eefa611 + 9c30c9b commit 57d05d3

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,15 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
588588
sym.info = readType()
589589
ValDef(sym.asTerm, readRhs(localCtx))
590590
case TYPEDEF | TYPEPARAM =>
591-
if (sym.isClass)
591+
if (sym.isClass) {
592+
val companion = sym.scalacLinkedClass
593+
if (companion != NoSymbol) {
594+
import transform.SymUtils._
595+
if (sym is Flags.ModuleClass) sym.registerCompanionMethod(nme.COMPANION_CLASS_METHOD, companion)
596+
else sym.registerCompanionMethod(nme.COMPANION_MODULE_METHOD, companion)
597+
}
592598
ta.assignType(untpd.TypeDef(sym.name.asTypeName, readTemplate(localCtx)), sym)
593-
else {
599+
} else {
594600
sym.info = readType()
595601
TypeDef(sym.asType)
596602
}

src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,10 @@ object Scala2Unpickler {
117117
val scalacCompanion = denot.classSymbol.scalacLinkedClass
118118

119119
def registerCompanionPair(module: Symbol, claz: Symbol) = {
120-
def registerCompanionMethod(name: Name, target: Symbol, owner: Symbol) = {
121-
if (!owner.unforcedDecls.lookup(name).exists) {
122-
val companionMethod = ctx.synthesizeCompanionMethod(name, target, owner)
123-
if (companionMethod.exists) {
124-
companionMethod.entered
125-
}
126-
}
127-
}
128-
registerCompanionMethod(nme.COMPANION_CLASS_METHOD, claz, module)
120+
import transform.SymUtils._
121+
module.registerCompanionMethod(nme.COMPANION_CLASS_METHOD, claz)
129122
if (claz.isClass) {
130-
registerCompanionMethod(nme.COMPANION_MODULE_METHOD, module, claz)
123+
claz.registerCompanionMethod(nme.COMPANION_MODULE_METHOD, module)
131124
}
132125
}
133126

src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,13 @@ class SymUtils(val self: Symbol) extends AnyVal {
9999
self.addAnnotations(from.annotationsCarrying(meta))
100100
self
101101
}
102+
103+
def registerCompanionMethod(name: Name, target: Symbol)(implicit ctx: Context) = {
104+
if (!self.unforcedDecls.lookup(name).exists) {
105+
val companionMethod = ctx.synthesizeCompanionMethod(name, target, self)
106+
if (companionMethod.exists) {
107+
companionMethod.entered
108+
}
109+
}
110+
}
102111
}

tests/pos/i0881/A_1.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class A
2+
object A {
3+
implicit val theA: A = new A
4+
}

tests/pos/i0881/B_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class B {
2+
def getA(implicit a: A): A = a
3+
def test = {
4+
getA
5+
}
6+
}

0 commit comments

Comments
 (0)