diff options
author | Paul Phillips <paulp@improving.org> | 2009-09-29 12:33:10 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-09-29 12:33:10 +0000 |
commit | 72789e9bb8c2474563c405ee8087b5e21be5993d (patch) | |
tree | 3da344f22e97dbef3906ecd9eacb86b87f2d035c /src/compiler | |
parent | 198f9932b94be5ef86bd6e76c5f0de992f002e2b (diff) | |
download | scala-72789e9bb8c2474563c405ee8087b5e21be5993d.tar.gz scala-72789e9bb8c2474563c405ee8087b5e21be5993d.tar.bz2 scala-72789e9bb8c2474563c405ee8087b5e21be5993d.zip |
Equality tests emerging from the pattern matche...
Equality tests emerging from the pattern matcher should no longer
involve unnecessary boxing.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/TreeDSL.scala | 7 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/matching/ParallelMatching.scala | 16 |
2 files changed, 18 insertions, 5 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/TreeDSL.scala b/src/compiler/scala/tools/nsc/ast/TreeDSL.scala index 65c1cd7486..7b56a31e97 100644 --- a/src/compiler/scala/tools/nsc/ast/TreeDSL.scala +++ b/src/compiler/scala/tools/nsc/ast/TreeDSL.scala @@ -92,6 +92,13 @@ trait TreeDSL { else if (other == EmptyTree) target else gen.mkAnd(target, other) + /** Note - calling ANY_== in the matcher caused primitives to get boxed + * for the comparison, whereas looking up nme.EQ does not. + */ + def MEMBER_== (other: Tree) = { + if (target.tpe == null) ANY_==(other) + else fn(target, target.tpe member nme.EQ, other) + } def ANY_NE (other: Tree) = fn(target, nme.ne, toAnyRef(other)) def ANY_EQ (other: Tree) = fn(target, nme.eq, toAnyRef(other)) def ANY_== (other: Tree) = fn(target, Any_==, other) diff --git a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala index 41949ad8cf..4f2b24b406 100644 --- a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala +++ b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala @@ -560,6 +560,12 @@ trait ParallelMatching extends ast.TreeDSL { /** translate outcome of the rule application into code (possible involving recursive application of rewriting) */ def tree(): Tree + + override def toString = { + "RuleApplication/%s (%s: %s) { %s ... }".format( + getClass(), scrut, scrut.tpe, head + ) + } } case class ErrorRule() extends RuleApplication { @@ -650,7 +656,7 @@ trait ParallelMatching extends ast.TreeDSL { cases match { case List(CaseDef(lit, _, body)) => // only one case becomes if/else - IF (scrut.id ANY_== lit) THEN body ELSE defaultTree + IF (scrut.id MEMBER_== lit) THEN body ELSE defaultTree case _ => // otherwise cast to an Int if necessary and run match val target: Tree = if (!scrut.tpe.isInt) scrut.id DOT nme.toInt else scrut.id @@ -848,7 +854,7 @@ trait ParallelMatching extends ast.TreeDSL { // precondition for matching: sequence is exactly length of arg protected def getPrecondition(tree: Tree, lengthArg: Int) = - lengthCheck(tree, lengthArg, _ ANY_== _) + lengthCheck(tree, lengthArg, _ MEMBER_== _) final def tree() = { val Branch(TransitionContext(transition), succ, Some(fail)) = this.getTransition @@ -900,7 +906,7 @@ trait ParallelMatching extends ast.TreeDSL { // todo: optimize if no guard, and no further tests val fail = mkFail(List.map2(rest.rows.tail, pats.tail.ps)(_ insert _)) - val action = typer typed (scrut.id ANY_== value) + val action = typer typed (scrut.id MEMBER_== value) (Branch(action, mkNewRep(Nil, rest.tvars, succ), fail), label) } @@ -1268,9 +1274,9 @@ trait ParallelMatching extends ast.TreeDSL { typer typed (tpe match { case ct: ConstantType => ct.value match { case v @ Constant(null) if isAnyRef(scrutTree.tpe) => scrutTree ANY_EQ NULL - case v => scrutTree ANY_== Literal(v) + case v => scrutTree MEMBER_== Literal(v) } - case _: SingletonType if useEqTest => REF(tpe.termSymbol) ANY_== scrutTree + case _: SingletonType if useEqTest => REF(tpe.termSymbol) MEMBER_== scrutTree case _ if scrutTree.tpe <:< tpe && isAnyRef(tpe) => scrutTree OBJ_!= NULL case _ => scrutTree IS tpe }) |