summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan@lightbend.com>2017-02-26 16:01:37 -0800
committerAdriaan Moors <adriaan@lightbend.com>2017-02-27 17:15:43 -0800
commit894b027ddf1551317ecdbda053aec90b6d6fa2f4 (patch)
tree7191b771c3d19524f1eb4ef087c5cbfd35d79eaa /test
parent011cc7ec86105640a6d606998f769986630fb62a (diff)
downloadscala-894b027ddf1551317ecdbda053aec90b6d6fa2f4.tar.gz
scala-894b027ddf1551317ecdbda053aec90b6d6fa2f4.tar.bz2
scala-894b027ddf1551317ecdbda053aec90b6d6fa2f4.zip
Allow user-defined `[un]apply` in case companion
Don't emit a synthetic `apply` (or `unapply`) when it would clash with an existing one. This allows e.g., a `private apply`, along with a `case class` with a `private` constructor. We have to retract the synthetic method in a pretty roundabout way, as we need the other methods and the owner to be completed already. Unless we have to complete the synthetic `apply` while completing the user-defined one, this should not be a problem. If this does happen, this implies there's a cycle in computing the user-defined signature and the synthetic one, which is not allowed.
Diffstat (limited to 'test')
-rw-r--r--test/files/neg/userdefined_apply.check13
-rw-r--r--test/files/neg/userdefined_apply.scala31
-rw-r--r--test/files/pos/userdefined_apply.scala36
3 files changed, 80 insertions, 0 deletions
diff --git a/test/files/neg/userdefined_apply.check b/test/files/neg/userdefined_apply.check
new file mode 100644
index 0000000000..ca0154885d
--- /dev/null
+++ b/test/files/neg/userdefined_apply.check
@@ -0,0 +1,13 @@
+userdefined_apply.scala:3: error: overloaded method apply needs result type
+ private def apply(x: Int) = if (x > 0) new ClashOverloadNoSig(x) else apply("")
+ ^
+userdefined_apply.scala:12: error: overloaded method apply needs result type
+ private def apply(x: Int) = if (x > 0) ClashRecNoSig(1) else ???
+ ^
+userdefined_apply.scala:19: error: overloaded method apply needs result type
+ private def apply(x: Boolean) = if (x) NoClashNoSig(1) else ???
+ ^
+userdefined_apply.scala:26: error: overloaded method apply needs result type
+ private def apply(x: Boolean) = if (x) NoClashOverload(1) else apply("")
+ ^
+four errors found
diff --git a/test/files/neg/userdefined_apply.scala b/test/files/neg/userdefined_apply.scala
new file mode 100644
index 0000000000..1f2aff6e82
--- /dev/null
+++ b/test/files/neg/userdefined_apply.scala
@@ -0,0 +1,31 @@
+object ClashOverloadNoSig {
+ // error: overloaded method apply needs result type
+ private def apply(x: Int) = if (x > 0) new ClashOverloadNoSig(x) else apply("")
+
+ def apply(x: String): ClashOverloadNoSig = ???
+}
+
+case class ClashOverloadNoSig private(x: Int)
+
+object ClashRecNoSig {
+ // error: recursive method apply needs result type
+ private def apply(x: Int) = if (x > 0) ClashRecNoSig(1) else ???
+}
+
+case class ClashRecNoSig private(x: Int)
+
+object NoClashNoSig {
+ // error: overloaded method apply needs result type
+ private def apply(x: Boolean) = if (x) NoClashNoSig(1) else ???
+}
+
+case class NoClashNoSig private(x: Int)
+
+object NoClashOverload {
+ // error: overloaded method apply needs result type
+ private def apply(x: Boolean) = if (x) NoClashOverload(1) else apply("")
+
+ def apply(x: String): NoClashOverload = ???
+}
+
+case class NoClashOverload private(x: Int)
diff --git a/test/files/pos/userdefined_apply.scala b/test/files/pos/userdefined_apply.scala
new file mode 100644
index 0000000000..ca563f1dc5
--- /dev/null
+++ b/test/files/pos/userdefined_apply.scala
@@ -0,0 +1,36 @@
+// NOTE: the companion inherits a public apply method from Function1!
+case class NeedsCompanion private (x: Int)
+
+object ClashNoSig { // ok
+ private def apply(x: Int) = if (x > 0) new ClashNoSig(x) else ???
+}
+case class ClashNoSig private (x: Int)
+
+
+object Clash {
+ private def apply(x: Int) = if (x > 0) new Clash(x) else ???
+}
+case class Clash private (x: Int)
+
+object ClashSig {
+ private def apply(x: Int): ClashSig = if (x > 0) new ClashSig(x) else ???
+}
+case class ClashSig private (x: Int)
+
+object ClashOverload {
+ private def apply(x: Int): ClashOverload = if (x > 0) new ClashOverload(x) else apply("")
+ def apply(x: String): ClashOverload = ???
+}
+case class ClashOverload private (x: Int)
+
+object NoClashSig {
+ private def apply(x: Boolean): NoClashSig = if (x) NoClashSig(1) else ???
+}
+case class NoClashSig private (x: Int)
+
+object NoClashOverload {
+ // needs full sig
+ private def apply(x: Boolean): NoClashOverload = if (x) NoClashOverload(1) else apply("")
+ def apply(x: String): NoClashOverload = ???
+}
+case class NoClashOverload private (x: Int)