Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-7567

Reflection on inner classes fails when outer object is not fully reflected before

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: Scala 2.10.2-RC2
    • Fix Version/s: None
    • Component/s: Reflection
    • Labels:
      None
    • Environment:

      Scala 2.10.0 through 2.10.2

      Description

      In the following example, I am trying to get the instance of Bar (copy from Stackoverflow - http://stackoverflow.com/questions/17012294/recovering-a-singleton-instance-via-reflection-from-sealed-super-trait-when-typ/17016268#17016268 ):

      import reflect.runtime.universe._
      import reflect.runtime.{currentMirror => cm}
       
      object Foo {
        case object Bar extends Foo
      }
      sealed trait Foo
       
      def getModule(tpe: Type): Any = {
        val classSymbol  = tpe.typeSymbol.asClass
        val compSymbol   = classSymbol.companionSymbol  // gives <none> !
        val moduleSymbol = compSymbol.asModule
        val moduleMirror = cm.reflectModule(moduleSymbol)
        moduleMirror.instance
      }
       
      val fooC     = typeOf[Foo].typeSymbol.asClass
      val subs     = fooC.knownDirectSubclasses
      // fooC.companionSymbol.typeSignature
      val tpeOther = subs.last.asType.toType
       
      val res = getModule(tpeOther)
      println(res)
      

      The companion symbol resolution in the getModule method fails here. If I uncomment the line fooC.companionSymbol.typeSignature, it works as expected.

        Attachments

          Activity

          Hide
          larsrh Lars Hupel added a comment -

          Related to: https://github.com/milessabin/shapeless/issues/48

          import scala.language.experimental.macros
          import scala.reflect.macros.Context
          def fooImpl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = { println(c.weakTypeOf[T].typeSymbol.asClass.companionSymbol); c.universe.reify {} }
          def foo[T] = macro fooImpl[T]
           
          scala> { case class Frob(x: Int); foo[Frob] }
          <none>
          

          However:

          import scala.reflect.runtime.universe._
           
          scala> { case class Frob(x: Int); weakTypeOf[Frob].typeSymbol.asClass.companionSymbol }
          scala.ScalaReflectionException: free type Frob is not a class
          

          So, run-time and compile-time reflection disagree about whether `Frob` is a "class" or a "free type".

          Show
          larsrh Lars Hupel added a comment - Related to: https://github.com/milessabin/shapeless/issues/48 import scala.language.experimental.macros import scala.reflect.macros.Context def fooImpl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = { println(c.weakTypeOf[T].typeSymbol.asClass.companionSymbol); c.universe.reify {} } def foo[T] = macro fooImpl[T]   scala> { case class Frob(x: Int); foo[Frob] } <none> However: import scala.reflect.runtime.universe._   scala> { case class Frob(x: Int); weakTypeOf[Frob].typeSymbol.asClass.companionSymbol } scala.ScalaReflectionException: free type Frob is not a class So, run-time and compile-time reflection disagree about whether `Frob` is a "class" or a "free type".
          Hide
          burmako Eugene Burmako added a comment -

          That's a known issue caused by the fact that metadata of local classes is not pickled. Therefore local classes, when used in reify and weakTypeOf, are reified as dummy free types.

          Show
          burmako Eugene Burmako added a comment - That's a known issue caused by the fact that metadata of local classes is not pickled. Therefore local classes, when used in reify and weakTypeOf, are reified as dummy free types.

            People

            • Assignee:
              burmako Eugene Burmako
              Reporter:
              sciss Sciss
            • Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: