summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/transform/Delambdafy.scala17
-rw-r--r--src/compiler/scala/tools/nsc/transform/Erasure.scala40
-rw-r--r--test/files/neg/t6260-named.check13
-rw-r--r--test/files/neg/t6260-named.scala15
-rw-r--r--test/files/neg/t6260.check13
-rw-r--r--test/files/neg/t6260b.check7
-rw-r--r--test/files/pos/delambdafy_t6260_method.check (renamed from test/files/neg/delambdafy_t6260_method.check)0
-rw-r--r--test/files/pos/delambdafy_t6260_method.flags (renamed from test/files/neg/delambdafy_t6260_method.flags)0
-rw-r--r--test/files/pos/delambdafy_t6260_method.scala (renamed from test/files/neg/delambdafy_t6260_method.scala)0
-rw-r--r--test/files/pos/t6260.flags (renamed from test/files/neg/t6260.flags)0
-rw-r--r--test/files/pos/t6260.scala (renamed from test/files/neg/t6260.scala)0
-rw-r--r--test/files/pos/t6260b.scala (renamed from test/files/neg/t6260b.scala)0
-rw-r--r--test/files/run/t6260-delambdafy.check4
-rw-r--r--test/files/run/t6260-delambdafy.flags1
-rw-r--r--test/files/run/t6260-delambdafy.scala12
-rw-r--r--test/files/run/t6260c.check5
-rw-r--r--test/files/run/t6260c.scala17
17 files changed, 103 insertions, 41 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala
index 933a2f70a1..d81a5d5755 100644
--- a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala
+++ b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala
@@ -232,6 +232,10 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre
val parents = addSerializable(abstractFunctionErasedType)
val funOwner = originalFunction.symbol.owner
+ // TODO harmonize the naming of delamdafy anon-fun classes with those spun up by Uncurry
+ // - make `anonClass.isAnonymousClass` true.
+ // - use `newAnonymousClassSymbol` or push the required variations into a similar factory method
+ // - reinstate the assertion in `Erasure.resolveAnonymousBridgeClash`
val suffix = "$lambda$" + (
if (funOwner.isPrimaryConstructor) ""
else "$" + funOwner.name
@@ -282,18 +286,11 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre
if (sym == NoSymbol) sym.toString
else s"$sym: ${sym.tpe} in ${sym.owner}"
- def clashError(bm: Symbol) = {
- unit.error(
- applyMethodDef.symbol.pos,
- sm"""bridge generated for member ${fulldef(applyMethodDef.symbol)}
- |which overrides ${fulldef(getMember(abstractFunctionErasedType.typeSymbol, nme.apply))}
- |clashes with definition of the member itself;
- |both have erased type ${exitingPostErasure(bm.tpe)}""")
- }
-
bridgeMethod foreach (bm =>
+ // TODO SI-6260 maybe just create the apply method with the signature (Object => Object) in all cases
+ // rather than the method+bridge pair.
if (bm.symbol.tpe =:= applyMethodDef.symbol.tpe)
- clashError(bm.symbol)
+ erasure.resolveAnonymousBridgeClash(applyMethodDef.symbol, bm.symbol)
)
val body = members ++ List(constr, applyMethodDef) ++ bridgeMethod
diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala
index ccfddab94a..60c1553ef3 100644
--- a/src/compiler/scala/tools/nsc/transform/Erasure.scala
+++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala
@@ -403,19 +403,19 @@ abstract class Erasure extends AddInterfaces
* @param other The overidden symbol for which the bridge was generated
* @param bridge The bridge
*/
- def checkBridgeOverrides(member: Symbol, other: Symbol, bridge: Symbol): Boolean = {
+ def checkBridgeOverrides(member: Symbol, other: Symbol, bridge: Symbol): Seq[(Position, String)] = {
def fulldef(sym: Symbol) =
if (sym == NoSymbol) sym.toString
else s"$sym: ${sym.tpe} in ${sym.owner}"
var noclash = true
+ val clashErrors = mutable.Buffer[(Position, String)]()
def clashError(what: String) = {
- noclash = false
- unit.error(
- if (member.owner == root) member.pos else root.pos,
- sm"""bridge generated for member ${fulldef(member)}
- |which overrides ${fulldef(other)}
- |clashes with definition of $what;
- |both have erased type ${exitingPostErasure(bridge.tpe)}""")
+ val pos = if (member.owner == root) member.pos else root.pos
+ val msg = sm"""bridge generated for member ${fulldef(member)}
+ |which overrides ${fulldef(other)}
+ |clashes with definition of $what;
+ |both have erased type ${exitingPostErasure(bridge.tpe)}"""
+ clashErrors += Tuple2(pos, msg)
}
for (bc <- root.baseClasses) {
if (settings.debug)
@@ -440,7 +440,7 @@ abstract class Erasure extends AddInterfaces
}
}
}
- noclash
+ clashErrors
}
/** TODO - work through this logic with a fine-toothed comb, incorporating
@@ -478,8 +478,18 @@ abstract class Erasure extends AddInterfaces
bridge setInfo (otpe cloneInfo bridge)
bridgeTarget(bridge) = member
- if (!(member.tpe exists (_.typeSymbol.isDerivedValueClass)) ||
- checkBridgeOverrides(member, other, bridge)) {
+ def sigContainsValueClass = (member.tpe exists (_.typeSymbol.isDerivedValueClass))
+
+ val shouldAdd = (
+ !sigContainsValueClass
+ || (checkBridgeOverrides(member, other, bridge) match {
+ case Nil => true
+ case es if member.owner.isAnonymousClass => resolveAnonymousBridgeClash(member, bridge); true
+ case es => for ((pos, msg) <- es) unit.error(pos, msg); false
+ })
+ )
+
+ if (shouldAdd) {
exitingErasure(root.info.decls enter bridge)
if (other.owner == root) {
exitingErasure(root.info.decls.unlink(other))
@@ -1127,5 +1137,13 @@ abstract class Erasure extends AddInterfaces
}
}
+ final def resolveAnonymousBridgeClash(sym: Symbol, bridge: Symbol) {
+ // TODO reinstate this after Delambdafy generates anonymous classes that meet this requirement.
+ // require(sym.owner.isAnonymousClass, sym.owner)
+ log(s"Expanding name of ${sym.debugLocationString} as it clashes with bridge. Renaming deemed safe because the owner is anonymous.")
+ sym.expandName(sym.owner)
+ bridge.resetFlag(BRIDGE)
+ }
+
private class TypeRefAttachment(val tpe: TypeRef)
}
diff --git a/test/files/neg/t6260-named.check b/test/files/neg/t6260-named.check
new file mode 100644
index 0000000000..ed6ab5e76f
--- /dev/null
+++ b/test/files/neg/t6260-named.check
@@ -0,0 +1,13 @@
+t6260-named.scala:12: error: bridge generated for member method apply: (a: C[Any])C[Any] in object O
+which overrides method apply: (v1: T1)R in trait Function1
+clashes with definition of the member itself;
+both have erased type (v1: Object)Object
+ def apply(a: C[Any]) = a
+ ^
+t6260-named.scala:14: error: bridge generated for member method apply: (a: C[Any])C[Any] in class X
+which overrides method apply: (a: A)A in trait T
+clashes with definition of the member itself;
+both have erased type (a: Object)Object
+ class X extends T[C[Any]] { def apply(a: C[Any]) = a }
+ ^
+two errors found
diff --git a/test/files/neg/t6260-named.scala b/test/files/neg/t6260-named.scala
new file mode 100644
index 0000000000..7cd9ce8473
--- /dev/null
+++ b/test/files/neg/t6260-named.scala
@@ -0,0 +1,15 @@
+class C[A](private val a: Any) extends AnyVal
+trait T[A] {
+ def apply(a: A): A
+}
+
+object Test {
+ (x: C[Any]) => {println(s"f($x)"); x} // okay
+ new T[C[Any]] { def apply(a: C[Any]) = a } // okay
+
+ // we can't rename the specific apply method to avoid the clash
+ object O extends Function1[C[Any], C[Any]] {
+ def apply(a: C[Any]) = a
+ }
+ class X extends T[C[Any]] { def apply(a: C[Any]) = a }
+}
diff --git a/test/files/neg/t6260.check b/test/files/neg/t6260.check
deleted file mode 100644
index 60c4add143..0000000000
--- a/test/files/neg/t6260.check
+++ /dev/null
@@ -1,13 +0,0 @@
-t6260.scala:3: error: bridge generated for member method apply: (bx: Box[X])Box[Y] in <$anon: Box[X] => Box[Y]>
-which overrides method apply: (v1: T1)R in trait Function1
-clashes with definition of the member itself;
-both have erased type (v1: Object)Object
- ((bx: Box[X]) => new Box(f(bx.x)))(this)
- ^
-t6260.scala:8: error: bridge generated for member method apply: (bx: Box[X])Box[Y] in <$anon: Box[X] => Box[Y]>
-which overrides method apply: (v1: T1)R in trait Function1
-clashes with definition of the member itself;
-both have erased type (v1: Object)Object
- ((bx: Box[X]) => new Box(f(bx.x)))(self)
- ^
-two errors found
diff --git a/test/files/neg/t6260b.check b/test/files/neg/t6260b.check
deleted file mode 100644
index 3a7e8947aa..0000000000
--- a/test/files/neg/t6260b.check
+++ /dev/null
@@ -1,7 +0,0 @@
-t6260b.scala:3: error: bridge generated for member method apply: ()X in <$anon: () => X>
-which overrides method apply: ()R in trait Function0
-clashes with definition of the member itself;
-both have erased type ()Object
-class Y { def f = new X("") or new X("") }
- ^
-one error found
diff --git a/test/files/neg/delambdafy_t6260_method.check b/test/files/pos/delambdafy_t6260_method.check
index f5cd6947d1..f5cd6947d1 100644
--- a/test/files/neg/delambdafy_t6260_method.check
+++ b/test/files/pos/delambdafy_t6260_method.check
diff --git a/test/files/neg/delambdafy_t6260_method.flags b/test/files/pos/delambdafy_t6260_method.flags
index 48b438ddf8..48b438ddf8 100644
--- a/test/files/neg/delambdafy_t6260_method.flags
+++ b/test/files/pos/delambdafy_t6260_method.flags
diff --git a/test/files/neg/delambdafy_t6260_method.scala b/test/files/pos/delambdafy_t6260_method.scala
index 93b5448227..93b5448227 100644
--- a/test/files/neg/delambdafy_t6260_method.scala
+++ b/test/files/pos/delambdafy_t6260_method.scala
diff --git a/test/files/neg/t6260.flags b/test/files/pos/t6260.flags
index 2349d8294d..2349d8294d 100644
--- a/test/files/neg/t6260.flags
+++ b/test/files/pos/t6260.flags
diff --git a/test/files/neg/t6260.scala b/test/files/pos/t6260.scala
index 93b5448227..93b5448227 100644
--- a/test/files/neg/t6260.scala
+++ b/test/files/pos/t6260.scala
diff --git a/test/files/neg/t6260b.scala b/test/files/pos/t6260b.scala
index 73e2e58f73..73e2e58f73 100644
--- a/test/files/neg/t6260b.scala
+++ b/test/files/pos/t6260b.scala
diff --git a/test/files/run/t6260-delambdafy.check b/test/files/run/t6260-delambdafy.check
new file mode 100644
index 0000000000..b2a7bed988
--- /dev/null
+++ b/test/files/run/t6260-delambdafy.check
@@ -0,0 +1,4 @@
+f(C@2e)
+
+Test$lambda$1$$apply
+apply
diff --git a/test/files/run/t6260-delambdafy.flags b/test/files/run/t6260-delambdafy.flags
new file mode 100644
index 0000000000..48b438ddf8
--- /dev/null
+++ b/test/files/run/t6260-delambdafy.flags
@@ -0,0 +1 @@
+-Ydelambdafy:method
diff --git a/test/files/run/t6260-delambdafy.scala b/test/files/run/t6260-delambdafy.scala
new file mode 100644
index 0000000000..056b1edd4e
--- /dev/null
+++ b/test/files/run/t6260-delambdafy.scala
@@ -0,0 +1,12 @@
+class C[A](private val a: Any) extends AnyVal
+
+object Test {
+ val f = (x: C[Any]) => {println(s"f($x)"); x}
+ def main(args: Array[String]) {
+ f(new C("."))
+ val methods = f.getClass.getDeclaredMethods.map(_.getName).sorted
+ println("")
+ println(methods.mkString("\n"))
+ }
+}
+
diff --git a/test/files/run/t6260c.check b/test/files/run/t6260c.check
new file mode 100644
index 0000000000..1a57f2d741
--- /dev/null
+++ b/test/files/run/t6260c.check
@@ -0,0 +1,5 @@
+f(C@2e)
+
+Test$$anonfun$$apply
+apply
+g(C@2e)
diff --git a/test/files/run/t6260c.scala b/test/files/run/t6260c.scala
new file mode 100644
index 0000000000..845dc157b7
--- /dev/null
+++ b/test/files/run/t6260c.scala
@@ -0,0 +1,17 @@
+class C[A](private val a: Any) extends AnyVal
+
+object Test {
+ val f = (x: C[Any]) => {println(s"f($x)"); x}
+ trait T[A] {
+ def apply(a: A): A
+ }
+ val g = new T[C[Any]] { def apply(a: C[Any]) = { println(s"g($a)"); a } }
+ def main(args: Array[String]) {
+ f(new C("."))
+ val methods = f.getClass.getDeclaredMethods.map(_.getName).sorted
+ println("")
+ println(methods.mkString("\n"))
+ g.apply(new C("."))
+ }
+}
+