diff options
author | Adriaan Moors <adriaan@lightbend.com> | 2017-04-10 10:12:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-10 10:12:01 -0700 |
commit | 715c88e9b74d1b4d1d0c4da9d0cc8f1b740e2dd3 (patch) | |
tree | b851e7bfd0dd821b39eae1ac25ca053362bee72d /test | |
parent | 2ba0530218daa170df5f1d25f7b39ab8cb8d0cf7 (diff) | |
parent | ade53a123c1edce12db442ee74b636d130e7e0f2 (diff) | |
download | scala-715c88e9b74d1b4d1d0c4da9d0cc8f1b740e2dd3.tar.gz scala-715c88e9b74d1b4d1d0c4da9d0cc8f1b740e2dd3.tar.bz2 scala-715c88e9b74d1b4d1d0c4da9d0cc8f1b740e2dd3.zip |
Merge pull request #5816 from adriaanm/userdefined-apply-212
Allow user-defined `[un]apply` in case companion
Diffstat (limited to 'test')
-rw-r--r-- | test/files/neg/userdefined_apply.check | 25 | ||||
-rw-r--r-- | test/files/neg/userdefined_apply.scala | 57 | ||||
-rw-r--r-- | test/files/pos/userdefined_apply.scala | 54 | ||||
-rw-r--r-- | test/files/pos/userdefined_apply_poly_overload.scala | 13 |
4 files changed, 149 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..c8c8976f5f --- /dev/null +++ b/test/files/neg/userdefined_apply.check @@ -0,0 +1,25 @@ +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:14: error: overloaded method apply needs result type + private def apply(x: Int) = if (x > 0) ClashRecNoSig(1) else ??? + ^ +userdefined_apply.scala:21: error: overloaded method apply needs result type + private def apply(x: Boolean) = if (x) NoClashNoSig(1) else ??? + ^ +userdefined_apply.scala:28: error: overloaded method apply needs result type + private def apply(x: Boolean) = if (x) NoClashOverload(1) else apply("") + ^ +userdefined_apply.scala:45: error: recursive method apply needs result type +case class NoClashNoSigPoly private(x: Int) + ^ +userdefined_apply.scala:39: error: NoClashNoSigPoly.type does not take parameters + def apply(x: T) = if (???) NoClashNoSigPoly(1) else ??? + ^ +userdefined_apply.scala:57: error: recursive method apply needs result type +case class ClashNoSigPoly private(x: Int) + ^ +userdefined_apply.scala:51: error: ClashNoSigPoly.type does not take parameters + def apply(x: T) = if (???) ClashNoSigPoly(1) else ??? + ^ +8 errors found diff --git a/test/files/neg/userdefined_apply.scala b/test/files/neg/userdefined_apply.scala new file mode 100644 index 0000000000..0a0d960b39 --- /dev/null +++ b/test/files/neg/userdefined_apply.scala @@ -0,0 +1,57 @@ +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 { + // TODO: status quo is that the error refers to an overloaded method, which is actually recursive + // (we should have unlinked the symbol in the `if(suppress)` part of `applyUnapplyMethodCompleter`) + // 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) + + +class BaseNCNSP[T] { + // TODO: suppress the following error + // error: NoClashNoSigPoly.type does not take parameters + def apply(x: T) = if (???) NoClashNoSigPoly(1) else ??? +} + +object NoClashNoSigPoly extends BaseNCNSP[Boolean] +// TODO: position error at definition of apply in superclass instead of on case clss +// error: recursive method apply needs result type +case class NoClashNoSigPoly private(x: Int) + + +class BaseCNSP[T] { + // TODO: suppress the following error + // error: ClashNoSigPoly.type does not take parameters + def apply(x: T) = if (???) ClashNoSigPoly(1) else ??? +} + +object ClashNoSigPoly extends BaseCNSP[Int] +// TODO: position error at definition of apply in superclass instead of on case clss +// error: recursive method apply needs result type +case class ClashNoSigPoly 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..e29f9f5141 --- /dev/null +++ b/test/files/pos/userdefined_apply.scala @@ -0,0 +1,54 @@ +// 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) + + + +class BaseNCP[T] { + // error: overloaded method apply needs result type + def apply(x: T): NoClashPoly = if (???) NoClashPoly(1) else ??? +} + +object NoClashPoly extends BaseNCP[Boolean] +case class NoClashPoly private(x: Int) + + +class BaseCP[T] { + // error: overloaded method apply needs result type + def apply(x: T): ClashPoly = if (???) ClashPoly(1) else ??? +} +object ClashPoly extends BaseCP[Int] +case class ClashPoly private(x: Int) diff --git a/test/files/pos/userdefined_apply_poly_overload.scala b/test/files/pos/userdefined_apply_poly_overload.scala new file mode 100644 index 0000000000..6760c1424f --- /dev/null +++ b/test/files/pos/userdefined_apply_poly_overload.scala @@ -0,0 +1,13 @@ +object Foo { + // spurious error if: + // - this definition precedes that of apply (which is overloaded with the synthetic one derived from the case class) + // - AND `Foo.apply` is explicitly applied to `[A]` (no error if `[A]` is inferred) + // + def referToPolyOverloadedApply[A]: Foo[A] = Foo.apply[A]("bla") + // ^ + // found : String("bla") + // required: Int + + def apply[A](x: Int): Foo[A] = ??? +} +case class Foo[A](x: String) // must be polymorphic |