diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-23 14:14:24 +0200 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-23 17:42:13 +0200 |
commit | ee5721e864de6fff2d54b9fb5452123bcca82483 (patch) | |
tree | 4d40dfff5f13984badf035d090bfcd6fea235ab0 /test | |
parent | 7b62eeea86c62f5e068c366065f8f4c2e6624eb7 (diff) | |
download | scala-ee5721e864de6fff2d54b9fb5452123bcca82483.tar.gz scala-ee5721e864de6fff2d54b9fb5452123bcca82483.tar.bz2 scala-ee5721e864de6fff2d54b9fb5452123bcca82483.zip |
SI-6111 accept single-subpattern unapply pattern
An extractor pattern `X(p)` should type check for any `X.unapply`/`X.unapplySeq`
that returns an `Option[_]` -- previously we were confused about the case where
it was an `Option[(T1, ... , Tn)]`. In this case, the expected type for the
pattern `p` is simply `(T1, ... , Tn)`.
While I was at it, tried to clean up unapplyTypeList and friends (by replacing them by extractorFormalTypes).
From the spec:
8.1.8 ExtractorPatterns
An extractor pattern x(p1, ..., pn) where n ≥ 0 is of the same syntactic form as a constructor pattern.
However, instead of a case class, the stable identifier x denotes an object which has a member method named unapply or unapplySeq that matches the pattern.
An unapply method in an object x matches the pattern x(p1, ..., pn) if it takes exactly one argument and one of the following applies:
n = 0 and unapply’s result type is Boolean.
n = 1 and unapply’s result type is Option[T], for some type T.
the (only) argument pattern p1 is typed in turn with expected type T
n > 1 and unapply’s result type is Option[(T1, ..., Tn)], for some types T1, ..., Tn.
the argument patterns p1, ..., pn are typed in turn with expected types T1, ..., Tn
Diffstat (limited to 'test')
-rw-r--r-- | test/files/neg/t997.check | 8 | ||||
-rw-r--r-- | test/files/neg/t997.scala | 2 | ||||
-rw-r--r-- | test/files/run/t6111.check | 2 | ||||
-rw-r--r-- | test/files/run/t6111.scala | 26 |
4 files changed, 30 insertions, 8 deletions
diff --git a/test/files/neg/t997.check b/test/files/neg/t997.check index c9fe0de756..186095f44a 100644 --- a/test/files/neg/t997.check +++ b/test/files/neg/t997.check @@ -1,13 +1,7 @@ -t997.scala:7: error: wrong number of arguments for object Foo -"x" match { case Foo(a) => Console.println(a) } - ^ -t997.scala:7: error: not found: value a -"x" match { case Foo(a) => Console.println(a) } - ^ t997.scala:13: error: wrong number of arguments for object Foo "x" match { case Foo(a, b, c) => Console.println((a,b,c)) } ^ t997.scala:13: error: not found: value a "x" match { case Foo(a, b, c) => Console.println((a,b,c)) } ^ -four errors found +two errors found diff --git a/test/files/neg/t997.scala b/test/files/neg/t997.scala index 42b46174d6..e8d10f4317 100644 --- a/test/files/neg/t997.scala +++ b/test/files/neg/t997.scala @@ -3,7 +3,7 @@ object Foo { def unapply(x : String) = Some(Pair(x, x)) } object Test extends App { -// Prints 'x'; ought not to compile (or maybe a should be the Pair?). +// Prints '(x, x)'. Should compile as per SI-6111. "x" match { case Foo(a) => Console.println(a) } // Prints '(x,x)' as expected. diff --git a/test/files/run/t6111.check b/test/files/run/t6111.check new file mode 100644 index 0000000000..7fd2e33526 --- /dev/null +++ b/test/files/run/t6111.check @@ -0,0 +1,2 @@ +(8,8) +(x,x) diff --git a/test/files/run/t6111.scala b/test/files/run/t6111.scala new file mode 100644 index 0000000000..7cceea1d09 --- /dev/null +++ b/test/files/run/t6111.scala @@ -0,0 +1,26 @@ +// slightly overkill, but a good test case for implicit resolution in extractor calls, +// along with the real fix: an extractor pattern with 1 sub-pattern should type check for all extractors +// that return Option[T], whatever T (even if it's a tuple) +object Foo { + def unapply[S, T](scrutinee: S)(implicit witness: FooHasType[S, T]): Option[T] = scrutinee match { + case i: Int => Some((i, i).asInstanceOf[T]) + } +} + +class FooHasType[S, T] +object FooHasType { + implicit object int extends FooHasType[Int, (Int, Int)] +} + +// resurrected from neg/t997 +object Foo997 { def unapply(x : String): Option[(String, String)] = Some((x, x)) } + +object Test extends App { + val x = 8 + println(x match { + case Foo(p) => p // p should be a pair of Int + }) + + // Prints '(x, x)' + "x" match { case Foo997(a) => println(a) } +}
\ No newline at end of file |