Skip to content

sbt.ExtractAPI: Support TypeArgRef #3280

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 1 commit into from
Oct 7, 2017
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
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
new api.Annotated(tp, Array(marker))
private def marker(name: String) =
new api.Annotation(new api.Constant(Constants.emptyType, name), Array())
val typeArgRefMarker = marker("TypeArgRef")
val orMarker = marker("Or")
val byNameMarker = marker("ByName")

Expand Down Expand Up @@ -343,6 +344,12 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
}
}

// Hack to represent dotty types which don't have an equivalent in xsbti
def combineApiTypes(apiTps: api.Type*): api.Type = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what this is supposed to achieve.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically fitting a square peg in a round hole: sbt has its own representation of types which doesn't include anything like TypeArgRef, but we can still get incremental compilation to work correctly if we can find a bijection between dotty's types and sbt's types, since the only thing that matter is that the hash of the sbt type changes when the dotty type changes. Don't worry too much about it :).

new api.Structure(strict2lzy(apiTps.toArray),
strict2lzy(Array()), strict2lzy(Array()))
}

def apiType(tp: Type): api.Type = {
typeCache.getOrElseUpdate(tp, computeType(tp))
}
Expand Down Expand Up @@ -450,7 +457,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
// TODO: Add a real representation for AndOrTypes in xsbti. The order of
// types in an `AndOrType` does not change the API, so the API hash should
// be symmetric.
val s = new api.Structure(strict2lzy(parents.toArray), strict2lzy(Array()), strict2lzy(Array()))
val s = combineApiTypes(apiType(tp.tp1), apiType(tp.tp2))
if (tp.isAnd)
s
else
Expand All @@ -471,6 +478,9 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
apiType(tp.ref)
case tp: TypeVar =>
apiType(tp.underlying)
case TypeArgRef(prefix, clsRef, idx) =>
val apiClsWithIdx = withMarker(apiType(clsRef), marker(idx.toString))
withMarker(combineApiTypes(apiType(prefix), apiClsWithIdx), typeArgRefMarker)
case _ => {
ctx.warning(i"sbt-api: Unhandled type ${tp.getClass} : $tp")
Constants.emptyType
Expand Down
10 changes: 10 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/typeargref/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.collection.JavaConverters._
import java.util.{ Map => JMap }

class A {
// Inferred type for `param`: java.util.Map[Int, _ <: String]#<parameter V of trait Map>
def param = {
val opt: Option[JMap[Int, _ <: String]] = None
opt.getOrElse(Map.empty.asJava).get(42)
}
}
3 changes: 3 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/typeargref/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object B {
def foo(a: A): Int = a.param.length
}
10 changes: 10 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/typeargref/changes/A1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.collection.JavaConverters._
import java.util.{ Map => JMap }

class A {
// Inferred type for `param`: java.util.Map[Int, _ <: Int]#<parameter V of trait Map>
def param = {
val opt: Option[JMap[Int, _ <: Int]] = None
opt.getOrElse(Map.empty.asJava).get(42)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sbt._
import Keys._

object DottyInjectedPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements

override val projectSettings = Seq(
scalaVersion := sys.props("plugin.scalaVersion"),
scalacOptions += "-language:Scala2"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
4 changes: 4 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/typeargref/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> compile
$ copy-file changes/A1.scala A.scala
# Compilation of B.scala should fail because the signature of A#param has changed
-> compile