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

pattern matcher exhaustiveness warnings include impossible types

    Details

      Description

      scala> object Test {
        sealed abstract class BarrierDirection
      
        object BarrierDirection {
          case object Down extends BarrierDirection {
          }
      
          case object Up extends BarrierDirection {
          }
      
          (d1: BarrierDirection, d2: BarrierDirection) =>
            (d1, d2) match {
              case (Up, Up) | (Down, Down) => false
              case (Down, Up)              => true
              case (Up, Down)              => false
            }
        }
      }
      <console>:22: warning: match may not be exhaustive.
      It would fail on the following inputs: (BarrierDirection(), Down), (BarrierDirection(), Up), (Down, BarrierDirection()), (Down, BarrierDirection()), (Down, Down), (Down, Down), (Down, Down), (Down, Up), (Down, Up), (Up, BarrierDirection()), (Up, Down), (Up, Down)
                   (d1, d2) match {
                   ^
      
      scala> object Test {
        sealed abstract class BarrierDirection
      
        object BarrierDirection {
          case object Down extends BarrierDirection {
          }
      
          case object Up extends BarrierDirection {
          }
      
          (d1: BarrierDirection, d2: BarrierDirection) =>
            (d1) match {
              case Up | Down => false
            }
        }
      }
      <console>:22: warning: match may not be exhaustive.
      It would fail on the following inputs: Down, Up
                   (d1) match {
                    ^
      
      object Test {
        sealed abstract class BarrierDirection
      
        object BarrierDirection {
          case object Down extends BarrierDirection
      
          (d1: BarrierDirection, d2: BarrierDirection) =>
            (d1, d2) match {
              case (Down, Down)              => false
            }
        }
      }
      
      // <console>:17: warning: match may not be exhaustive.
      // It would fail on the following inputs: (Down, Down), (Down, Down), (Down, Down)
      //              (d1, d2) match {
      //              ^
      

        Issue Links

          Activity

          Hide
          Jason Zaugg added a comment - - edited

          By contrast, this is okay:

          scala>
          
          scala> object Test {
            sealed abstract class BarrierDirection
          
            object BarrierDirection {
              case object Down extends BarrierDirection {
              }
          
              case object Up extends BarrierDirection {
              }
          
            }
            import Test.BarrierDirection._
            (d1: BarrierDirection, d2: BarrierDirection) =>
              (d1, d2) match {
                case (Up, Up) | (Down, Down) => false
                case (Down, Up)              => true
                case (Up, Down)              => false
              }
          }
          defined module Test
          
          Show
          Jason Zaugg added a comment - - edited By contrast, this is okay: scala> scala> object Test { sealed abstract class BarrierDirection object BarrierDirection { case object Down extends BarrierDirection { } case object Up extends BarrierDirection { } } import Test.BarrierDirection._ (d1: BarrierDirection, d2: BarrierDirection) => (d1, d2) match { case (Up, Up) | (Down, Down) => false case (Down, Up) => true case (Up, Down) => false } } defined module Test
          Show
          Jason Zaugg added a comment - WIP https://github.com/retronym/scala/compare/ticket/7285
          Show
          Jason Zaugg added a comment - https://github.com/scala/scala/pull/2292

            People

            • Assignee:
              Jason Zaugg
              Reporter:
              Jason Zaugg
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development