bug
bug copied to clipboard
NoSuchMethodException when the return value of a function in a structural refinement on Any is a parameter
The following code:
import scala.language.reflectiveCalls
def double[T](x: Any{def * (arg0: Int): T}) = x * 2
double[Int](4)
causes the following exception:
java.lang.NoSuchMethodException: java.lang.Integer.$times(int)
at java.lang.Class.getMethod(Class.java:1778)
at .reflMethod$Method1(<console>:18)
at .double(<console>:18)
... 32 elided
However, it works as expected when I change the definition of double to:
def double(x: Any{def * (arg0: Int): Int}) = x * 2
scala> double(4)
res21: Int = 8
Imported From: https://issues.scala-lang.org/browse/SI-9919?orig=1 Reporter: Michał Kosek (michau) Affected Versions: 2.11.8
@SethTisue said (edited on Sep 13, 2016 8:34:06 PM UTC):
note that structural types are AnyRef by default so an explicit "Any { ... }" is required here
@SethTisue said: #10175 has reproduction code that doesn't involve a type parameter:
val oops: Any { def +(a: Int): Any } = 42
import scala.language.reflectiveCalls
oops + 5
java.lang.NoSuchMethodException: java.lang.Integer.$plus(int)