summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/transform/UnCurry.scala28
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Duplicators.scala28
-rw-r--r--test/files/pos/t4717.scala35
-rw-r--r--test/files/run/t5125.check4
-rw-r--r--test/files/run/t5125.scala24
-rw-r--r--test/files/run/t5125b.check7
-rw-r--r--test/files/run/t5125b.scala37
-rw-r--r--test/pending/run/t3899.check4
-rw-r--r--test/pending/run/t3899/Base_1.java5
-rw-r--r--test/pending/run/t3899/Derived_2.scala30
10 files changed, 179 insertions, 23 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala
index f01cbade18..8af12f3f10 100644
--- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala
+++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala
@@ -77,9 +77,17 @@ abstract class UnCurry extends InfoTransform
private var inConstructorFlag = 0L
private val byNameArgs = mutable.HashSet[Tree]()
private val noApply = mutable.HashSet[Tree]()
- private val newMembers = mutable.ArrayBuffer[Tree]()
+ private val newMembers = mutable.Map[Symbol, mutable.Buffer[Tree]]()
private val repeatedParams = mutable.Map[Symbol, List[ValDef]]()
+ /** Add a new synthetic member for `currentOwner` */
+ private def addNewMember(t: Tree): Unit =
+ newMembers.getOrElseUpdate(currentOwner, mutable.Buffer()) += t
+
+ /** Process synthetic members for `owner`. They are removed form the `newMembers` as a side-effect. */
+ @inline private def useNewMembers[T](owner: Symbol)(f: List[Tree] => T): T =
+ f(newMembers.remove(owner).getOrElse(Nil).toList)
+
@inline private def withInPattern[T](value: Boolean)(body: => T): T = {
inPattern = value
try body
@@ -573,7 +581,7 @@ abstract class UnCurry extends InfoTransform
val vparamssNoRhs = dd.vparamss mapConserve (_ mapConserve {p =>
treeCopy.ValDef(p, p.mods, p.name, p.tpt, EmptyTree)
})
-
+
if (dd.symbol hasAnnotation VarargsClass) saveRepeatedParams(dd)
withNeedLift(false) {
@@ -680,9 +688,8 @@ abstract class UnCurry extends InfoTransform
tree match {
/* Some uncurry post transformations add members to templates.
- * When inside a template, the following sequence is available:
- * - newMembers
- * Any entry in this sequence will be added into the template
+ *
+ * Members registered by `addMembers` for the current template are added
* once the template transformation has finished.
*
* In particular, this case will add:
@@ -690,8 +697,10 @@ abstract class UnCurry extends InfoTransform
*/
case Template(_, _, _) =>
localTyper = typer.atOwner(tree, currentClass)
- try deriveTemplate(tree)(transformTrees(newMembers.toList) ::: _)
- finally newMembers.clear()
+ useNewMembers(currentClass) {
+ newMembers =>
+ deriveTemplate(tree)(transformTrees(newMembers) ::: _)
+ }
case dd @ DefDef(_, _, _, vparamss0, _, rhs0) =>
val flatdd = copyDefDef(dd)(
@@ -763,7 +772,7 @@ abstract class UnCurry extends InfoTransform
/* 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
- * varargs forwarder. It then adds the forwarder to the `newMembers` sequence.
+ * varargs forwarder.
*/
private def addJavaVarargsForwarders(dd: DefDef, flatdd: DefDef): DefDef = {
if (!dd.symbol.hasAnnotation(VarargsClass) || !repeatedParams.contains(dd.symbol))
@@ -840,8 +849,7 @@ abstract class UnCurry extends InfoTransform
case None =>
// enter symbol into scope
currentClass.info.decls enter forwsym
- // add the method to `newMembers`
- newMembers += forwtree
+ addNewMember(forwtree)
}
flatdd
diff --git a/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala b/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala
index 2574a1d241..b7a6ea677e 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Duplicators.scala
@@ -143,8 +143,8 @@ abstract class Duplicators extends Analyzer {
else
sym
- private def invalidate(tree: Tree) {
- debuglog("attempting to invalidate " + tree.symbol + ", owner - " + (if (tree.symbol ne null) tree.symbol.owner else "<NULL>"))
+ private def invalidate(tree: Tree, owner: Symbol = NoSymbol) {
+ debuglog("attempting to invalidate " + tree.symbol)
if (tree.isDef && tree.symbol != NoSymbol) {
debuglog("invalid " + tree.symbol)
invalidSyms(tree.symbol) = tree
@@ -158,18 +158,20 @@ abstract class Duplicators extends Analyzer {
newsym.setInfo(fixType(ldef.symbol.info))
ldef.symbol = newsym
debuglog("newsym: " + newsym + " info: " + newsym.info)
-
+
case vdef @ ValDef(mods, name, _, rhs) if mods.hasFlag(Flags.LAZY) =>
debuglog("ValDef " + name + " sym.info: " + vdef.symbol.info)
invalidSyms(vdef.symbol) = vdef
- val newsym = vdef.symbol.cloneSymbol(context.owner)
+ val newowner = if (owner != NoSymbol) owner else context.owner
+ val newsym = vdef.symbol.cloneSymbol(newowner)
newsym.setInfo(fixType(vdef.symbol.info))
vdef.symbol = newsym
- debuglog("newsym: " + newsym + " info: " + newsym.info)
-
+ debuglog("newsym: " + newsym + " info: " + newsym.info + ", owner: " + newsym.owner + ", " + newsym.owner.isClass)
+ if (newsym.owner.isClass) newsym.owner.info.decls enter newsym
+
case DefDef(_, name, tparams, vparamss, _, rhs) =>
// invalidate parameters
- invalidate(tparams ::: vparamss.flatten)
+ invalidateAll(tparams ::: vparamss.flatten)
tree.symbol = NoSymbol
case _ =>
@@ -178,14 +180,14 @@ abstract class Duplicators extends Analyzer {
}
}
- private def invalidate(stats: List[Tree]) {
- stats foreach invalidate
+ private def invalidateAll(stats: List[Tree], owner: Symbol = NoSymbol) {
+ stats.foreach(invalidate(_, owner))
}
def retypedMethod(ddef: DefDef, oldThis: Symbol, newThis: Symbol): Tree = {
oldClassOwner = oldThis
newClassOwner = newThis
- invalidate(ddef.tparams)
+ invalidateAll(ddef.tparams)
mforeach(ddef.vparamss) { vdef =>
invalidate(vdef)
vdef.tpe = null
@@ -239,15 +241,15 @@ abstract class Duplicators extends Analyzer {
case Block(stats, res) =>
debuglog("invalidating block")
- invalidate(stats)
+ invalidateAll(stats)
invalidate(res)
tree.tpe = null
super.typed(tree, mode, pt)
case ClassDef(_, _, _, tmpl @ Template(parents, _, stats)) =>
- // log("invalidating classdef " + tree.tpe)
+ // log("invalidating classdef " + tree)
tmpl.symbol = tree.symbol.newLocalDummy(tree.pos)
- invalidate(stats)
+ invalidateAll(stats, tree.symbol)
tree.tpe = null
super.typed(tree, mode, pt)
diff --git a/test/files/pos/t4717.scala b/test/files/pos/t4717.scala
new file mode 100644
index 0000000000..4acfe489cc
--- /dev/null
+++ b/test/files/pos/t4717.scala
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+trait Bug1[@specialized(Boolean) A] extends TraversableOnce[A] {
+
+ def ++[B >: A](that: TraversableOnce[B]): Iterator[B] = new Iterator[B] {
+ lazy val it = that.toIterator
+ def hasNext = it.hasNext
+ def next = it.next
+ }
+
+}
+
+
+
+trait WorksFine[@specialized(Boolean) A] {
+ class SubBounds[B >: A] extends Bounds[B] {
+ lazy val it = ???
+ }
+ def x[B >: A]: Unit = new SubBounds[B]
+}
+
+
+trait Bounds[@specialized(Boolean) A] {
+ // okay without `>: A`
+ def x[B >: A]: Unit = new Bounds[B] {
+ lazy val it = ??? // def or val okay
+ }
+}
+
+
diff --git a/test/files/run/t5125.check b/test/files/run/t5125.check
new file mode 100644
index 0000000000..d8a0565005
--- /dev/null
+++ b/test/files/run/t5125.check
@@ -0,0 +1,4 @@
+public void O1$.f(java.lang.String[])
+public void O1$.f(scala.collection.Seq)
+public void O2$.f(java.lang.String[])
+public void O2$.f(scala.collection.Seq)
diff --git a/test/files/run/t5125.scala b/test/files/run/t5125.scala
new file mode 100644
index 0000000000..7ec2b929d9
--- /dev/null
+++ b/test/files/run/t5125.scala
@@ -0,0 +1,24 @@
+object O1 {
+ def instance = this
+ @scala.annotation.varargs
+ def f(values:String*) = println("Calling O1.f(): " + values)
+}
+
+object O2 {
+ def instance = this
+ @scala.annotation.varargs
+ def f(values:String*) = println("Calling O2.f(): " + values)
+ // uncommenting g() results in errors in A.java
+ def g(): String => Int = s => s.hashCode
+}
+
+object Test extends App {
+ def check(c: Class[_]) {
+ val methodName = "f"
+ val methods = c.getDeclaredMethods.filter(_.getName == methodName)
+ println(methods.map(_.toString).sorted.mkString("\n"))
+ }
+
+ check(O1.getClass)
+ check(O2.getClass)
+} \ No newline at end of file
diff --git a/test/files/run/t5125b.check b/test/files/run/t5125b.check
new file mode 100644
index 0000000000..ddbf908f04
--- /dev/null
+++ b/test/files/run/t5125b.check
@@ -0,0 +1,7 @@
+public void C1.f(java.lang.String[])
+public void C1.f(scala.collection.Seq)
+public void C2.f(java.lang.String[])
+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)
diff --git a/test/files/run/t5125b.scala b/test/files/run/t5125b.scala
new file mode 100644
index 0000000000..29c08fee4c
--- /dev/null
+++ b/test/files/run/t5125b.scala
@@ -0,0 +1,37 @@
+class C1 {
+ @scala.annotation.varargs
+ def f(values:String*) = println("Calling C1.f(): " + values)
+}
+
+class C2 {
+ @scala.annotation.varargs
+ def f(values:String*) = println("Calling C2.f(): " + values)
+ def g(): String => Int = s => s.hashCode
+
+ class C3 {
+ @scala.annotation.varargs
+ def f(values:String*) = println("Calling C3.f(): " + values)
+ }
+}
+
+class C4 {
+ def f(values: String*) = println("Calling C4.f(): " + values)
+
+ locally {
+ @scala.annotation.varargs
+ def f(values: String*) = println("Calling C4.<locally>.f(): " + values)
+ }
+}
+
+object Test extends App {
+ def check(c: Class[_]) {
+ val methodName = "f"
+ val methods = c.getDeclaredMethods.filter(_.getName == methodName)
+ println(methods.map(_.toString).sorted.mkString("\n"))
+ }
+
+ check(classOf[C1])
+ check(classOf[C2])
+ check(classOf[C2#C3])
+ check(classOf[C4])
+}
diff --git a/test/pending/run/t3899.check b/test/pending/run/t3899.check
new file mode 100644
index 0000000000..c317608eab
--- /dev/null
+++ b/test/pending/run/t3899.check
@@ -0,0 +1,4 @@
+a,b
+a,b
+a,b
+a,b
diff --git a/test/pending/run/t3899/Base_1.java b/test/pending/run/t3899/Base_1.java
new file mode 100644
index 0000000000..114cc0b7a6
--- /dev/null
+++ b/test/pending/run/t3899/Base_1.java
@@ -0,0 +1,5 @@
+public class Base_1 {
+ public String[] varargs1(String... as) {
+ return as;
+ }
+}
diff --git a/test/pending/run/t3899/Derived_2.scala b/test/pending/run/t3899/Derived_2.scala
new file mode 100644
index 0000000000..bb4e53784d
--- /dev/null
+++ b/test/pending/run/t3899/Derived_2.scala
@@ -0,0 +1,30 @@
+trait T extends Base_1 {
+ def t1(as: String*): Array[String] = {
+ varargs1(as: _*)
+ }
+ def t2(as: String*): Array[String] = {
+ // This is the bug reported in the ticket.
+ super.varargs1(as: _*)
+ }
+}
+
+class C extends Base_1 {
+ def c1(as: String*): Array[String] = {
+ varargs1(as: _*)
+ }
+ def c2(as: String*): Array[String] = {
+ super.varargs1(as: _*)
+ }
+}
+
+
+object Test extends App {
+ val t = new T {}
+ println(t.t1("a", "b").mkString(","))
+ println(t.t2("a", "b").mkString(","))
+
+ val c = new C {}
+ println(c.c1("a", "b").mkString(","))
+ println(c.c2("a", "b").mkString(","))
+
+}