summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-09-17 15:43:13 +0200
committerJason Zaugg <jzaugg@gmail.com>2013-09-17 16:03:50 +0200
commit9fee7b6a2ac8663b0608b85001259320e1409a04 (patch)
tree3551b0f01c584566172438db32db508324fb633b /src/compiler/scala/tools/nsc/backend/icode/GenICode.scala
parent2bb960c68091b804d8ace1f76460279f6567a193 (diff)
downloadscala-9fee7b6a2ac8663b0608b85001259320e1409a04.tar.gz
scala-9fee7b6a2ac8663b0608b85001259320e1409a04.tar.bz2
scala-9fee7b6a2ac8663b0608b85001259320e1409a04.zip
SI-7852 Omit null check for "".==
Although the same the code would be later optimized by -Yconst-opt, we can offer the same lean byte code to those compiling without that option by being more discerning when translating ==. This helps people using bytecode based code coverage tools such as jacoco that would emit "branch not covered" warnings for the impossible null check.
Diffstat (limited to 'src/compiler/scala/tools/nsc/backend/icode/GenICode.scala')
-rw-r--r--src/compiler/scala/tools/nsc/backend/icode/GenICode.scala14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala
index 843299398b..1413add240 100644
--- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala
+++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala
@@ -1319,6 +1319,7 @@ abstract class GenICode extends SubComponent {
/** Some useful equality helpers.
*/
def isNull(t: Tree) = cond(t) { case Literal(Constant(null)) => true }
+ def isLiteral(t: Tree) = cond(t) { case Literal(_) => true }
/* If l or r is constant null, returns the other ; otherwise null */
def ifOneIsNull(l: Tree, r: Tree) = if (isNull(l)) r else if (isNull(r)) l else null
@@ -1514,6 +1515,19 @@ abstract class GenICode extends SubComponent {
val branchesReachable = !ctx1.bb.ignore
ctx1.bb emitOnly CZJUMP(thenCtx.bb, elseCtx.bb, EQ, ObjectReference)
branchesReachable
+ } else if (isLiteral(l)) {
+ // "" == expr -> "".equals(expr) (no null check)
+ // Common enough (through pattern matching) to treat this specially here rather than
+ // hoping that -Yconst-opt is enabled. The impossible branches for null checks lead
+ // to spurious "branch not covered" warnings in Jacoco code coverage.
+ var ctx1 = genLoad(l, ctx, ObjectReference)
+ val branchesReachable = !ctx1.bb.ignore
+ ctx1 = genLoad(r, ctx1, ObjectReference)
+ ctx1.bb emitOnly(
+ CALL_METHOD(Object_equals, Dynamic),
+ CZJUMP(thenCtx.bb, elseCtx.bb, NE, BOOL)
+ )
+ branchesReachable
} else {
val eqEqTempLocal = getTempLocal
var ctx1 = genLoad(l, ctx, ObjectReference)