Scala Programming Language
  1. Scala Programming Language
  2. SI-5356

structural type conformance allows values as AnyRefs, then doesn't box them

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.1, Scala 2.10.0
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      Confirmed on 2.9.1 and 2.10 trunk

      Description

      This issue originates here: http://stackoverflow.com/questions/8422690/generic-method-convertible-to-structural-type-in-scala

      Code to reproduce:

      val a = implicitly[Int => { def toInt : Int }]
      a(1).toInt
      

      The REPL shows that the implicit works and the exception gets thrown when trying to invoke `toInt`:

      scala> val a = implicitly[Int => { def toInt : Int }]
      a: Int => AnyRef{def toInt: Int} = <function1>
      
      scala> a(1)
      res1: AnyRef{def toInt: Int} = 1
      
      scala> a(1).toInt
      java.lang.NoSuchMethodException
      	at scala.runtime.BoxesRunTime.toInteger(Unknown Source)
      	at .<init>(<console>:9)
      	at .<clinit>(<console>:9)
      	at .<init>(<console>:11)
      	at .<clinit>(<console>)
      	at $print(<console>)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:601)
      	at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:751)
      	at scala.tools.nsc.interpreter.IMain$Request$$anonfun$13.apply(IMain.scala:992)
      	at scala.tools.nsc.interpreter.Line.scala$tools$nsc$interpreter$Line$$runAndSetState(Line.scala:41)
      	at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:47)
      	at scala.tools.nsc.io.package$$anon$2.run(package.scala:19)
      	at java.lang.Thread.run(Thread.java:722)
      

        Activity

        Hide
        Paul Phillips added a comment -

        This is unrelated to implicits; it's more directly demonstrated as

        (1: { def toInt: Int }).toInt
        
        Show
        Paul Phillips added a comment - This is unrelated to implicits; it's more directly demonstrated as (1: { def toInt: Int }).toInt
        Hide
        Paul Phillips added a comment -

        Oh good, hadn't heard from RichInt in a while.

        scala> (1: { def toInt: Int }).getClass
        res2: Class[_ <: AnyRef] = class scala.runtime.RichInt
        
        Show
        Paul Phillips added a comment - Oh good, hadn't heard from RichInt in a while. scala> (1: { def toInt: Int }).getClass res2: Class[_ <: AnyRef] = class scala.runtime.RichInt
        Hide
        Paul Phillips added a comment -

        be46e48713

        Show
        Paul Phillips added a comment - be46e48713

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Simon Ochsenreither
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development