From 228d1b1ceb7643df1313672cd620cddb1a429029 Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Wed, 27 Jul 2016 11:18:08 -0700 Subject: Propagate overloaded function type to expected arg type Infer missing parameter types for function literals passed to higher-order overloaded methods by deriving the expected argument type from the function types in the overloaded method type's argument types. This eases the pain caused by methods becoming overloaded because SAM types and function types are compatible, which used to disable parameter type inference because for overload resolution arguments are typed without expected type, while typedFunction needs the expected type to infer missing parameter types for function literals. It also aligns us with dotty. The special case for function literals seems reasonable, as it has precedent, and it just enables the special case in typing function literals (derive the param types from the expected type). Since this does change type inference, you can opt out using the Scala 2.11 source level. Fix scala/scala-dev#157 --- test/files/neg/sammy_overload.check | 14 +++++++------- test/files/neg/sammy_overload.scala | 12 +++++++----- test/files/neg/t6214.check | 7 +++++-- 3 files changed, 19 insertions(+), 14 deletions(-) (limited to 'test/files/neg') 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: ) => 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 + (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 -- cgit v1.2.3