Skip to content

Resolve SeqType based on the alias defined in scala package object #4743

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

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 21 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,27 @@ 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

// 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
lazy val Seq_applyR = SeqClass.requiredMethodRef(nme.apply)
def Seq_apply(implicit ctx: Context) = Seq_applyR.symbol
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 13 additions & 1 deletion tests/pos/repeatedArgs.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions tests/pos/seqtype-cycle/Test1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Test {
def bar = Array(1) // call to Array(_: Repeated)
}
6 changes: 6 additions & 0 deletions tests/pos/seqtype-cycle/Test2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package object scala {
type Throwable = java.lang.Throwable // needed for some reasons

type Seq[+A] = scala.collection.Seq[A]
val Seq = scala.collection.Seq
}
12 changes: 12 additions & 0 deletions tests/pos/seqtype-cycle/Test3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package scala

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]]
}