Permalink
Browse files

SI-6911, regression in generated case class equality.

Caught out by the different semantics of isInstanceOf and
pattern matching.

  trait K { case class CC(name: String) }
  object Foo extends K
  object Bar extends K
  Foo.CC("a") == Bar.CC("a")

That expression is supposed to be false, and with this
commit it is once again.
  • Loading branch information...
1 parent e112ac9 commit eeb6ee6eb21e7bd473ab5775474444b5d1b72856 @paulp paulp committed Jan 4, 2013
@@ -157,11 +157,23 @@ trait SyntheticMethods extends ast.TreeDSL {
Ident(m.firstParam) IS_OBJ classExistentialType(clazz))
}
- /** (that.isInstanceOf[this.C])
- * where that is the given methods first parameter.
+ /** that match { case _: this.C => true ; case _ => false }
+ * where `that` is the given method's first parameter.
+ *
+ * An isInstanceOf test is insufficient because it has weaker
+ * requirements than a pattern match. Given an inner class Foo and
+ * two different instantiations of the container, an x.Foo and and a y.Foo
+ * are both .isInstanceOf[Foo], but the one does not match as the other.
*/
- def thatTest(eqmeth: Symbol): Tree =
- gen.mkIsInstanceOf(Ident(eqmeth.firstParam), classExistentialType(clazz), true, false)
+ def thatTest(eqmeth: Symbol): Tree = {
+ Match(
+ Ident(eqmeth.firstParam),
+ List(
+ CaseDef(Typed(Ident(nme.WILDCARD), TypeTree(clazz.tpe)), EmptyTree, TRUE),
+ CaseDef(WILD.empty, EmptyTree, FALSE)
+ )
+ )
+ }
/** (that.asInstanceOf[this.C])
* where that is the given methods first parameter.
@@ -29,7 +29,10 @@ C(2,3)
Statics.this.finalizeHash(acc, 2)
};
override <synthetic> def toString(): String = ScalaRunTime.this._toString(C.this);
- override <synthetic> def equals(x$1: Any): Boolean = C.this.eq(x$1.asInstanceOf[Object]).||(x$1.isInstanceOf[C].&&({
+ override <synthetic> def equals(x$1: Any): Boolean = C.this.eq(x$1.asInstanceOf[Object]).||(x$1 match {
+ case (_: C) => true
+ case _ => false
+}.&&({
<synthetic> val C$1: C = x$1.asInstanceOf[C];
C.this.x.==(C$1.x).&&(C.this.y.==(C$1.y)).&&(C$1.canEqual(C.this))
}))
Oops, something went wrong.

0 comments on commit eeb6ee6

Please sign in to comment.