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

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

scabug opened this issue Jun 10, 2013 · 4 comments


Copy link

scabug commented Jun 10, 2013

In the following example, I am trying to get the instance of Bar (copy from Stackoverflow - ):

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)

val fooC     = typeOf[Foo].typeSymbol.asClass
val subs     = fooC.knownDirectSubclasses
// fooC.companionSymbol.typeSignature
val tpeOther = subs.last.asType.toType

val res = getModule(tpeOther)

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

Copy link

scabug commented Jun 10, 2013

Imported From:
Reporter: @Sciss
Affected Versions: 2.10.2-RC2

Copy link

scabug commented Sep 15, 2013

Lars Hupel (larsrh) said:
Related to: milessabin/shapeless#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] }


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".

Copy link

scabug commented Sep 15, 2013

@xeno-by said:
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.

Copy link

SethTisue commented Mar 2, 2018

not clear to me there's anything actionable here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

No branches or pull requests

3 participants