From eeb6ee6eb21e7bd473ab5775474444b5d1b72856 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Thu, 3 Jan 2013 20:35:22 -0800 Subject: 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. --- .../tools/nsc/typechecker/SyntheticMethods.scala | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 17e67e6429..a907ab6c66 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -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. -- cgit v1.2.3