Skip to content

Commit d52f98c

Browse files
authored
Merge pull request #3280 from dotty-staging/sbt-extract-typeargref
sbt.ExtractAPI: Support TypeArgRef
2 parents ed22c31 + 0b565cb commit d52f98c

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
147147
new api.Annotated(tp, Array(marker))
148148
private def marker(name: String) =
149149
new api.Annotation(new api.Constant(Constants.emptyType, name), Array())
150+
val typeArgRefMarker = marker("TypeArgRef")
150151
val orMarker = marker("Or")
151152
val byNameMarker = marker("ByName")
152153

@@ -343,6 +344,12 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
343344
}
344345
}
345346

347+
// Hack to represent dotty types which don't have an equivalent in xsbti
348+
def combineApiTypes(apiTps: api.Type*): api.Type = {
349+
new api.Structure(strict2lzy(apiTps.toArray),
350+
strict2lzy(Array()), strict2lzy(Array()))
351+
}
352+
346353
def apiType(tp: Type): api.Type = {
347354
typeCache.getOrElseUpdate(tp, computeType(tp))
348355
}
@@ -450,7 +457,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
450457
// TODO: Add a real representation for AndOrTypes in xsbti. The order of
451458
// types in an `AndOrType` does not change the API, so the API hash should
452459
// be symmetric.
453-
val s = new api.Structure(strict2lzy(parents.toArray), strict2lzy(Array()), strict2lzy(Array()))
460+
val s = combineApiTypes(apiType(tp.tp1), apiType(tp.tp2))
454461
if (tp.isAnd)
455462
s
456463
else
@@ -471,6 +478,9 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
471478
apiType(tp.ref)
472479
case tp: TypeVar =>
473480
apiType(tp.underlying)
481+
case TypeArgRef(prefix, clsRef, idx) =>
482+
val apiClsWithIdx = withMarker(apiType(clsRef), marker(idx.toString))
483+
withMarker(combineApiTypes(apiType(prefix), apiClsWithIdx), typeArgRefMarker)
474484
case _ => {
475485
ctx.warning(i"sbt-api: Unhandled type ${tp.getClass} : $tp")
476486
Constants.emptyType
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.collection.JavaConverters._
2+
import java.util.{ Map => JMap }
3+
4+
class A {
5+
// Inferred type for `param`: java.util.Map[Int, _ <: String]#<parameter V of trait Map>
6+
def param = {
7+
val opt: Option[JMap[Int, _ <: String]] = None
8+
opt.getOrElse(Map.empty.asJava).get(42)
9+
}
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object B {
2+
def foo(a: A): Int = a.param.length
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.collection.JavaConverters._
2+
import java.util.{ Map => JMap }
3+
4+
class A {
5+
// Inferred type for `param`: java.util.Map[Int, _ <: Int]#<parameter V of trait Map>
6+
def param = {
7+
val opt: Option[JMap[Int, _ <: Int]] = None
8+
opt.getOrElse(Map.empty.asJava).get(42)
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion"),
10+
scalacOptions += "-language:Scala2"
11+
)
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
> compile
2+
$ copy-file changes/A1.scala A.scala
3+
# Compilation of B.scala should fail because the signature of A#param has changed
4+
-> compile

0 commit comments

Comments
 (0)