Skip to content

Scala2Unpickler: Fix annotation argument handling #14334

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
Jan 27, 2022
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
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