From a43e6b1242018f538a25efd70e74ab87abc8691d Mon Sep 17 00:00:00 2001 From: Gilles Dubochet Date: Sun, 20 May 2007 16:05:38 +0000 Subject: Rich comparison (scala.runtime.Comparator.equal... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rich comparison (scala.runtime.Comparator.equals) is not inserted when both sides of an equality are statically known to be equal and of a subtype of Number or Character. This is an optimisation — behaviour should be equivalent as their own equal methods should do ok. --- src/compiler/scala/tools/nsc/backend/icode/GenICode.scala | 15 +++++++++++---- src/library/scala/runtime/Comparator.java | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index b668e28c05..5ddf6b052d 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -1378,15 +1378,22 @@ abstract class GenICode extends SubComponent { local } + /** True if the equality comparison is between values that require the use of the rich equality + * comparator (scala.runtime.Comparator.equals). This is the case when either side of the + * comparison might have a run-time type subtype of java.lang.Number or java.lang.Character. + * When it is statically known that both sides are equal and subtypes of Number of Character, + * not using the rich equality is possible (their own equals method will do ok.)*/ def mustUseAnyComparator: Boolean = { val lsym = l.tpe.symbol val rsym = r.tpe.symbol (lsym == definitions.ObjectClass) || (rsym == definitions.ObjectClass) || - (lsym isNonBottomSubClass definitions.BoxedNumberClass)|| - (!forMSIL && (lsym isNonBottomSubClass BoxedCharacterClass)) || - (rsym isNonBottomSubClass definitions.BoxedNumberClass) || - (!forMSIL && (rsym isNonBottomSubClass BoxedCharacterClass)) + (lsym != rsym) && ( + (lsym isNonBottomSubClass definitions.BoxedNumberClass) || + (!forMSIL && (lsym isNonBottomSubClass BoxedCharacterClass)) || + (rsym isNonBottomSubClass definitions.BoxedNumberClass) || + (!forMSIL && (rsym isNonBottomSubClass BoxedCharacterClass)) + ) } if (mustUseAnyComparator) { diff --git a/src/library/scala/runtime/Comparator.java b/src/library/scala/runtime/Comparator.java index c73c7506ba..705bf30d5c 100644 --- a/src/library/scala/runtime/Comparator.java +++ b/src/library/scala/runtime/Comparator.java @@ -36,8 +36,8 @@ public class Comparator { * ICode phase, method genEqEqPrimitive) only when either * side of the comparison is a subclass of AnyVal, of * java.lang.Number, of java.lang.Character or - * is exactly Any or AnyRef. - */ + * is exactly Any or AnyRef, but when both sides + * have different types. */ public static boolean equals(Object a, Object b) { if (a == null) return b == null; -- cgit v1.2.3