Scala Programming Language
  1. Scala Programming Language
  2. SI-4482

Failing guard in pattern match causes incorrect behavior of later matches

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0
    • Component/s: Pattern Matcher
    • Labels:
      None

      Description

      === What steps will reproduce the problem? ===
      Have two traits, one of which has an abstract method. Write a pattern-match expression, as below, where you first match trait A (with a pattern guard), then trait B, then trait A again. If you pass in an object that extends both A and B, and does NOT pass the boolean guard, the second match is skipped and the third match is selected. If you remove the first match (with the pattern guard) the behavior is as expected.

      Basically, the presence of one failing match is causing the following match to fail, which it shouldn't.

      Welcome to Scala version 2.9.0.RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala> {
           |   trait Foo  {
           |     def i: Int
           |   }
           |   trait Bar
           | 
           |   case class Spam(i: Int) extends Foo with Bar
           | 
           |   def matchParent(p:Any) = p match {
           |     case f:Foo if f.i == 1 => 1
           |     case _:Bar => 2
           |     case _:Foo => 3
           |   }
           | 
           |   matchParent(Spam(3))
           | }
      res0: Int = 3
      
      scala> {
           |   trait Foo  {
           |     def i: Int
           |   }
           |   trait Bar
           | 
           |   case class Spam(i: Int) extends Foo with Bar
           | 
           |   def matchParent(p:Any) = p match {
           |     case _:Bar => 2
           |     case _:Foo => 3
           |   }
           | 
           |   matchParent(Spam(3))
           | }
      res1: Int = 2
      

      === What is the expected behavior? ===
      Both of the above expressions should resolve to 2.

      === What do you see instead? ===
      The first expression resolves to 3, skipping the second match.

      === What versions of the following are you using? ===

      • Scala: 2.9.0.RC1 (also occurs in 2.8.1)
      • Java: 1.6.0_24
      • Operating system: OS X 10.6.7

        Activity

        Hide
        Stephen Judkins added a comment -

        Patch adding failing test

        Show
        Stephen Judkins added a comment - Patch adding failing test
        Hide
        Stephen Judkins added a comment -

        The problem seems to be introduced in the "explicitouter" compiler stage:

            def matchParent(p: Any): Int = {
              <synthetic> def gd2(x$$1: com.wtf.Main.Foo): Boolean = x$$1.i().==(1);
              {
                <synthetic> val temp5: Any = p;
                if (temp5.isInstanceOf[com.wtf.Main.Foo]())
                  {
                    if (gd2(temp5.asInstanceOf[com.wtf.Main.Foo]()))
                      {
                        1
                      }
                    else
                      {
                        3
                      }
                  }
                else
                  if (temp5.isInstanceOf[com.wtf.Main.Bar]())
                    {
                      2
                    }
                  else
                    throw new MatchError(temp5)
              }
            };
        

        It appears the generated code branches on whether it's an instance of "Foo" first, only branching based on the guard after it's determined it will match one of the "Foo" cases.

        Show
        Stephen Judkins added a comment - The problem seems to be introduced in the "explicitouter" compiler stage: def matchParent(p: Any): Int = { <synthetic> def gd2(x$$1: com.wtf.Main.Foo): Boolean = x$$1.i().==(1); { <synthetic> val temp5: Any = p; if (temp5.isInstanceOf[com.wtf.Main.Foo]()) { if (gd2(temp5.asInstanceOf[com.wtf.Main.Foo]())) { 1 } else { 3 } } else if (temp5.isInstanceOf[com.wtf.Main.Bar]()) { 2 } else throw new MatchError(temp5) } }; It appears the generated code branches on whether it's an instance of "Foo" first, only branching based on the guard after it's determined it will match one of the "Foo" cases.
        Hide
        Miguel Garcia added a comment -

        Yes, matchParent(Spam(3)) returns 3 when defined as

          def matchParent(p:Any) = p match {
            case f:Foo if f.i == 1 => 1
            case _:Bar => 2
            case _:Foo => 3
          }
        

        However when compiled with -Ypmat-naive it returns 2.

        The other option that comes to mind (-Yno-squeeze) makes compiled code return 3.

        Show
        Miguel Garcia added a comment - Yes, matchParent(Spam(3)) returns 3 when defined as def matchParent(p:Any) = p match { case f:Foo if f.i == 1 => 1 case _:Bar => 2 case _:Foo => 3 } However when compiled with -Ypmat-naive it returns 2 . The other option that comes to mind ( -Yno-squeeze ) makes compiled code return 3 .
        Show
        Simon Ochsenreither added a comment - Fixed in https://github.com/scala/scala/commit/4c04213b991596aa73dec3aa34cb8816a277f538

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Stephen Judkins
            TracCC:
            Alex Cruise, Daniel Sobral, Seth Tisue
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development