diff options
author | Adriaan Moors <adriaan@lightbend.com> | 2017-01-10 16:33:37 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-10 16:33:37 -0800 |
commit | a5d38ea33430e144d05e7486791f70e144c5b602 (patch) | |
tree | 18b1458795b529dd1331b84231e8a8add0946fa3 | |
parent | 36967321c7a8a99cab2f9f1c4c0c46f09d3d34a6 (diff) | |
parent | 359b0bce8ec9bced0ca6fbb3865e9b3a6bcb30b4 (diff) | |
download | scala-a5d38ea33430e144d05e7486791f70e144c5b602.tar.gz scala-a5d38ea33430e144d05e7486791f70e144c5b602.tar.bz2 scala-a5d38ea33430e144d05e7486791f70e144c5b602.zip |
Merge pull request #5630 from adriaanm/rebase-5557
[backport] SI-10071 SI-8786 varargs methods
21 files changed, 343 insertions, 132 deletions
@@ -134,7 +134,7 @@ codebase and re-compiles too many files, resulting in long build times (check [sbt#1104](https://github.com/sbt/sbt/issues/1104) for progress on that front). In the meantime you can: - Enable "ant mode" in which sbt only re-compiles source files that were modified. - Create a file `local.sbt` containing the line `(incOptions in ThisBuild) := (incOptions in ThisBuild).value.withNameHashing(false).withAntStyle(true)`. + Create a file `local.sbt` containing the line `antStyle := true`. Add an entry `local.sbt` to your `~/.gitignore`. - Use IntelliJ IDEA for incremental compiles (see [IDE Setup](#ide-setup) below) - its incremental compiler is a bit less conservative, but usually correct. diff --git a/project/PartestUtil.scala b/project/PartestUtil.scala index 4b18c94b47..0bbbc3dc69 100644 --- a/project/PartestUtil.scala +++ b/project/PartestUtil.scala @@ -29,7 +29,7 @@ object PartestUtil { val knownUnaryOptions = List( "--pos", "--neg", "--run", "--jvm", "--res", "--ant", "--scalap", "--specialized", "--scalacheck", "--instrumented", "--presentation", "--failed", "--update-check", - "--show-diff", "--verbose", "--terse", "--debug", "--version", "--self-test", "--help") + "--show-diff", "--show-log", "--verbose", "--terse", "--debug", "--version", "--self-test", "--help") val srcPathOption = "--srcpath" val grepOption = "--grep" diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala index a04625c9c5..7a06c0cf2c 100644 --- a/src/compiler/scala/tools/nsc/transform/Erasure.scala +++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala @@ -336,7 +336,18 @@ abstract class Erasure extends AddInterfaces case MethodType(params, restpe) => val buf = new StringBuffer("(") - params foreach (p => buf append jsig(p.tpe)) + params foreach (p => { + val tp = p.attachments.get[TypeParamVarargsAttachment] match { + case Some(att) => + // For @varargs forwarders, a T* parameter has type Array[Object] in the forwarder + // instead of Array[T], as the latter would erase to Object (instead of Array[Object]). + // To make the generic signature correct ("[T", not "[Object"), an attachment on the + // parameter symbol stores the type T that was replaced by Object. + buf.append("["); att.typeParamRef + case _ => p.tpe + } + buf append jsig(tp) + }) buf append ")" buf append (if (restpe.typeSymbol == UnitClass || sym0.isConstructor) VOID_TAG.toString else jsig(restpe)) buf.toString diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index 870c35338c..d5a7213cfb 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -26,6 +26,8 @@ import scala.reflect.internal.util.ListOfNil * - for every repeated Scala parameter `x: T*' --> x: Seq[T]. * - for every repeated Java parameter `x: T...' --> x: Array[T], except: * if T is an unbounded abstract type, replace --> x: Array[Object] + * - for every method defining repeated parameters annotated with @varargs, generate + * a synthetic Java-style vararg method * - for every argument list that corresponds to a repeated Scala parameter * (a_1, ..., a_n) => (Seq(a_1, ..., a_n)) * - for every argument list that corresponds to a repeated Java parameter @@ -43,6 +45,8 @@ import scala.reflect.internal.util.ListOfNil * def liftedTry$1 = try { x_i } catch { .. } * meth(x_1, .., liftedTry$1(), .. ) * } + * - remove calls to elidable methods and replace their bodies with NOPs when elide-below + * requires it */ /*</export> */ abstract class UnCurry extends InfoTransform @@ -593,7 +597,13 @@ abstract class UnCurry extends InfoTransform case None => newRhs } ) - addJavaVarargsForwarders(dd, flatdd) + // Only class members can reasonably be called from Java due to name mangling. + // Additionally, the Uncurry info transformer only adds a forwarder symbol to class members, + // since the other symbols are not part of the ClassInfoType (see reflect.internal.transform.UnCurry) + if (dd.symbol.owner.isClass) + addJavaVarargsForwarders(dd, flatdd) + else + flatdd case tree: Try => if (tree.catches exists (cd => !treeInfo.isCatchCase(cd))) @@ -684,7 +694,7 @@ abstract class UnCurry extends InfoTransform // declared type and assign this to a synthetic val. Later, we'll patch // the method body to refer to this, rather than the parameter. val tempVal: ValDef = { - // SI-9442: using the "uncurry-erased" type (the one after the uncurry phase) can lead to incorrect + // SI-9442: using the "uncurry-erased" type (the one after the uncurry phase) can lead to incorrect // tree transformations. For example, compiling: // ``` // def foo(c: Ctx)(l: c.Tree): Unit = { @@ -713,7 +723,7 @@ abstract class UnCurry extends InfoTransform // to redo this desugaring manually here // 2. the type needs to be normalized, since `gen.mkCast` checks this (no HK here, just aliases have // to be expanded before handing the type to `gen.mkAttributedCast`, which calls `gen.mkCast`) - val info0 = + val info0 = enteringUncurry(p.symbol.info) match { case DesugaredParameterType(desugaredTpe) => desugaredTpe @@ -755,80 +765,59 @@ abstract class UnCurry extends InfoTransform if (!hasRepeated) reporter.error(dd.symbol.pos, "A method without repeated parameters cannot be annotated with the `varargs` annotation.") } - /* Called during post transform, after the method argument lists have been flattened. - * It looks for the method in the `repeatedParams` map, and generates a Java-style + /** + * Called during post transform, after the method argument lists have been flattened. + * It looks for the forwarder symbol in the symbol attachments and generates a Java-style * varargs forwarder. + * + * @note The Java-style varargs method symbol is generated in the Uncurry info transformer. If the + * symbol can't be found this method reports a warning and carries on. + * @see [[scala.reflect.internal.transform.UnCurry]] */ private def addJavaVarargsForwarders(dd: DefDef, flatdd: DefDef): DefDef = { if (!dd.symbol.hasAnnotation(VarargsClass) || !enteringUncurry(mexists(dd.symbol.paramss)(sym => definitions.isRepeatedParamType(sym.tpe)))) return flatdd - def toArrayType(tp: Type): Type = { - val arg = elementType(SeqClass, tp) - // to prevent generation of an `Object` parameter from `Array[T]` parameter later - // as this would crash the Java compiler which expects an `Object[]` array for varargs - // e.g. def foo[T](a: Int, b: T*) - // becomes def foo[T](a: Int, b: Array[Object]) - // instead of def foo[T](a: Int, b: Array[T]) ===> def foo[T](a: Int, b: Object) - arrayType( - if (arg.typeSymbol.isTypeParameterOrSkolem) ObjectTpe - else arg - ) + val forwSym: Symbol = { + currentClass.info // make sure the info is up to date, so the varargs forwarder symbol has been generated + flatdd.symbol.attachments.get[VarargsSymbolAttachment] match { + case Some(VarargsSymbolAttachment(sym)) => sym + case None => + reporter.warning(dd.pos, s"Could not generate Java varargs forwarder for ${flatdd.symbol}. Please file a bug.") + return flatdd + } } - val theTyper = typer.atOwner(dd, currentClass) - val flatparams = flatdd.symbol.paramss.head + val newPs = forwSym.tpe.params val isRepeated = enteringUncurry(dd.symbol.info.paramss.flatten.map(sym => definitions.isRepeatedParamType(sym.tpe))) + val oldPs = flatdd.symbol.paramss.head - // create the type - val forwformals = map2(flatparams, isRepeated) { - case (p, true) => toArrayType(p.tpe) - case (p, false)=> p.tpe - } - val forwresult = dd.symbol.tpe_*.finalResultType - val forwformsyms = map2(forwformals, flatparams)((tp, oldparam) => - currentClass.newValueParameter(oldparam.name.toTermName, oldparam.pos).setInfo(tp) - ) - def mono = MethodType(forwformsyms, forwresult) - val forwtype = dd.symbol.tpe match { - case MethodType(_, _) => mono - case PolyType(tps, _) => PolyType(tps, mono) - } - - // create the symbol - val forwsym = currentClass.newMethod(dd.name.toTermName, dd.pos, VARARGS | SYNTHETIC | flatdd.symbol.flags) setInfo forwtype - def forwParams = forwsym.info.paramss.flatten - - // create the tree - val forwtree = theTyper.typedPos(dd.pos) { - val locals = map3(forwParams, flatparams, isRepeated) { - case (_, fp, false) => null - case (argsym, fp, true) => - Block(Nil, - gen.mkCast( - gen.mkWrapArray(Ident(argsym), elementType(ArrayClass, argsym.tpe)), - seqType(elementType(SeqClass, fp.tpe)) - ) - ) - } - val seqargs = map2(locals, forwParams) { - case (null, argsym) => Ident(argsym) - case (l, _) => l - } - val end = if (forwsym.isConstructor) List(UNIT) else Nil + val theTyper = typer.atOwner(dd, currentClass) + val forwTree = theTyper.typedPos(dd.pos) { + val seqArgs = map3(newPs, oldPs, isRepeated)((param, oldParam, isRep) => { + if (!isRep) Ident(param) + else { + val parTp = elementType(ArrayClass, param.tpe) + val wrap = gen.mkWrapArray(Ident(param), parTp) + param.attachments.get[TypeParamVarargsAttachment] match { + case Some(TypeParamVarargsAttachment(tp)) => gen.mkCast(wrap, seqType(tp)) + case _ => wrap + } + } + }) - DefDef(forwsym, BLOCK(Apply(gen.mkAttributedRef(flatdd.symbol), seqargs) :: end : _*)) + val forwCall = Apply(gen.mkAttributedRef(flatdd.symbol), seqArgs) + DefDef(forwSym, if (forwSym.isConstructor) Block(List(forwCall), UNIT) else forwCall) } // check if the method with that name and those arguments already exists in the template - currentClass.info.member(forwsym.name).alternatives.find(s => s != forwsym && s.tpe.matches(forwsym.tpe)) match { - case Some(s) => reporter.error(dd.symbol.pos, - "A method with a varargs annotation produces a forwarder method with the same signature " - + s.tpe + " as an existing method.") + enteringUncurry(currentClass.info.member(forwSym.name).alternatives.find(s => s != forwSym && s.tpe.matches(forwSym.tpe))) match { + case Some(s) => + reporter.error(dd.symbol.pos, + s"A method with a varargs annotation produces a forwarder method with the same signature ${s.tpe} as an existing method.") case None => // enter symbol into scope - currentClass.info.decls enter forwsym - addNewMember(forwtree) + addNewMember(forwTree) } flatdd diff --git a/src/reflect/scala/reflect/internal/StdAttachments.scala b/src/reflect/scala/reflect/internal/StdAttachments.scala index 3df31b538c..cddb0c8f72 100644 --- a/src/reflect/scala/reflect/internal/StdAttachments.scala +++ b/src/reflect/scala/reflect/internal/StdAttachments.scala @@ -58,4 +58,7 @@ trait StdAttachments { * error to indicate that the earlier observation was incomplete. */ case object KnownDirectSubclassesCalled extends PlainAttachment + + /** An attachment carrying information between uncurry and erasure */ + case class TypeParamVarargsAttachment(val typeParamRef: Type) } diff --git a/src/reflect/scala/reflect/internal/transform/UnCurry.scala b/src/reflect/scala/reflect/internal/transform/UnCurry.scala index 85e3ac60e8..c22ff71f8b 100644 --- a/src/reflect/scala/reflect/internal/transform/UnCurry.scala +++ b/src/reflect/scala/reflect/internal/transform/UnCurry.scala @@ -4,6 +4,7 @@ package internal package transform import Flags._ +import scala.collection.mutable trait UnCurry { @@ -11,6 +12,12 @@ trait UnCurry { import global._ import definitions._ + /** + * The synthetic Java vararg method symbol corresponding to a Scala vararg method + * annotated with @varargs. + */ + case class VarargsSymbolAttachment(varargMethod: Symbol) + /** Note: changing tp.normalize to tp.dealias in this method leads to a single * test failure: run/t5688.scala, where instead of the expected output * Vector(ta, tb, tab) @@ -65,18 +72,74 @@ trait UnCurry { def apply(tp0: Type): Type = { val tp = expandAlias(tp0) tp match { - case ClassInfoType(parents, decls, clazz) => + case ClassInfoType(parents, decls, clazz) if !clazz.isJavaDefined => val parents1 = parents mapConserve uncurry - if (parents1 eq parents) tp - else ClassInfoType(parents1, decls, clazz) // @MAT normalize in decls?? + val varargOverloads = mutable.ListBuffer.empty[Symbol] + + // Not using `hasAnnotation` here because of dreaded cyclic reference errors: + // it may happen that VarargsClass has not been initialized yet and we get here + // while processing one of its superclasses (such as java.lang.Object). Since we + // don't need the more precise `matches` semantics, we only check the symbol, which + // is anyway faster and safer + for (decl <- decls if decl.annotations.exists(_.symbol == VarargsClass)) { + if (mexists(decl.paramss)(sym => definitions.isRepeatedParamType(sym.tpe))) { + varargOverloads += varargForwarderSym(clazz, decl, exitingPhase(phase)(decl.info)) + } + } + if ((parents1 eq parents) && varargOverloads.isEmpty) tp + else { + val newDecls = decls.cloneScope + varargOverloads.foreach(newDecls.enter) + ClassInfoType(parents1, newDecls, clazz) + } // @MAT normalize in decls?? + case PolyType(_, _) => mapOver(tp) + case _ => tp } } } + private def varargForwarderSym(currentClass: Symbol, origSym: Symbol, newInfo: Type): Symbol = { + val forwSym = origSym.cloneSymbol(currentClass, VARARGS | SYNTHETIC | origSym.flags & ~DEFERRED, origSym.name.toTermName).withoutAnnotations + + // we are using `origSym.info`, which contains the type *before* the transformation + // so we still see repeated parameter types (uncurry replaces them with Seq) + val isRepeated = origSym.info.paramss.flatten.map(sym => definitions.isRepeatedParamType(sym.tpe)) + val oldPs = newInfo.paramss.head + def toArrayType(tp: Type, newParam: Symbol): Type = { + val arg = elementType(SeqClass, tp) + val elem = if (arg.typeSymbol.isTypeParameterOrSkolem && !(arg <:< AnyRefTpe)) { + // To prevent generation of an `Object` parameter from `Array[T]` parameter later + // as this would crash the Java compiler which expects an `Object[]` array for varargs + // e.g. def foo[T](a: Int, b: T*) + // becomes def foo[T](a: Int, b: Array[Object]) + // instead of def foo[T](a: Int, b: Array[T]) ===> def foo[T](a: Int, b: Object) + // + // In order for the forwarder method to type check we need to insert a cast: + // def foo'[T'](a: Int, b: Array[Object]) = foo[T'](a, wrapRefArray(b).asInstanceOf[Seq[T']]) + // The target element type for that cast (T') is stored in the TypeParamVarargsAttachment +// val originalArg = arg.substSym(oldTps, tps) + // Store the type parameter that was replaced by Object to emit the correct generic signature + newParam.updateAttachment(new TypeParamVarargsAttachment(arg)) + ObjectTpe + } else + arg + arrayType(elem) + } + + foreach2(forwSym.paramss.flatten, isRepeated)((p, isRep) => + if (isRep) { + p.setInfo(toArrayType(p.info, p)) + } + ) + + origSym.updateAttachment(VarargsSymbolAttachment(forwSym)) + forwSym + } + /** - return symbol's transformed type, * - if symbol is a def parameter with transformed type T, return () => T * diff --git a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala index 8481cd8996..45dd550e3e 100644 --- a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala +++ b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala @@ -42,6 +42,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse => this.SyntheticUnitAttachment this.SubpatternsAttachment this.KnownDirectSubclassesCalled + this.TypeParamVarargsAttachment this.noPrint this.typeDebug this.Range @@ -444,6 +445,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse => definitions.ScalaValueClassesNoUnit definitions.ScalaValueClasses + uncurry.VarargsSymbolAttachment uncurry.DesugaredParameterType erasure.GenericArray erasure.scalaErasure diff --git a/test/files/jvm/t8786-sig.scala b/test/files/jvm/t8786-sig.scala new file mode 100644 index 0000000000..f22e400528 --- /dev/null +++ b/test/files/jvm/t8786-sig.scala @@ -0,0 +1,126 @@ +class A[U] { + @annotation.varargs def m1[T] (a: T*): T = a.head + @annotation.varargs def m2[T <: AnyRef](a: T*): T = a.head + @annotation.varargs def m3[T <: AnyVal](a: T*): T = a.head + @annotation.varargs def m4[T <: Int] (a: T*): T = a.head + @annotation.varargs def m5[T <: String](a: T*): T = a.head + @annotation.varargs def m6 (a: String*): String = a.head + @annotation.varargs def m7 (a: Int*): Int = a.head + @annotation.varargs def m8 (a: U*): U = a.head + + def n1[T] (a: Array[T]): T = a(0) + def n2[T <: AnyRef](a: Array[T]): T = a(0) + def n3[T <: AnyVal](a: Array[T]): T = a(0) + def n4[T <: Int] (a: Array[T]): T = a(0) + def n5[T <: String](a: Array[T]): T = a(0) + def n6 (a: Array[String]): String = a(0) + def n7 (a: Array[Int]): Int = a(0) + def n8 (a: Array[U]): U = a(0) +} + +object Test extends App { + val a = classOf[A[_]] + + def sig (method: String, tp: Class[_]) = a.getDeclaredMethod(method, tp).toString + def genSig(method: String, tp: Class[_]) = a.getDeclaredMethod(method, tp).toGenericString + def isVarArgs(method: String, tp: Class[_]) = a.getDeclaredMethod(method, tp).isVarArgs + def bound (method: String, tp: Class[_]) = { + val m = a.getDeclaredMethod(method, tp) + m.getGenericParameterTypes.apply(0) match { + case _: Class[_] => "" + case gat: java.lang.reflect.GenericArrayType => + val compTp = gat.getGenericComponentType.asInstanceOf[java.lang.reflect.TypeVariable[_]] + compTp.getBounds.apply(0).toString + } + } + + def check(found: String, expected: String): Unit = + assert(found == expected, s"found: $found\nexpected: $expected") + + def checkVarArgs(method: String, tp: Class[_])(expected: String): Unit = { + assert(isVarArgs(method, tp), s"expected varargs for $method") + val found = genSig(method, tp) + + def varArgsToBraces(sig: String) = sig.replaceAll("""\.\.\.""","[]") + // Normalize sigs so that the tests works on Java 6 (where varargs are printed as []) + // and above (where vargs are pretty printed using ...) + assert(varArgsToBraces(found) == varArgsToBraces(expected), s"found: $found\nexpected: $expected (modulo `...` or `[]` as varargs suffix)") + } + + val sq = classOf[Seq[_]] + val ob = classOf[Object] + val ao = classOf[Array[Object]] + val as = classOf[Array[String]] + val ai = classOf[Array[Int]] + + check(sig("m1", sq) , "public java.lang.Object A.m1(scala.collection.Seq)") + check(sig("m2", sq) , "public java.lang.Object A.m2(scala.collection.Seq)") + check(sig("m3", sq) , "public java.lang.Object A.m3(scala.collection.Seq)") + check(sig("m4", sq) , "public int A.m4(scala.collection.Seq)") + check(sig("m5", sq) , "public java.lang.String A.m5(scala.collection.Seq)") + check(sig("m6", sq) , "public java.lang.String A.m6(scala.collection.Seq)") + check(sig("m7", sq) , "public int A.m7(scala.collection.Seq)") + check(sig("m8", sq) , "public java.lang.Object A.m8(scala.collection.Seq)") + + check(genSig("m1", sq), "public <T> T A.m1(scala.collection.Seq<T>)") + check(genSig("m2", sq), "public <T> T A.m2(scala.collection.Seq<T>)") + check(genSig("m3", sq), "public <T> T A.m3(scala.collection.Seq<T>)") + // TODO: the signature for is wrong for T <: Int, SI-9846. The signature should be + // `public int A.m4(scala.collection.Seq<java.lang.Object>)`. This is testing the status quo. + check(genSig("m4", sq), "public <T> T A.m4(scala.collection.Seq<T>)") + check(genSig("m5", sq), "public <T> T A.m5(scala.collection.Seq<T>)") + check(genSig("m6", sq), "public java.lang.String A.m6(scala.collection.Seq<java.lang.String>)") + check(genSig("m7", sq), "public int A.m7(scala.collection.Seq<java.lang.Object>)") + check(genSig("m8", sq), "public U A.m8(scala.collection.Seq<U>)") + + + // varargs forwarder + + check(sig("m1", ao) , "public java.lang.Object A.m1(java.lang.Object[])") + check(sig("m2", ao) , "public java.lang.Object A.m2(java.lang.Object[])") + check(sig("m3", ao) , "public java.lang.Object A.m3(java.lang.Object[])") + check(sig("m4", ao) , "public int A.m4(java.lang.Object[])") + check(sig("m5", as) , "public java.lang.String A.m5(java.lang.String[])") + check(sig("m6", as) , "public java.lang.String A.m6(java.lang.String[])") + check(sig("m7", ai) , "public int A.m7(int[])") + check(sig("m8", ao) , "public java.lang.Object A.m8(java.lang.Object[])") + + checkVarArgs("m1", ao)("public <T> T A.m1(T...)") + checkVarArgs("m2", ao)("public <T> T A.m2(T...)") + checkVarArgs("m3", ao)("public <T> T A.m3(T...)") + // testing status quo: signature is wrong for T <: Int, SI-9846 + checkVarArgs("m4", ao)("public <T> T A.m4(T...)") + checkVarArgs("m5", as)("public <T> T A.m5(T...)") + checkVarArgs("m6", as)("public java.lang.String A.m6(java.lang.String...)") + checkVarArgs("m7", ai)("public int A.m7(int...)") + checkVarArgs("m8", ao)("public U A.m8(U...)") + + check(bound("m1", ao) , "class java.lang.Object") + check(bound("m2", ao) , "class java.lang.Object") + check(bound("m3", ao) , "class java.lang.Object") + check(bound("m4", ao) , "class java.lang.Object") + check(bound("m5", as) , "class java.lang.String") + check(bound("m6", as) , "") + check(bound("m7", ai) , "") + check(bound("m8", ao) , "class java.lang.Object") + + + check(sig("n1", ob) , "public java.lang.Object A.n1(java.lang.Object)") + check(sig("n2", ao) , "public java.lang.Object A.n2(java.lang.Object[])") + check(sig("n3", ob) , "public java.lang.Object A.n3(java.lang.Object)") + check(sig("n4", ob) , "public int A.n4(java.lang.Object)") + check(sig("n5", as) , "public java.lang.String A.n5(java.lang.String[])") + check(sig("n6", as) , "public java.lang.String A.n6(java.lang.String[])") + check(sig("n7", ai) , "public int A.n7(int[])") + check(sig("n8", ob) , "public java.lang.Object A.n8(java.lang.Object)") + + check(genSig("n1", ob), "public <T> T A.n1(java.lang.Object)") + check(genSig("n2", ao), "public <T> T A.n2(T[])") + check(genSig("n3", ob), "public <T> T A.n3(java.lang.Object)") + // testing status quo: signature is wrong for T <: Int, SI-9846 + check(genSig("n4", ob), "public <T> T A.n4(java.lang.Object)") + check(genSig("n5", as), "public <T> T A.n5(T[])") + check(genSig("n6", as), "public java.lang.String A.n6(java.lang.String[])") + check(genSig("n7", ai), "public int A.n7(int[])") + check(genSig("n8", ob), "public U A.n8(java.lang.Object)") +} diff --git a/test/files/jvm/t8786/A_1.scala b/test/files/jvm/t8786/A_1.scala new file mode 100644 index 0000000000..13c0ad191d --- /dev/null +++ b/test/files/jvm/t8786/A_1.scala @@ -0,0 +1,3 @@ +class A { + @annotation.varargs def foo[T](a: Int, b: T*): T = b.head +} diff --git a/test/files/jvm/t8786/B_2.java b/test/files/jvm/t8786/B_2.java new file mode 100644 index 0000000000..dc155a290f --- /dev/null +++ b/test/files/jvm/t8786/B_2.java @@ -0,0 +1,22 @@ +public class B_2 { + private static int res = 0; + + public static void m(char a[]) { res += 10; } + public static void m(String a) { res += 100; } + public static void m(Object a) { res += 1000; } + + public static <T> T foo(int a, T... b) { return b[0]; } + + public static <T> T bar(T b[]) { return b[0]; } + + public static void main(String[] args) { + m(foo(15, "a", "b", "c")); + if (res != 100) + throw new Error("bad: "+ res); + + A a = new A(); + m(a.foo(16, "a", "b", "c")); + if (res != 200) + throw new Error("bad: " + res); + } +} diff --git a/test/files/jvm/t8786/Test_2.scala b/test/files/jvm/t8786/Test_2.scala new file mode 100644 index 0000000000..76ccb4c3ed --- /dev/null +++ b/test/files/jvm/t8786/Test_2.scala @@ -0,0 +1,3 @@ +object Test extends App { + B_2.main(null) +} diff --git a/test/files/jvm/varargs-separate-bytecode.check b/test/files/jvm/varargs-separate-bytecode.check new file mode 100644 index 0000000000..1507cd48c5 --- /dev/null +++ b/test/files/jvm/varargs-separate-bytecode.check @@ -0,0 +1 @@ +Found vararg overload for method create
\ No newline at end of file diff --git a/test/files/jvm/varargs-separate-bytecode/AbstractProps_1.scala b/test/files/jvm/varargs-separate-bytecode/AbstractProps_1.scala new file mode 100644 index 0000000000..5dfb8d1a9e --- /dev/null +++ b/test/files/jvm/varargs-separate-bytecode/AbstractProps_1.scala @@ -0,0 +1,8 @@ +package foo + +import scala.annotation.varargs + +trait AbstractProps { + @varargs + def create(x: String, y: Int*): AbstractProps = null +} diff --git a/test/files/jvm/varargs-separate-bytecode/Props_2.scala b/test/files/jvm/varargs-separate-bytecode/Props_2.scala new file mode 100644 index 0000000000..3fc09586fc --- /dev/null +++ b/test/files/jvm/varargs-separate-bytecode/Props_2.scala @@ -0,0 +1,3 @@ +import foo.AbstractProps + +class Props extends AbstractProps
\ No newline at end of file diff --git a/test/files/jvm/varargs-separate-bytecode/Test.scala b/test/files/jvm/varargs-separate-bytecode/Test.scala new file mode 100644 index 0000000000..a666de7f39 --- /dev/null +++ b/test/files/jvm/varargs-separate-bytecode/Test.scala @@ -0,0 +1,15 @@ +import scala.collection.JavaConverters._ +import scala.tools.asm +import scala.tools.asm.Opcodes +import scala.tools.partest.BytecodeTest + +object Test extends BytecodeTest { + def show: Unit = { + val classNode = loadClassNode("Props") + val methods = classNode.methods.iterator().asScala.filter( m => m.name == "create") + + for (m <- methods if (m.access & Opcodes.ACC_VARARGS) > 0) { + println(s"Found vararg overload for method ${m.name}") + } + } +} diff --git a/test/files/jvm/varargs/JavaClass.java b/test/files/jvm/varargs/JavaClass.java index 6928ee5adc..0cc3587c5e 100644 --- a/test/files/jvm/varargs/JavaClass.java +++ b/test/files/jvm/varargs/JavaClass.java @@ -1,16 +1,12 @@ - - - public class JavaClass { - public static <T> void varargz(int i, T... v) { - } - - public static void callSomeAnnotations() { - VaClass va = new VaClass(); - va.vs(4, "", "", ""); - va.vi(1, 2, 3, 4); - varargz(5, 1.0, 2.0, 3.0); - va.vt(16, "", "", ""); - System.out.println(va.vt1(16, "a", "b", "c")); - } -}
\ No newline at end of file + public static <T> void varargz(int i, T... v) { } + + public static void callSomeAnnotations() { + VaClass va = new VaClass(); + va.vs(4, "", "", ""); + va.vi(1, 2, 3, 4); + varargz(5, 1.0, 2.0, 3.0); + va.vt(16, "", "", ""); + System.out.println(va.vt1(16, "a", "b", "c")); + } +} diff --git a/test/files/jvm/varargs/VaClass.scala b/test/files/jvm/varargs/VaClass.scala index d83e63ace1..ee8c288a16 100644 --- a/test/files/jvm/varargs/VaClass.scala +++ b/test/files/jvm/varargs/VaClass.scala @@ -1,15 +1,8 @@ - - import annotation.varargs - - class VaClass { - @varargs def vs(a: Int, b: String*) = println(a + b.length) @varargs def vi(a: Int, b: Int*) = println(a + b.sum) @varargs def vt[T](a: Int, b: T*) = println(a + b.length) - - // TODO remove type bound after fixing SI-8786, see also https://github.com/scala/scala/pull/3961 - @varargs def vt1[T <: String](a: Int, b: T*): T = b.head + @varargs def vt1[T](a: Int, b: T*): T = b.head } diff --git a/test/files/jvm/varargs/varargs.scala b/test/files/jvm/varargs/varargs.scala index 6d2e707bdf..b09818f46f 100644 --- a/test/files/jvm/varargs/varargs.scala +++ b/test/files/jvm/varargs/varargs.scala @@ -1,21 +1,5 @@ - - - - - - object Test { def main(args: Array[String]) { JavaClass.callSomeAnnotations } } - - - - - - - - - - diff --git a/test/files/run/reflection-mem-typecheck.scala b/test/files/run/reflection-mem-typecheck.scala deleted file mode 100644 index f1fe983ede..0000000000 --- a/test/files/run/reflection-mem-typecheck.scala +++ /dev/null @@ -1,26 +0,0 @@ -import scala.tools.partest.MemoryTest - -trait A { type T <: A } -trait B { type T <: B } - -object Test extends MemoryTest { - lazy val tb = { - import scala.reflect.runtime.universe._ - import scala.reflect.runtime.{currentMirror => cm} - import scala.tools.reflect.ToolBox - cm.mkToolBox() - } - - override def maxDelta = 12 - override def calcsPerIter = 8 - override def calc() { - var snippet = """ - trait A { type T <: A } - trait B { type T <: B } - def foo[T](x: List[T]) = x - foo(List(new A {}, new B {})) - """.trim - snippet = snippet + "\n" + (List.fill(50)(snippet.split("\n").last) mkString "\n") - tb.typecheck(tb.parse(snippet)) - } -}
\ No newline at end of file diff --git a/test/files/run/t5125b.check b/test/files/run/t5125b.check index ddbf908f04..29b438a2d6 100644 --- a/test/files/run/t5125b.check +++ b/test/files/run/t5125b.check @@ -5,3 +5,6 @@ public void C2.f(scala.collection.Seq) public void C2$C3.f(java.lang.String[]) public void C2$C3.f(scala.collection.Seq) public void C4.f(scala.collection.Seq) +private void C5.f(int,int[]) +private void C5.f(int,scala.collection.Seq) +public void C5.f(scala.collection.Seq) diff --git a/test/files/run/t5125b.scala b/test/files/run/t5125b.scala index 149c49e213..60ab1d9792 100644 --- a/test/files/run/t5125b.scala +++ b/test/files/run/t5125b.scala @@ -23,6 +23,17 @@ class C4 { } } +class C5 { + def f(values: String*) = println("Calling C5.f(): " + values) + @scala.annotation.varargs + private def f(v: Int, values: Int*) = println("Calling C5.f(): " + values) + + def method(): Unit = { + @scala.annotation.varargs + def f(values: String*) = println("Calling C5.<locally>.f(): " + values) + } +} + object Test extends App { def check(c: Class[_]) { val methodName = "f" @@ -34,4 +45,5 @@ object Test extends App { check(classOf[C2]) check(classOf[C2#C3]) check(classOf[C4]) + check(classOf[C5]) } |