summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/nsc/backend/jvm/opt/UnusedLocalVariablesTest.scala
blob: c9c98b403b56b161c39e2e97382c294c69b431ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package scala.tools.nsc
package backend.jvm
package opt

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.collection.JavaConverters._
import scala.tools.partest.ASMConverters._
import scala.tools.testing.BytecodeTesting
import scala.tools.testing.BytecodeTesting._

@RunWith(classOf[JUnit4])
class UnusedLocalVariablesTest extends BytecodeTesting {
  override def compilerArgs = "-Yopt:unreachable-code"
  import compiler._

  @Test
  def removeUnusedVar(): Unit = {
    val code = """def f(a: Long, b: String, c: Double): Unit = { return; var x = a; var y = x + 10 }"""
    assertLocalVarCount(code, 4) // `this, a, b, c`

    val code2 = """def f(): Unit = { var x = if (true) return else () }"""
    assertLocalVarCount(code2, 1) // x is eliminated, constant folding in scalac removes the if

    val code3 = """def f: Unit = return""" // paramless method
    assertLocalVarCount(code3, 1) // this
  }

  @Test
  def keepUsedVar(): Unit = {
    val code = """def f(a: Long, b: String, c: Double): Unit = { val x = 10 + a; val y = x + 10 }"""
    assertLocalVarCount(code, 6)

    val code2 = """def f(a: Long): Unit = { var x = if (a == 0l) return else () }"""
    assertLocalVarCount(code2, 3) // remains
  }

  @Test
  def constructorLocals(): Unit = {
    val code = """class C {
                 |  def this(a: Int) = {
                 |    this()
                 |    throw new Exception("")
                 |    val y = 0
                 |  }
                 |}
                 |""".stripMargin
    val cls = compileClass(code)
    val m = convertMethod(cls.methods.asScala.toList.find(_.desc == "(I)V").get)
    assertTrue(m.localVars.length == 2) // this, a, but not y


    val code2 =
      """class C {
        |  {
        |    throw new Exception("")
        |    val a = 0
        |  }
        |}
        |
        |object C {
        |  {
        |    throw new Exception("")
        |    val b = 1
        |  }
        |}
      """.stripMargin

    val List(cls2, companion2) = compileClasses(code2)

    assertTrue(getMethod(cls2, "<init>").localVars.length == 1) // this
    assertTrue(getMethod(companion2, "<init>").localVars.length == 1) // this
  }

  def assertLocalVarCount(code: String, numVars: Int): Unit = {
    assertTrue(compileMethod(code).localVars.length == numVars)
  }

}