Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary tree generated by PatternMatcher #5054

Closed
allanrenucci opened this issue Aug 28, 2018 · 0 comments
Closed

Unnecessary tree generated by PatternMatcher #5054

allanrenucci opened this issue Aug 28, 2018 · 0 comments

Comments

@allanrenucci
Copy link
Contributor

While compiling something like:

class Test {
  def test(s: String) = s match {
    case "Hello" => 1
    case _       => 2
  }
}

after PatternMatcher, we get a tree of the form:

class Test {
  def test(s: String): Int =
    matchResult1[Int]:
      {
        case val x1: String = s
        if "Hello".==(x1) then return[matchResult1] 1 else ()
        return[matchResult1] 2
        return[matchResult1] throw new MatchError(x1)
      }
}

It seems unnecessary to generate the tree return[matchResult1] throw new MatchError(x1).

Before the labelled block PR:

class Test {
  def test(s: String): Int = {
    case val x1: String(s) = s
    if "Hello".==(x1) then 1 else 2
  }
}

Also unnecessary bytecode seems to be generated:

Compiled from "Test.scala"
public class Test {
  public int test(java.lang.String);
    Code:
       0: aload_1
       1: astore_2
       2: ldc           #15                 // String Hello
       4: aload_2
       5: invokevirtual #19                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
       8: ifeq          18
      11: iconst_1
      12: goto          34
      15: nop
      16: nop
      17: athrow
      18: iconst_2
      19: goto          34
      22: nop
      23: nop
      24: nop
      25: nop
      26: nop
      27: nop
      28: nop
      29: nop
      30: nop
      31: nop
      32: nop
      33: athrow
      34: ireturn

Before the labelled block PR:

Compiled from "Test.scala"
public class Test {
  public int test(java.lang.String);
    Code:
       0: aload_1
       1: astore_2
       2: ldc           #15                 // String Hello
       4: aload_2
       5: invokevirtual #19                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
       8: ifeq          15
      11: iconst_1
      12: goto          16
      15: iconst_2
      16: ireturn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants