Skip to content

Commit 71a8601

Browse files
committed
Fix #6989: Make sure scalacLinkedClass finds companion when unpickling
scalacLinkedClass uses unforcedDecls which might miss a companion object when unpickling. I tried to make scalacLinekdClass stricter, but ran into problems. It turns out that it is finely balanced as it is. The fix is to make sure the owner is completed before calling scalacLinkedClass from an unpickler.
1 parent 0d0c006 commit 71a8601

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ class TreeUnpickler(reader: TastyReader,
801801
ValDef(tpt)
802802
case TYPEDEF | TYPEPARAM =>
803803
if (sym.isClass) {
804+
sym.owner.ensureCompleted() // scalacLinkedClass uses unforcedDecls. Make sure it does not miss anything.
804805
val companion = sym.scalacLinkedClass
805806

806807
// Is the companion defined in the same Tasty file as `sym`?

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ object Scala2Unpickler {
122122
}
123123
if (!denot.flagsUNSAFE.isAllOf(JavaModule)) ensureConstructor(denot.symbol.asClass, decls)
124124

125+
denot.classSymbol.owner.ensureCompleted() // scalacLinkedClass uses unforcedDecls. Make sure it does not miss anything.
125126
val scalacCompanion = denot.classSymbol.scalacLinkedClass
126127

127128
def registerCompanionPair(module: Symbol, claz: Symbol) = {

tests/pos/i6989/Container_1.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package mypkg
2+
3+
object Container {
4+
class StringExtras(val s: String) extends AnyVal {
5+
def op(item: Int): Int = ???
6+
}
7+
}
8+
9+
trait Container {
10+
import Container._
11+
implicit def mkStringExtras(s: String): StringExtras = new StringExtras(s)
12+
}

tests/pos/i6989/SimpleTest_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package mypkg
2+
3+
class SimpleTest extends Container {
4+
"foo".op(5)
5+
}

0 commit comments

Comments
 (0)