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

pattern matcher allows wrong number of subpatterns in extractor

    Details

      Description

      In addition to matching the contents of a tuple returned by `def unapply`, scala 2.10 also allows a single pattern matching the tuple itself. This is a regression from 2.9 in terms of pattern verification.

      Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala> object X{def unapply(s: String):Option[(Int,Int,Int)]=Some((1,2,3))}
      defined module X
      
      scala> "" match { case X(b) => b }
      <console>:9: error: wrong number of arguments for object X
                    "" match { case X(b) => b }
                                     ^
      <console>:9: error: not found: value b
                    "" match { case X(b) => b }
                                            ^
      
      Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala>  object X{def unapply(s: String):Option[(Int,Int,Int)]=Some((1,2,3))}
      defined module X
      
      scala> "" match { case X(b) => b }
      res0: (Int, Int, Int) = (1,2,3)
      

      This is problematic because there is no warning (except in certain lucky cases) if argument patterns are forgotten, resulting in a case statement which will never match, e.g. `case X(x: SomeTrait)`.

        Activity

        Hide
        Paul Phillips added a comment -

        Insufficient grounds according to me, back then. These days, I have no standards. Close away.

        Show
        Paul Phillips added a comment - Insufficient grounds according to me, back then. These days, I have no standards. Close away.
        Hide
        Paul Phillips added a comment -

        Since this is still assigned to me, I'm calling it fixed. If anyone wants to reopen please assume ownership of it.

        Show
        Paul Phillips added a comment - Since this is still assigned to me, I'm calling it fixed. If anyone wants to reopen please assume ownership of it.
        Hide
        Adriaan Moors added a comment -

        Since we failed to address this early enough in 2.11, the best I can do is to make this a deprecation warning. Marking as blocker.

        Show
        Adriaan Moors added a comment - Since we failed to address this early enough in 2.11, the best I can do is to make this a deprecation warning. Marking as blocker.
        Hide
        Roland Kuhn added a comment -

        A deprecation warning is sure better than nothing, +1 from me.

        Show
        Roland Kuhn added a comment - A deprecation warning is sure better than nothing, +1 from me.
        Hide
        Adriaan Moors added a comment -
        Show
        Adriaan Moors added a comment - Deprecation warning: https://github.com/scala/scala/pull/3564

          People

          • Assignee:
            Adriaan Moors
            Reporter:
            Roland Kuhn
          • Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development