Skip to content

pattern matcher allows wrong number of subpatterns in extractor #6675

Closed
@scabug

Description

@scabug

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).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions