Skip to content

Complete owners of type parameters before parameters themselves #10972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,31 @@ object SymDenotations {
}

final def completeFrom(completer: LazyType)(using Context): Unit =
if (Config.showCompletions) {
println(i"${" " * indent}completing ${if (isType) "type" else "val"} $name")
indent += 1

if (myFlags.is(Touched)) throw CyclicReference(this)
myFlags |= Touched

// completions.println(s"completing ${this.debugString}")
try atPhase(validFor.firstPhaseId)(completer.complete(this))
catch {
case ex: CyclicReference =>
println(s"error while completing ${this.debugString}")
throw ex
if completer.needsCompletion(this) then
if (Config.showCompletions) {
println(i"${" " * indent}completing ${if (isType) "type" else "val"} $name")
indent += 1

if (myFlags.is(Touched)) throw CyclicReference(this)
myFlags |= Touched

// completions.println(s"completing ${this.debugString}")
try atPhase(validFor.firstPhaseId)(completer.complete(this))
catch {
case ex: CyclicReference =>
println(s"error while completing ${this.debugString}")
throw ex
}
finally {
indent -= 1
println(i"${" " * indent}completed $name in $owner")
}
}
finally {
indent -= 1
println(i"${" " * indent}completed $name in $owner")
else {
if (myFlags.is(Touched)) throw CyclicReference(this)
myFlags |= Touched
atPhase(validFor.firstPhaseId)(completer.complete(this))
}
}
else {
if (myFlags.is(Touched)) throw CyclicReference(this)
myFlags |= Touched
atPhase(validFor.firstPhaseId)(completer.complete(this))
}

protected[dotc] def info_=(tp: Type): Unit = {
/* // DEBUG
Expand Down Expand Up @@ -2517,6 +2518,13 @@ object SymDenotations {
def withModuleClass(moduleClassFn: Context ?=> Symbol): this.type = { myModuleClassFn = moduleClassFn; this }

override def toString: String = getClass.toString

/** A hook that is called before trying to complete a symbol with its
* associated cycle detection via the Touched flag. This is overridden
* for Type definitions in Namer, where we make sure that owners are
* completed before nested types.
*/
def needsCompletion(symd: SymDenotation)(using Context): Boolean = true
}

object LazyType:
Expand Down
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ class TypeApplications(val self: Type) extends AnyVal {

/** If `self` is a generic class, its type parameter symbols, otherwise Nil */
final def typeParamSymbols(using Context): List[TypeSymbol] = typeParams match {
case (_: Symbol) :: _ =>
assert(typeParams.forall(_.isInstanceOf[Symbol]))
typeParams.asInstanceOf[List[TypeSymbol]]
case tparams @ (_: Symbol) :: _ =>
assert(tparams.forall(_.isInstanceOf[Symbol]))
tparams.asInstanceOf[List[TypeSymbol]]
// Note: Two successive calls to typeParams can yield different results here because
// of different completion status. I.e. the first call might produce some symbols,
// whereas the second call gives some LambdaParams. This was observed
// for ticket0137.scala
case _ => Nil
}

Expand Down
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,20 @@ class Namer { typer: Typer =>
private var nestedCtx: Context = null
assert(!original.isClassDef)

/** If completion of the owner of the to be completed symbol has not yet started,
* complete the owner first and check again. This prevents cyclic references
* where we need to copmplete a type parameter that has an owner that is not
* yet completed. Test case is pos/i10967.scala.
*/
override def needsCompletion(symd: SymDenotation)(using Context): Boolean =
val owner = symd.owner
!owner.exists
|| owner.is(Touched)
|| {
owner.ensureCompleted()
!symd.isCompleted
}

override def completerTypeParams(sym: Symbol)(using Context): List[TypeSymbol] =
if myTypeParams == null then
//println(i"completing type params of $sym in ${sym.owner}")
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i10638.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package woes

trait Txn[T <: Txn[T]]

trait Impl[Repr[~ <: Txn[~]]] {
final type Ext = Extension[Repr] // Huh!
}

trait Extension[Repr[~ <: Txn[~]]]
9 changes: 9 additions & 0 deletions tests/pos/i10967.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait SomeTrait

trait CollBase[A <: SomeTrait, +CC1[A2 <: SomeTrait]] {
val companion: CollCompanion[CC1]
}

trait Coll[A <: SomeTrait] extends CollBase[A, Coll]

trait CollCompanion[+CC2[A <: SomeTrait]]