Skip to content

Commit 1ce80ff

Browse files
committed
Compute type params in namer without completing the whole info
Type params should be computed before computing the whole info of a type. Without the patch we get a cycluc reference in the compileMixed test.
1 parent 5f1e298 commit 1ce80ff

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,9 +1782,6 @@ object SymDenotations {
17821782
def apply(sym: Symbol) = this
17831783
def apply(module: TermSymbol, modcls: ClassSymbol) = this
17841784

1785-
/** The type parameters computed by the completer before completion has finished */
1786-
def completerTypeParams(sym: Symbol)(implicit ctx: Context): List[TypeSymbol] = Nil
1787-
17881785
private var myDecls: Scope = EmptyScope
17891786
private var mySourceModuleFn: Context => Symbol = NoSymbolFn
17901787
private var myModuleClassFn: Context => Symbol = NoSymbolFn
@@ -1805,6 +1802,14 @@ object SymDenotations {
18051802
def withModuleClass(moduleClassFn: Context => Symbol): this.type = { myModuleClassFn = moduleClassFn; this }
18061803
}
18071804

1805+
/** A subclass of LazyTypes where type parameters can be completed independently of
1806+
* the info.
1807+
*/
1808+
abstract class TypeParamsCompleter extends LazyType {
1809+
/** The type parameters computed by the completer before completion has finished */
1810+
def completerTypeParams(sym: Symbol): List[TypeSymbol]
1811+
}
1812+
18081813
val NoSymbolFn = (ctx: Context) => NoSymbol
18091814

18101815
/** A missing completer */

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package core
44
import Types._
55
import Contexts._
66
import Symbols._
7+
import SymDenotations.TypeParamsCompleter
78
import Decorators._
89
import util.Stats._
910
import util.common._
@@ -240,14 +241,21 @@ class TypeApplications(val self: Type) extends AnyVal {
240241
case self: TypeRef =>
241242
val tsym = self.symbol
242243
if (tsym.isClass) tsym.typeParams
243-
//else if (tsym.isAliasType) self.underlying.typeParams
244-
else if (tsym.isCompleting) tsym.completer.completerTypeParams(tsym)
245-
// We are facing a problem when computing the type parameters of an uncompleted
246-
// abstract type. We can't access the bounds of the symbol yet because that
247-
// would cause a cause a cyclic reference. So we return `Nil` instead
248-
// and try to make up for it later. The acrobatics in Scala2Unpicker#readType
249-
// for reading a TypeRef show what's neeed.
250-
else tsym.info.typeParams
244+
else tsym.infoOrCompleter match {
245+
case completer: TypeParamsCompleter =>
246+
val tparams = completer.completerTypeParams(tsym)
247+
if (tsym.isClass) tparams
248+
else defn.LambdaTrait(tparams.map(_.variance)).typeParams
249+
case _ =>
250+
if (!tsym.isCompleting || tsym.isAliasType) tsym.info.typeParams
251+
else
252+
// We are facing a problem when computing the type parameters of an uncompleted
253+
// abstract type. We can't access the bounds of the symbol yet because that
254+
// would cause a cause a cyclic reference. So we return `Nil` instead
255+
// and try to make up for it later. The acrobatics in Scala2Unpicker#readType
256+
// for reading a TypeRef show what's needed.
257+
Nil
258+
}
251259
case self: RefinedType =>
252260
// inlined and optimized version of
253261
// val sym = self.LambdaTrait

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,14 @@ class Namer { typer: Typer =>
478478
}
479479

480480
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */
481-
class Completer(val original: Tree)(implicit ctx: Context) extends LazyType {
481+
class Completer(val original: Tree)(implicit ctx: Context) extends TypeParamsCompleter {
482482

483483
protected def localContext(owner: Symbol) = ctx.fresh.setOwner(owner).setTree(original)
484484

485485
private var myTypeParams: List[TypeSymbol] = null
486486
private var nestedCtx: Context = null
487487

488-
override def completerTypeParams(sym: Symbol)(implicit ctx: Context): List[TypeSymbol] = {
488+
def completerTypeParams(sym: Symbol): List[TypeSymbol] = {
489489
if (myTypeParams == null) {
490490
//println(i"completing type params of $sym in ${sym.owner}")
491491
myTypeParams = original match {

test/dotc/tests.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ class tests extends CompilerTest {
186186
.toList
187187

188188
@Test def compileStdLib = compileList("compileStdLib", stdlibFiles, "-migration" :: scala2mode)
189+
@Test def compileMixed = compileLine(
190+
"""tests/pos/B.scala
191+
|./scala-scala/src/library/scala/collection/immutable/Seq.scala
192+
|./scala-scala/src/library/scala/package.scala
193+
|./scala-scala/src/library/scala/collection/GenSeqLike.scala
194+
|./scala-scala/src/library/scala/collection/SeqLike.scala
195+
|./scala-scala/src/library/scala/collection/generic/GenSeqFactory.scala""".stripMargin)
189196
@Test def compileIndexedSeq = compileLine("./scala-scala/src/library/scala/collection/immutable/IndexedSeq.scala")
190197

191198
@Test def dotty = compileDir(dottyDir, ".", List("-deep", "-Ycheck-reentrant"))(allowDeepSubtypes) // note the -deep argument

tests/pos/B.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object B{
2+
def main(args: Array[String]): Unit = {
3+
val s = List(1,2,3)
4+
()
5+
}
6+
}

0 commit comments

Comments
 (0)