summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan@lightbend.com>2016-08-13 08:33:25 -0700
committerGitHub <noreply@github.com>2016-08-13 08:33:25 -0700
commit3cb45e16cd8c12e4704431f40e4186cc8bd7ed63 (patch)
tree91f4887985c706e4e093fa892e41e4e53f974c14 /test
parentd6f601d53e53be6f8071c0646c19b68faac69ec0 (diff)
parent228d1b1ceb7643df1313672cd620cddb1a429029 (diff)
downloadscala-3cb45e16cd8c12e4704431f40e4186cc8bd7ed63.tar.gz
scala-3cb45e16cd8c12e4704431f40e4186cc8bd7ed63.tar.bz2
scala-3cb45e16cd8c12e4704431f40e4186cc8bd7ed63.zip
Merge pull request #5307 from adriaanm/issue-157
Propagate overloaded function type to expected arg type
Diffstat (limited to 'test')
-rw-r--r--test/files/neg/sammy_overload.check14
-rw-r--r--test/files/neg/sammy_overload.scala12
-rw-r--r--test/files/neg/t6214.check7
-rw-r--r--test/files/pos/overloaded_ho_fun.scala51
4 files changed, 70 insertions, 14 deletions
diff --git a/test/files/neg/sammy_overload.check b/test/files/neg/sammy_overload.check
index 903d7c88f4..87b198f4f0 100644
--- a/test/files/neg/sammy_overload.check
+++ b/test/files/neg/sammy_overload.check
@@ -1,7 +1,7 @@
-sammy_overload.scala:11: error: missing parameter type for expanded function ((x$1: <error>) => x$1.toString)
- O.m(_.toString) // error expected: eta-conversion breaks down due to overloading
- ^
-sammy_overload.scala:12: error: missing parameter type
- O.m(x => x) // error expected: needs param type
- ^
-two errors found
+sammy_overload.scala:14: error: overloaded method value m with alternatives:
+ (x: ToString)Int <and>
+ (x: Int => String)Int
+ cannot be applied to (Int => Int)
+ O.m(x => x) // error expected: m cannot be applied to Int => Int
+ ^
+one error found
diff --git a/test/files/neg/sammy_overload.scala b/test/files/neg/sammy_overload.scala
index 91c52cf96c..548e9d2d2e 100644
--- a/test/files/neg/sammy_overload.scala
+++ b/test/files/neg/sammy_overload.scala
@@ -2,12 +2,14 @@ trait ToString { def convert(x: Int): String }
class ExplicitSamType {
object O {
- def m(x: Int => String): Int = 0
- def m(x: ToString): Int = 1
+ def m(x: Int => String): Int = 0 // (1)
+ def m(x: ToString): Int = 1 // (2)
}
- O.m((x: Int) => x.toString) // ok, function type takes precedence
+ O.m((x: Int) => x.toString) // ok, function type takes precedence, because (1) is more specific than (2),
+ // because (1) is as specific as (2): (2) can be applied to a value of type Int => String (well, assuming it's a function literal)
+ // but (2) is not as specific as (1): (1) cannot be applied to a value of type ToString
- O.m(_.toString) // error expected: eta-conversion breaks down due to overloading
- O.m(x => x) // error expected: needs param type
+ O.m(_.toString) // ok: overloading resolution pushes through `Int` as the argument type, so this type checks
+ O.m(x => x) // error expected: m cannot be applied to Int => Int
}
diff --git a/test/files/neg/t6214.check b/test/files/neg/t6214.check
index 6349a3e71c..9d746351d1 100644
--- a/test/files/neg/t6214.check
+++ b/test/files/neg/t6214.check
@@ -1,4 +1,7 @@
-t6214.scala:5: error: missing parameter type
+t6214.scala:5: error: ambiguous reference to overloaded definition,
+both method m in object Test of type (f: Int => Unit)Int
+and method m in object Test of type (f: String => Unit)Int
+match argument types (Any => Unit)
m { s => case class Foo() }
- ^
+ ^
one error found
diff --git a/test/files/pos/overloaded_ho_fun.scala b/test/files/pos/overloaded_ho_fun.scala
new file mode 100644
index 0000000000..2699ad35f8
--- /dev/null
+++ b/test/files/pos/overloaded_ho_fun.scala
@@ -0,0 +1,51 @@
+import scala.math.Ordering
+import scala.reflect.ClassTag
+
+trait Sam { def apply(x: Int): String }
+trait SamP[U] { def apply(x: Int): U }
+
+class OverloadedFun[T](x: T) {
+ def foo(f: T => String): String = f(x)
+ def foo(f: Any => T): T = f("a")
+
+ def poly[U](f: Int => String): String = f(1)
+ def poly[U](f: Int => U): U = f(1)
+
+ def polySam[U](f: Sam): String = f(1)
+ def polySam[U](f: SamP[U]): U = f(1)
+
+ // check that we properly instantiate java.util.function.Function's type param to String
+ def polyJavaSam(f: String => String) = 1
+ def polyJavaSam(f: java.util.function.Function[String, String]) = 2
+}
+
+class StringLike(xs: String) {
+ def map[A](f: Char => A): Array[A] = ???
+ def map(f: Char => Char): String = ???
+}
+
+object Test {
+ val of = new OverloadedFun[Int](1)
+
+ of.foo(_.toString)
+
+ of.poly(x => x / 2 )
+ of.polySam(x => x / 2 )
+ of.polyJavaSam(x => x)
+
+ val sl = new StringLike("a")
+ sl.map(_ == 'a') // : Array[Boolean]
+ sl.map(x => 'a') // : String
+}
+
+object sorting {
+ def stableSort[K: ClassTag](a: Seq[K], f: (K, K) => Boolean): Array[K] = ???
+ def stableSort[L: ClassTag](a: Array[L], f: (L, L) => Boolean): Unit = ???
+
+ stableSort(??? : Seq[Boolean], (x: Boolean, y: Boolean) => x && !y)
+}
+
+// trait Bijection[A, B] extends (A => B) {
+// def andThen[C](g: Bijection[B, C]): Bijection[A, C] = ???
+// def compose[T](g: Bijection[T, A]) = g andThen this
+// }