From 40e6397dd8060cd7ad1daf1b10965ad3825b8a59 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Fri, 29 Jun 2018 17:58:31 +0200 Subject: [PATCH 1/4] Resolve SeqType based on alias defined in scala package object - in 2.12: scala.collection.Seq - in 2.13: scala.collection.immutable.Seq Revert when we drop support for 2.12? --- compiler/src/dotty/tools/dotc/core/Definitions.scala | 9 ++++++++- compiler/src/dotty/tools/dotc/typer/Implicits.scala | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 18f46407ddd8..229ebc2ad5bd 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -401,7 +401,14 @@ class Definitions { List(AnyClass.typeRef), EmptyScope) lazy val SingletonType: TypeRef = SingletonClass.typeRef - lazy val SeqType: TypeRef = ctx.requiredClassRef("scala.collection.Seq") + lazy val SeqType: TypeRef = { + // We load SeqType from the alias in scala package object + // - in 2.12: scala.collection.Seq + // - in 2.13: scala.collection.immutable.Seq + val alias = ctx.base.staticRef("scala.Seq".toTypeName).requiredSymbol(_.isAliasType) + alias.info.classSymbol.typeRef + } + def SeqClass(implicit ctx: Context) = SeqType.symbol.asClass lazy val Seq_applyR = SeqClass.requiredMethodRef(nme.apply) def Seq_apply(implicit ctx: Context) = Seq_applyR.symbol diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index 848d928d7cf5..4ba82bc897b9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -71,7 +71,7 @@ object Implicits { /** The implicit references */ def refs: List[ImplicitRef] - private var SingletonClass: ClassSymbol = null + private[this] var SingletonClass: ClassSymbol = null /** Widen type so that it is neither a singleton type nor a type that inherits from scala.Singleton. */ private def widenSingleton(tp: Type)(implicit ctx: Context): Type = { From e89b440b1f0ccda7ab79385d173b386c00af2810 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Fri, 29 Jun 2018 17:59:55 +0200 Subject: [PATCH 2/4] More tests for repeated --- tests/pos/repeatedArgs.scala | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/pos/repeatedArgs.scala b/tests/pos/repeatedArgs.scala index f9abb4aee70e..d11f270824fd 100644 --- a/tests/pos/repeatedArgs.scala +++ b/tests/pos/repeatedArgs.scala @@ -1,3 +1,15 @@ object testRepeated { - def foo = java.lang.System.out.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") + def foo = java.lang.System.out.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") + + def bar(xs: Int*): Int = xs.length + def bat(xs: scala.Seq[Int]): Int = xs.length + + def test(xs: List[Int]): Unit = { + bar(1, 2, 3) + bar(xs: _*) + + val List(_, ys: _*) = xs + bar(ys: _*) + bat(ys) + } } From 97db2a94ffb77d2f4e6141dddeb93b7f2207c13a Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Tue, 10 Jul 2018 15:33:13 +0200 Subject: [PATCH 3/4] Add test that exercises a cycle reference error --- tests/pos/seqtype-cycle/Test1.scala | 3 +++ tests/pos/seqtype-cycle/Test2.scala | 5 +++++ tests/pos/seqtype-cycle/Test3.scala | 5 +++++ 3 files changed, 13 insertions(+) create mode 100644 tests/pos/seqtype-cycle/Test1.scala create mode 100644 tests/pos/seqtype-cycle/Test2.scala create mode 100644 tests/pos/seqtype-cycle/Test3.scala diff --git a/tests/pos/seqtype-cycle/Test1.scala b/tests/pos/seqtype-cycle/Test1.scala new file mode 100644 index 000000000000..ee8d10d7c36a --- /dev/null +++ b/tests/pos/seqtype-cycle/Test1.scala @@ -0,0 +1,3 @@ +class Test { + def bar = Array(1) // call to Array(_: Repeated) +} diff --git a/tests/pos/seqtype-cycle/Test2.scala b/tests/pos/seqtype-cycle/Test2.scala new file mode 100644 index 000000000000..e5cecd7e63da --- /dev/null +++ b/tests/pos/seqtype-cycle/Test2.scala @@ -0,0 +1,5 @@ +package object scala { + type Throwable = java.lang.Throwable // needed for some reasons + + type Seq[+A] = scala.Seq[A] +} diff --git a/tests/pos/seqtype-cycle/Test3.scala b/tests/pos/seqtype-cycle/Test3.scala new file mode 100644 index 000000000000..6426d14f6591 --- /dev/null +++ b/tests/pos/seqtype-cycle/Test3.scala @@ -0,0 +1,5 @@ +package scala + +trait Seq[@specialized(1, 2) A] + +class specialized(types: Int*) extends scala.annotation.StaticAnnotation From ea8a79889cea2136c20f3264d01675e657a320ee Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Tue, 10 Jul 2018 18:25:55 +0200 Subject: [PATCH 4/4] Try using the val alias --- .../src/dotty/tools/dotc/core/Definitions.scala | 17 +++++++++++++++-- tests/pos/seqtype-cycle/Test2.scala | 3 ++- tests/pos/seqtype-cycle/Test3.scala | 11 +++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 229ebc2ad5bd..a02977950680 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -405,8 +405,21 @@ class Definitions { // We load SeqType from the alias in scala package object // - in 2.12: scala.collection.Seq // - in 2.13: scala.collection.immutable.Seq - val alias = ctx.base.staticRef("scala.Seq".toTypeName).requiredSymbol(_.isAliasType) - alias.info.classSymbol.typeRef + + // force to avoid cycles + // ctx.requiredClassRef("scala.collection.Seq").symbol.ensureCompleted() + // ctx.requiredClassRef("scala.collection.immutable.Seq").symbol.ensureCompleted() + + // // Using type Seq[+A] = ... + // val alias = ctx.base.staticRef("scala.Seq".toTypeName).requiredSymbol(_.isAliasType) + // alias.info.classSymbol.typeRef + + // Using val Seq = ... + val scalaSeq = ctx.base.staticRef("scala.Seq".toTermName) + val seqName = scalaSeq.info.resultType.termSymbol.fullName + ctx.requiredClassRef(seqName.toTypeName) + + // ctx.requiredClassRef("scala.collection.Seq") } def SeqClass(implicit ctx: Context) = SeqType.symbol.asClass diff --git a/tests/pos/seqtype-cycle/Test2.scala b/tests/pos/seqtype-cycle/Test2.scala index e5cecd7e63da..a6c2ad67da30 100644 --- a/tests/pos/seqtype-cycle/Test2.scala +++ b/tests/pos/seqtype-cycle/Test2.scala @@ -1,5 +1,6 @@ package object scala { type Throwable = java.lang.Throwable // needed for some reasons - type Seq[+A] = scala.Seq[A] + type Seq[+A] = scala.collection.Seq[A] + val Seq = scala.collection.Seq } diff --git a/tests/pos/seqtype-cycle/Test3.scala b/tests/pos/seqtype-cycle/Test3.scala index 6426d14f6591..f137ded9ffa0 100644 --- a/tests/pos/seqtype-cycle/Test3.scala +++ b/tests/pos/seqtype-cycle/Test3.scala @@ -1,5 +1,12 @@ package scala -trait Seq[@specialized(1, 2) A] - class specialized(types: Int*) extends scala.annotation.StaticAnnotation + +package collection { + class Foo[@specialized(1, 2) A] + + trait Seq[A] extends Foo[A] + object Seq extends SeqFactory[Seq] + + trait SeqFactory[CC[X] <: Seq[X]] +}