bug icon indicating copy to clipboard operation
bug copied to clipboard

NoSuchMethodException when the return value of a function in a structural refinement on Any is a parameter

Open scabug opened this issue 9 years ago • 3 comments

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

scabug avatar Sep 09 '16 16:09 scabug

Imported From: https://issues.scala-lang.org/browse/SI-9919?orig=1 Reporter: Michał Kosek (michau) Affected Versions: 2.11.8

scabug avatar Sep 09 '16 16:09 scabug

@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

scabug avatar Sep 13 '16 20:09 scabug

@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)

scabug avatar Feb 11 '17 18:02 scabug