New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong expected argument type for overloaded higher-order method #9943

Closed
scabug opened this Issue Oct 1, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@scabug
Copy link

scabug commented Oct 1, 2016

The following codes cannot be compiled with 2.12.0-RC1

class Foo[T] {
  def toMap[K, V](implicit ev: Foo[T] <:< Foo[(K, V)]): Foo[Map[K, V]] = null
  def toMap[K](keySelector: T => K): Foo[Map[K, T]] = null
}

object Foo {
  val f: Foo[Int] = null
  val m = f.toMap(_ % 2)
}

The error information is:

value % is not a member of Any
[error]   val m = f.toMap(_ % 2)
[error]                     ^
[error] one error found

It works with 2.12.0-M5, 2.11 and 2.10.

Is it a regression in 2.12.0-RC1?

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 1, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9943?orig=1
Reporter: Shixiong Zhu (zsxwing)
Affected Versions: 2.12.0-RC1

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 3, 2016

@adriaanm said:
Thank you for reporting, and the nice self-contained example!

This is a consequence of scala/scala#5307, which tries harder to infer an expected type for the function literal that's passed as the argument to the overloaded method toMap. Since the first argument of toMap's overloads is a (subtype of) a function type, we infer that the function literal should have type Any => ? (Any is the least upper bound of T and Foo[T] -- remember that <:< is a subclass of Function1). We could add a special case to exclude overloads where argument lists differ in their "implicitness", but I have to think about that a little bit more.

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 3, 2016

@milessabin said (edited on Oct 3, 2016 4:58:52 PM UTC):
Changing the handling of implicit argument lists in this respect would get a big -1 from me.

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 4, 2016

@scabug scabug closed this Oct 11, 2016

@scabug scabug added this to the 2.12.0-RC2 milestone Apr 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment