diff options
-rw-r--r-- | src/compiler/scala/tools/nsc/backend/icode/GenICode.scala | 14 | ||||
-rw-r--r-- | test/files/run/t7852.check | 22 |
2 files changed, 19 insertions, 17 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) diff --git a/test/files/run/t7852.check b/test/files/run/t7852.check index fdced88fa8..008639075a 100644 --- a/test/files/run/t7852.check +++ b/test/files/run/t7852.check @@ -2,29 +2,17 @@ class scala.tools.asm.tree.MethodNode LDC "" ALOAD 0 INVOKEVIRTUAL java/lang/Object.toString ()Ljava/lang/String; - ASTORE 1 - DUP - IFNONNULL L0 - POP - ALOAD 1 - IFNULL L1 - GOTO L2 - L0 - FRAME FULL [Lean java/lang/String] [java/lang/String] - ALOAD 1 INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z - IFEQ L2 - L1 - FRAME SAME + IFEQ L0 ICONST_1 - GOTO L3 - L2 + GOTO L1 + L0 FRAME SAME ICONST_0 - L3 + L1 FRAME SAME1 I POP RETURN MAXSTACK = 2 - MAXLOCALS = 2 + MAXLOCALS = 1 |