Skip to content

Unnecessary tree generated by PatternMatcher #5054

Closed
@allanrenucci

Description

@allanrenucci

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
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions