From eaffc785be1e42c3a44ce149dfb8cabb6681d7c6 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 20 Jul 2016 23:23:51 +0200 Subject: [PATCH 1/3] Tweaks to printing - increase page width - print scopes more legibly under -verbose --- src/dotty/tools/dotc/config/ScalaSettings.scala | 2 +- src/dotty/tools/dotc/printing/RefinedPrinter.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/config/ScalaSettings.scala b/src/dotty/tools/dotc/config/ScalaSettings.scala index b2fa745f14eb..1c1c7267172a 100644 --- a/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -37,7 +37,7 @@ class ScalaSettings extends Settings.SettingGroup { val usejavacp = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.") val verbose = BooleanSetting("-verbose", "Output messages about what the compiler is doing.") val version = BooleanSetting("-version", "Print product version and exit.") - val pageWidth = IntSetting("-pagewidth", "Set page width", 80) + val pageWidth = IntSetting("-pagewidth", "Set page width", 120) val jvmargs = PrefixSetting("-J", "-J", "Pass directly to the runtime system.") val defines = PrefixSetting("-Dproperty=value", "-D", "Pass -Dproperty=value directly to the runtime system.") diff --git a/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/src/dotty/tools/dotc/printing/RefinedPrinter.scala index bdfce266ca5e..ce063f06a221 100644 --- a/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -403,7 +403,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { case impl: Template => modText(tree.mods, if ((tree).mods is Trait) "trait" else "class") ~~ nameIdText(tree) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ - (if (tree.hasType && ctx.settings.verbose.value) s"[decls = ${tree.symbol.info.decls}]" else "") + (if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "") case rhs: TypeBoundsTree => typeDefText(toText(rhs)) case _ => From c37185d3307e2b02e25e888fd44d5e8bba95aa1d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 21 Jul 2016 14:51:16 +0200 Subject: [PATCH 2/3] Fix #1401: Make sure all refs are forwarded Faced with recursive dependencies through self types, we might have to apply `normalizeToClassRefs` to a class P with a parent that is not yet initialized (witnessed by P's parents being Nil). In that case we should still execute forwardRefs on P, but we have to wait in a suspension until P is initialized. This avoids the problem raised in #1401. I am still not quite sure why forwardRefs is needed, but it seems that asSeenFrom alone is not enough to track the dependencies in this case. --- src/dotty/tools/dotc/core/TypeOps.scala | 22 +++++++++++----- src/dotty/tools/dotc/core/Types.scala | 13 +++++++++- .../core/unpickleScala2/Scala2Unpickler.scala | 2 +- src/dotty/tools/dotc/typer/Namer.scala | 5 +++- test/dotc/scala-collections.whitelist | 2 +- tests/pos/i1401.scala | 25 +++++++++++++++++++ 6 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 tests/pos/i1401.scala diff --git a/src/dotty/tools/dotc/core/TypeOps.scala b/src/dotty/tools/dotc/core/TypeOps.scala index 10b0f5615642..9433cb882d39 100644 --- a/src/dotty/tools/dotc/core/TypeOps.scala +++ b/src/dotty/tools/dotc/core/TypeOps.scala @@ -361,13 +361,23 @@ trait TypeOps { this: Context => // TODO: Make standalone object. * to the current scope, provided (1) variances of both aliases are the same, and * (2) X is not yet defined in current scope. This "short-circuiting" prevents * long chains of aliases which would have to be traversed in type comparers. + * + * Note: Test i1401.scala shows that `forwardRefs` is also necessary + * for typechecking in the case where self types refer to type parameters + * that are upper-bounded by subclass instances. */ def forwardRefs(from: Symbol, to: Type, prefs: List[TypeRef]) = to match { case to @ TypeBounds(lo1, hi1) if lo1 eq hi1 => - for (pref <- prefs) - for (argSym <- pref.decls) - if (argSym is BaseTypeArg) - forwardRef(argSym, from, to, cls, decls) + for (pref <- prefs) { + def forward(): Unit = + for (argSym <- pref.decls) + if (argSym is BaseTypeArg) + forwardRef(argSym, from, to, cls, decls) + pref.info match { + case info: TempClassInfo => info.suspensions = (() => forward()) :: info.suspensions // !!! dotty deviation `forward` alone does not eta expand + case _ => forward() + } + } case _ => } @@ -419,9 +429,9 @@ trait TypeOps { this: Context => // TODO: Make standalone object. s"redefinition of ${decls.lookup(name).debugString} in ${cls.showLocated}") enterArgBinding(formals(name), refinedInfo, cls, decls) } - // Forward definitions in super classes that have one of the refined paramters + // Forward definitions in super classes that have one of the refined parameters // as aliases directly to the refined info. - // Note that this cannot be fused bwith the previous loop because we now + // Note that this cannot be fused with the previous loop because we now // assume that all arguments have been entered in `decls`. refinements foreachBinding { (name, refinedInfo) => forwardRefs(formals(name), refinedInfo, parentRefs) diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 11da27265289..8019750bffac 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -3043,9 +3043,20 @@ object Types { override def toString = s"ClassInfo($prefix, $cls)" } - final class CachedClassInfo(prefix: Type, cls: ClassSymbol, classParents: List[TypeRef], decls: Scope, selfInfo: DotClass) + class CachedClassInfo(prefix: Type, cls: ClassSymbol, classParents: List[TypeRef], decls: Scope, selfInfo: DotClass) extends ClassInfo(prefix, cls, classParents, decls, selfInfo) + /** A class for temporary class infos where `parents` are not yet known. */ + final class TempClassInfo(prefix: Type, cls: ClassSymbol, decls: Scope, selfInfo: DotClass) + extends CachedClassInfo(prefix, cls, Nil, decls, selfInfo) { + + /** A list of actions that were because they rely on the class info of `cls` to + * be no longer temporary. These actions will be performed once `cls` gets a real + * ClassInfo. + */ + var suspensions: List[() => Unit] = Nil + } + object ClassInfo { def apply(prefix: Type, cls: ClassSymbol, classParents: List[TypeRef], decls: Scope, selfInfo: DotClass = NoType)(implicit ctx: Context) = unique(new CachedClassInfo(prefix, cls, classParents, decls, selfInfo)) diff --git a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 3dbeb4040806..14747619a043 100644 --- a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -106,7 +106,7 @@ object Scala2Unpickler { // `denot.sourceModule.exists` provision i859.scala crashes in the backend. denot.owner.thisType select denot.sourceModule else selfInfo - denot.info = ClassInfo(denot.owner.thisType, denot.classSymbol, Nil, decls, ost) // first rough info to avoid CyclicReferences + denot.info = new TempClassInfo(denot.owner.thisType, denot.classSymbol, decls, ost) // first rough info to avoid CyclicReferences var parentRefs = ctx.normalizeToClassRefs(parents, cls, decls) if (parentRefs.isEmpty) parentRefs = defn.ObjectType :: Nil for (tparam <- tparams) { diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala index 3b193d2dba4e..20cf098468be 100644 --- a/src/dotty/tools/dotc/typer/Namer.scala +++ b/src/dotty/tools/dotc/typer/Namer.scala @@ -688,7 +688,8 @@ class Namer { typer: Typer => else createSymbol(self) // pre-set info, so that parent types can refer to type params - denot.info = ClassInfo(cls.owner.thisType, cls, Nil, decls, selfInfo) + val tempClassInfo = new TempClassInfo(cls.owner.thisType, cls, decls, selfInfo) + denot.info = tempClassInfo // Ensure constructor is completed so that any parameter accessors // which have type trees deriving from its parameters can be @@ -705,6 +706,8 @@ class Namer { typer: Typer => index(rest)(inClassContext(selfInfo)) denot.info = ClassInfo(cls.owner.thisType, cls, parentRefs, decls, selfInfo) + tempClassInfo.suspensions.foreach(_()) + Checking.checkWellFormed(cls) if (isDerivedValueClass(cls)) cls.setFlag(Final) cls.setApplicableFlags( diff --git a/test/dotc/scala-collections.whitelist b/test/dotc/scala-collections.whitelist index 50a3f3479537..29db48cf2c31 100644 --- a/test/dotc/scala-collections.whitelist +++ b/test/dotc/scala-collections.whitelist @@ -83,7 +83,7 @@ ./scala-scala/src/library/scala/collection/immutable/Seq.scala ./scala-scala/src/library/scala/collection/mutable/IndexedSeq.scala ./scala-scala/src/library/scala/collection/mutable/ListBuffer.scala -#./scala-scala/src/library/scala/collection/mutable/BufferLike.scala // works under junit, fails under partest, but can't see more info on the cause +./scala-scala/src/library/scala/collection/mutable/BufferLike.scala ./scala-scala/src/library/scala/collection/mutable/ArrayBuilder.scala diff --git a/tests/pos/i1401.scala b/tests/pos/i1401.scala new file mode 100644 index 000000000000..140d78e7f621 --- /dev/null +++ b/tests/pos/i1401.scala @@ -0,0 +1,25 @@ +package i1401 + +trait Subtractable[A, +Repr <: Subtractable[A, Repr]] { + def -(elem: A): Repr +} + +trait BufferLike[BA, +This <: BufferLike[BA, This] with Buffer[BA]] + extends Subtractable[BA, This] +{ self : This => + + /* Without fix-#1401: + * + error: overriding method - in trait Subtractable of type (elem: A)This & i1401.Buffer[A]; + method - of type (elem: BA)This has incompatible type + def -(elem: BA): This + ^ + one error found + */ + def -(elem: BA): This +} + +trait Buffer[A] extends BufferLike[A, Buffer[A]] + + + From 1a3aeb147dff4875d8bf6701571babdaca9d869c Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 22 Jul 2016 16:12:16 +0200 Subject: [PATCH 3/3] Resume suspensions also when reading from classfiles Make treatment in Scala2Unpickler and Namer the same and factor out common functionality. --- src/dotty/tools/dotc/core/TypeOps.scala | 2 +- src/dotty/tools/dotc/core/Types.scala | 12 ++++++++++-- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 6 +++--- src/dotty/tools/dotc/typer/Namer.scala | 7 +++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/dotty/tools/dotc/core/TypeOps.scala b/src/dotty/tools/dotc/core/TypeOps.scala index 9433cb882d39..93b1b1f02e7a 100644 --- a/src/dotty/tools/dotc/core/TypeOps.scala +++ b/src/dotty/tools/dotc/core/TypeOps.scala @@ -374,7 +374,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object. if (argSym is BaseTypeArg) forwardRef(argSym, from, to, cls, decls) pref.info match { - case info: TempClassInfo => info.suspensions = (() => forward()) :: info.suspensions // !!! dotty deviation `forward` alone does not eta expand + case info: TempClassInfo => info.addSuspension(forward) case _ => forward() } } diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 8019750bffac..37342810835a 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -3049,12 +3049,20 @@ object Types { /** A class for temporary class infos where `parents` are not yet known. */ final class TempClassInfo(prefix: Type, cls: ClassSymbol, decls: Scope, selfInfo: DotClass) extends CachedClassInfo(prefix, cls, Nil, decls, selfInfo) { - + /** A list of actions that were because they rely on the class info of `cls` to * be no longer temporary. These actions will be performed once `cls` gets a real * ClassInfo. */ - var suspensions: List[() => Unit] = Nil + private var suspensions: List[() => Unit] = Nil + + def addSuspension(suspension: () => Unit): Unit = suspensions ::= suspension + + /** Install classinfo with known parents in `denot` and resume all suspensions */ + def finalize(denot: SymDenotation, parents: List[TypeRef])(implicit ctx: Context) = { + denot.info = derivedClassInfo(classParents = parents) + suspensions.foreach(_()) + } } object ClassInfo { diff --git a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 14747619a043..8ea4cecde6e9 100644 --- a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -106,7 +106,8 @@ object Scala2Unpickler { // `denot.sourceModule.exists` provision i859.scala crashes in the backend. denot.owner.thisType select denot.sourceModule else selfInfo - denot.info = new TempClassInfo(denot.owner.thisType, denot.classSymbol, decls, ost) // first rough info to avoid CyclicReferences + val tempInfo = new TempClassInfo(denot.owner.thisType, denot.classSymbol, decls, ost) + denot.info = tempInfo // first rough info to avoid CyclicReferences var parentRefs = ctx.normalizeToClassRefs(parents, cls, decls) if (parentRefs.isEmpty) parentRefs = defn.ObjectType :: Nil for (tparam <- tparams) { @@ -132,8 +133,7 @@ object Scala2Unpickler { registerCompanionPair(scalacCompanion, denot.classSymbol) } - denot.info = ClassInfo( // final info, except possibly for typeparams ordering - denot.owner.thisType, denot.classSymbol, parentRefs, decls, ost) + tempInfo.finalize(denot, parentRefs) // install final info, except possibly for typeparams ordering denot.ensureTypeParamsInCorrectOrder() } } diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala index 20cf098468be..26c8f5c955b1 100644 --- a/src/dotty/tools/dotc/typer/Namer.scala +++ b/src/dotty/tools/dotc/typer/Namer.scala @@ -688,8 +688,8 @@ class Namer { typer: Typer => else createSymbol(self) // pre-set info, so that parent types can refer to type params - val tempClassInfo = new TempClassInfo(cls.owner.thisType, cls, decls, selfInfo) - denot.info = tempClassInfo + val tempInfo = new TempClassInfo(cls.owner.thisType, cls, decls, selfInfo) + denot.info = tempInfo // Ensure constructor is completed so that any parameter accessors // which have type trees deriving from its parameters can be @@ -705,8 +705,7 @@ class Namer { typer: Typer => typr.println(s"completing $denot, parents = $parents, parentTypes = $parentTypes, parentRefs = $parentRefs") index(rest)(inClassContext(selfInfo)) - denot.info = ClassInfo(cls.owner.thisType, cls, parentRefs, decls, selfInfo) - tempClassInfo.suspensions.foreach(_()) + tempInfo.finalize(denot, parentRefs) Checking.checkWellFormed(cls) if (isDerivedValueClass(cls)) cls.setFlag(Final)