Skip to content
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

method overloading type inference regression #9796

Closed
scabug opened this issue May 29, 2016 · 4 comments
Closed

method overloading type inference regression #9796

scabug opened this issue May 29, 2016 · 4 comments
Labels

Comments

@scabug
Copy link

scabug commented May 29, 2016

When preparing twitter util for cross-publishing to scala 2.12, we found that a method that we had previously used without having to specify types now required type annotations.

The diff is here:

https://github.com/twitter/util/pull/163/files

The method is Witness#apply, which is overloaded.

https://github.com/twitter/util/blob/develop/util-core/src/main/scala/com/twitter/util/Event.scala#L416-L442

We did a little more thinking about what might be going on here:

twitter/util#162 (comment)

We were able to construct a reproduction case which succeeds on 2.11, but fails on 2.12.

trait Updatable[T] {
  def update(t: T): Unit
}

object A {
  def apply[T](f: T => Unit): T => Unit = f

  def apply[T](u: Updatable[T]): T => Unit = { t: T =>
    u() = t
  }
}

object C {
  val x: Any => Unit = A(println(_)) // fails
  val y: Int => Unit = A({ num => num + 2 }) // fails
}

The output on 2.12 is:

[error] /Users/moses/projects/reproduce/src/main/scala/com/mosesn/reproduce/Driver.scala:14: missing parameter type for expanded function ((x$1: <error>) => println(x$1))
[error]   val x: Any => Unit = A(println(_)) // fails
[error]                                  ^
[error] /Users/moses/projects/reproduce/src/main/scala/com/mosesn/reproduce/Driver.scala:15: missing parameter type
[error]   val y: Int => Unit = A({ num => num + 2 }) // fails
[error]                            ^
[error] two errors found
[error] (compile:compile) Compilation failed
[error] Total time: 4 s, completed May 29, 2016 8:06:20 AM

Under 2.11, it compiles successfully.

@scabug
Copy link
Author

scabug commented May 29, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9796?orig=1
Reporter: Moses Nakamura (mosesn)
Affected Versions: 2.12.0-M4

@scabug
Copy link
Author

scabug commented May 29, 2016

Moses Nakamura (mosesn) said (edited on May 29, 2016 3:26:15 PM UTC):
Update: adding another method to Updatable makes it work ¯\_(ツ)_/¯

@scabug
Copy link
Author

scabug commented May 29, 2016

@som-snytt said (edited on May 29, 2016 6:39:55 PM UTC):
Updatable is a SAM type. You'll see the new behavior in 2.11 under -Xexperimental. You'll see the old behavior under -Xsource:2.11.

When each A.apply takes a function parameter, then the args are typed with no expected type during overload resolution.

In the working case, the shape of the args selects only the alternative that takes a function.

This is discussed in the announcement under breaking changes.

@scabug
Copy link
Author

scabug commented May 29, 2016

Moses Nakamura (mosesn) said:
Aha! I understand now, I didn't get what a SAM type was. Thanks!

@scabug scabug closed this as completed May 29, 2016
@scabug scabug added the backend label Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant