Skip to content

Commit

Permalink
Merge pull request #14334 from dotty-staging/fix-scala2-arrayannotarg
Browse files Browse the repository at this point in the history
Scala2Unpickler: Fix annotation argument handling
  • Loading branch information
bishabosha authored Jan 27, 2022
2 parents afbb5a5 + e43eb18 commit 3c82006
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -959,33 +959,33 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
/** Read an annotation argument, which is pickled either
* as a Constant or a Tree.
*/
protected def readAnnotArg(i: Int)(using Context): Tree = bytes(index(i)) match {
protected def readAnnotArg(i: Int)(using Context): untpd.Tree = untpd.TypedSplice(bytes(index(i)) match
case TREE => at(i, () => readTree())
case _ => at(i, () =>
readConstant() match
case c: Constant => Literal(c)
case tp: TermRef => ref(tp)
)
}
)

/** Read a ClassfileAnnotArg (argument to a classfile annotation)
*/
private def readArrayAnnotArg()(using Context): Tree = {
private def readArrayAnnotArg()(using Context): untpd.Tree = {
readByte() // skip the `annotargarray` tag
val end = readNat() + readIndex
// array elements are trees representing instances of scala.annotation.Annotation
SeqLiteral(
untpd.JavaSeqLiteral(
until(end, () => readClassfileAnnotArg(readNat())),
TypeTree(defn.AnnotationClass.typeRef))
untpd.TypeTree())
}

private def readAnnotInfoArg()(using Context): Tree = {
private def readAnnotInfoArg()(using Context): untpd.Tree = untpd.TypedSplice {
readByte() // skip the `annotinfo` tag
val end = readNat() + readIndex
readAnnotationContents(end)
}

protected def readClassfileAnnotArg(i: Int)(using Context): Tree = bytes(index(i)) match {
protected def readClassfileAnnotArg(i: Int)(using Context): untpd.Tree = bytes(index(i)) match {
case ANNOTINFO => at(i, () => readAnnotInfoArg())
case ANNOTARGARRAY => at(i, () => readArrayAnnotArg())
case _ => readAnnotArg(i)
Expand All @@ -997,22 +997,22 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
protected def readAnnotationContents(end: Int)(using Context): Tree = {
val atp = readTypeRef()
val args = {
val t = new ListBuffer[Tree]
val t = new ListBuffer[untpd.Tree]

while (readIndex != end) {
val argref = readNat()
t += {
if (isNameEntry(argref)) {
val name = at(argref, () => readName())
val arg = readClassfileAnnotArg(readNat())
NamedArg(name.asTermName, arg)
untpd.NamedArg(name.asTermName, arg)
}
else readAnnotArg(argref)
}
}
t.toList
}
resolveConstructor(atp, args)
untpd.resolveConstructor(atp, args)
}

/** Read an annotation and as a side effect store it into
Expand Down
6 changes: 6 additions & 0 deletions sbt-test/scala2-compat/i14223/app/App.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import kc.Encoder

def test: Unit =
val cellEncoder = new Encoder[String] {
override def encode: String = ""
}
13 changes: 13 additions & 0 deletions sbt-test/scala2-compat/i14223/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
val scala3Version = sys.props("plugin.scalaVersion")
val scala2Version = sys.props("plugin.scala2Version")

lazy val lib = project.in(file("lib"))
.settings(
scalaVersion := scala2Version
)

lazy val app = project.in(file("app"))
.dependsOn(lib)
.settings(
scalaVersion := scala3Version
)
12 changes: 12 additions & 0 deletions sbt-test/scala2-compat/i14223/lib/Lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kc

trait Encoder[E] {
def encode: E

@SuppressWarnings(Array("foo"))
def tag1: Encoder[E] = ???

// Make sure we handle empty Array literals too where we can't guess the Array type from its elements
@SuppressWarnings(Array())
def tag2: Encoder[E] = ???
}
1 change: 1 addition & 0 deletions sbt-test/scala2-compat/i14223/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> app/compile

0 comments on commit 3c82006

Please sign in to comment.