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 equal types returns false #5959

Closed
scabug opened this issue Jun 21, 2012 · 8 comments
Closed

Reflection: == on equal types returns false #5959

scabug opened this issue Jun 21, 2012 · 8 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jun 21, 2012

I access the result type of a method ({Int) and compiare it with typeOf[Int], but that returns false. I expected to see true.

class X {
   def methodIntIntInt(x: Int, y: Int) = x+y
}

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{ currentMirror => cm }
val im: InstanceMirror = cm.reflect(new X)
val cs: ClassSymbol = im.symbol
val ts: Type = cs.typeSignature
val ms: MethodSymbol = ts.declaration(newTermName("methodIntIntInt")).asMethodSymbol
val MethodType( _, t1 ) = ms.typeSignature
val t2 = typeOf[scala.Int]

and now the output from the REPL:

scala> t1
res0: reflect.runtime.universe.Type = scala.Int

scala> t2
res1: reflect.runtime.universe.Type = Int

scala> showRaw(t1)
res2: String = TypeRef(ThisType(scala), scala.Int, List())

scala> showRaw(t2)
res3: String = TypeRef(ThisType(scala), scala.Int, List())

scala> t1==t2
res4: Boolean = false

scala> t1.normalize == t2.normalize
res5: Boolean = false
@scabug
Copy link
Author

scabug commented Jun 21, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5959?orig=1
Reporter: @dgruntz
Affected Versions: 2.10.0-M4

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@dgruntz said:
the type symbols of t1 and t2 are equivalent:

scala> t1.typeSymbol
res29: reflect.runtime.universe.Symbol = class Int

scala> t2.typeSymbol
res30: reflect.runtime.universe.Symbol = class Int

scala> t1.typeSymbol == t2.typeSymbol
res32: Boolean = true

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@adriaanm said:
and what about their prefixes?
if those are equal, hashconsing might be broken -- in principle it should hold that t1 eq t2

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@dgruntz said:
how can I access the prefix on a type of type {{reflect.runtime.universe.Type}}?

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@adriaanm said:
seems like hashconsing is broken:

scala> TypeRef.unapply(t1.asInstanceOf[TypeRef])
res2: Option[(reflect.runtime.universe.Type, reflect.runtime.universe.Symbol, List[reflect.runtime.universe.Type])] = Some((scala.type,class Int,List()))

scala> TypeRef.unapply(t2.asInstanceOf[TypeRef])
res3: Option[(reflect.runtime.universe.Type, reflect.runtime.universe.Symbol, List[reflect.runtime.universe.Type])] = Some((scala.type,class Int,List()))

scala> TypeRef.unapply(t1.asInstanceOf[TypeRef]).get._1 == TypeRef.unapply(t2.asInstanceOf[TypeRef]).get._1
res9: Boolean = false

scala> System.identityHashCode(t1)
res10: Int = 598346035

scala> System.identityHashCode(t2)
res11: Int = 1622300681

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@xeno-by said (edited on Jun 21, 2012 10:57:28 PM UTC):
The sky is not falling. It's just that symbols have been loaded by different mirrors.

scala> mirrorThatLoaded(pre1.typeSymbol)
res2 @ 6547bc27: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain
$TranslatingClassLoader@4069b25f of type class scala.tools.nsc.interpreter.IMain$TranslatingClassLoa
der with classpath <unknown>

scala> mirrorThatLoaded(pre2.typeSymbol)
res3 @ 4b59df6c: reflect.runtime.universe.Mirror = JavaMirror with sun.misc.Launcher$AppClassLoader@
546b97fd of type class sun.misc.Launcher$AppClassLoader with classpath [file:/C:/Projects/Kepler/tes
t/files/codelib/code.jar,file:/C:/Projects/Kepler/lib/jline.jar,file:/C:/Projects/Kepler/lib/fjbg.ja
r,file:/C:/Projects/Kepler/build/locker/classes/compiler/,file:/C:/Projects/Kepler/build/locker/clas
ses/reflect/,file:/C:/Projects/Kepler/build/locker/classes/library/]

Very well. It seems that we need to disable symbol sharing altogether.

@scabug
Copy link
Author

scabug commented Jun 21, 2012

@xeno-by said:
Good news is that this works outside REPL.

@scabug
Copy link
Author

scabug commented Jul 6, 2012

@xeno-by said:
Fixed in scala/scala@0529dd5

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

No branches or pull requests

2 participants