From e43eb18ca8d8ddc8d11cdc4232de023aa1a284b8 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Thu, 6 Jan 2022 15:38:40 +0100 Subject: [PATCH] Scala2Unpickler: Fix annotation argument handling When an annotation argument is an Array, we can't know the element type of the Array until we've applied the argument to the annotation constructor, so we need to unpickle annotation arguments as untyped trees and rely on resolveConstructor to type them. This is the same logic we already use when unpickling Java classfiles since 610450f4f91be6c416a41ee2887f667e21385425. Fixes #14223. --- .../core/unpickleScala2/Scala2Unpickler.scala | 20 +++++++++---------- sbt-test/scala2-compat/i14223/app/App.scala | 6 ++++++ sbt-test/scala2-compat/i14223/build.sbt | 13 ++++++++++++ sbt-test/scala2-compat/i14223/lib/Lib.scala | 12 +++++++++++ sbt-test/scala2-compat/i14223/test | 1 + 5 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 sbt-test/scala2-compat/i14223/app/App.scala create mode 100644 sbt-test/scala2-compat/i14223/build.sbt create mode 100644 sbt-test/scala2-compat/i14223/lib/Lib.scala create mode 100644 sbt-test/scala2-compat/i14223/test diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 305a8a7510df..b5b8c4715ebc 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -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) @@ -997,7 +997,7 @@ 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() @@ -1005,14 +1005,14 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas 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 diff --git a/sbt-test/scala2-compat/i14223/app/App.scala b/sbt-test/scala2-compat/i14223/app/App.scala new file mode 100644 index 000000000000..dc229c0b1a86 --- /dev/null +++ b/sbt-test/scala2-compat/i14223/app/App.scala @@ -0,0 +1,6 @@ +import kc.Encoder + +def test: Unit = + val cellEncoder = new Encoder[String] { + override def encode: String = "" + } diff --git a/sbt-test/scala2-compat/i14223/build.sbt b/sbt-test/scala2-compat/i14223/build.sbt new file mode 100644 index 000000000000..433a5e8baddf --- /dev/null +++ b/sbt-test/scala2-compat/i14223/build.sbt @@ -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 + ) diff --git a/sbt-test/scala2-compat/i14223/lib/Lib.scala b/sbt-test/scala2-compat/i14223/lib/Lib.scala new file mode 100644 index 000000000000..5311bd22f313 --- /dev/null +++ b/sbt-test/scala2-compat/i14223/lib/Lib.scala @@ -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] = ??? +} diff --git a/sbt-test/scala2-compat/i14223/test b/sbt-test/scala2-compat/i14223/test new file mode 100644 index 000000000000..19aca297fdcf --- /dev/null +++ b/sbt-test/scala2-compat/i14223/test @@ -0,0 +1 @@ +> app/compile