From 016d815104b1d44b2b899c68804dde500963fbcd Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 12 May 2010 21:17:29 +0000 Subject: Overhauled sequence length logic in the pattern... Overhauled sequence length logic in the pattern matcher. Removes unnecessary boxing and a few varieties of wrongness. Closes #3395, #3150, #2958, #2945, #2187. No review. --- test/files/pos/bug2187-2.scala | 7 +++++++ test/files/pos/bug2945.scala | 12 ++++++++++++ test/files/run/bug2958.scala | 16 +++++++++++++++ test/files/run/bug3150.scala | 10 ++++++++++ test/files/run/bug3395.check | 2 ++ test/files/run/bug3395.scala | 13 +++++++++++++ test/files/run/patmat-seqs.check | 13 +++++++++++++ test/files/run/patmat-seqs.scala | 42 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 115 insertions(+) create mode 100644 test/files/pos/bug2187-2.scala create mode 100644 test/files/pos/bug2945.scala create mode 100644 test/files/run/bug2958.scala create mode 100644 test/files/run/bug3150.scala create mode 100644 test/files/run/bug3395.check create mode 100644 test/files/run/bug3395.scala create mode 100644 test/files/run/patmat-seqs.check create mode 100644 test/files/run/patmat-seqs.scala (limited to 'test') diff --git a/test/files/pos/bug2187-2.scala b/test/files/pos/bug2187-2.scala new file mode 100644 index 0000000000..3f2742dd89 --- /dev/null +++ b/test/files/pos/bug2187-2.scala @@ -0,0 +1,7 @@ +class Test { + def test[A](list: List[A]) = list match { + case Seq(x, y) => "xy" + case Seq(x) => "x" + case _ => "something else" + } +} \ No newline at end of file diff --git a/test/files/pos/bug2945.scala b/test/files/pos/bug2945.scala new file mode 100644 index 0000000000..762bdb61e1 --- /dev/null +++ b/test/files/pos/bug2945.scala @@ -0,0 +1,12 @@ +object Foo { + def test(s: String) = { + (s: Seq[Char]) match { + case Seq('f', 'o', 'o', ' ', rest1 @ _*) => + rest1 + case Seq('b', 'a', 'r', ' ', ' ', rest2 @ _*) => + rest2 + case _ => + s + } + } +} \ No newline at end of file diff --git a/test/files/run/bug2958.scala b/test/files/run/bug2958.scala new file mode 100644 index 0000000000..dcd24ecc36 --- /dev/null +++ b/test/files/run/bug2958.scala @@ -0,0 +1,16 @@ +object Test { + def f(args: Array[String]) = args match { + case Array("-p", prefix, from, to) => + prefix + from + to + + case Array(from, to) => + from + to + + case _ => + "default" + } + + def main(args: Array[String]) { + assert(f(Array("1", "2")) == "12") + } +} \ No newline at end of file diff --git a/test/files/run/bug3150.scala b/test/files/run/bug3150.scala new file mode 100644 index 0000000000..034703b5f7 --- /dev/null +++ b/test/files/run/bug3150.scala @@ -0,0 +1,10 @@ +object Test { + case object Bob { override def equals(other: Any) = true } + def f(x: Any) = x match { case Bob => Bob } + + def main(args: Array[String]): Unit = { + assert(f(Bob) eq Bob) + assert(f(0) eq Bob) + assert(f(Nil) eq Bob) + } +} diff --git a/test/files/run/bug3395.check b/test/files/run/bug3395.check new file mode 100644 index 0000000000..5f5521fae2 --- /dev/null +++ b/test/files/run/bug3395.check @@ -0,0 +1,2 @@ +abc +def diff --git a/test/files/run/bug3395.scala b/test/files/run/bug3395.scala new file mode 100644 index 0000000000..b4990a1716 --- /dev/null +++ b/test/files/run/bug3395.scala @@ -0,0 +1,13 @@ +object Test { + def main(args: Array[String]): Unit = { + Seq("") match { + case Seq("") => println("abc") + case Seq(_, _, x) => println(x) + } + + Seq(1, 2, "def") match { + case Seq("") => println("abc") + case Seq(_, _, x) => println(x) + } + } +} \ No newline at end of file diff --git a/test/files/run/patmat-seqs.check b/test/files/run/patmat-seqs.check new file mode 100644 index 0000000000..bb2a5ee44a --- /dev/null +++ b/test/files/run/patmat-seqs.check @@ -0,0 +1,13 @@ +s3 +s2 +s1 +s0 +ss6 +d +s3 +s3 +d +s1 +s3 +d +d diff --git a/test/files/run/patmat-seqs.scala b/test/files/run/patmat-seqs.scala new file mode 100644 index 0000000000..b5c47b4b4b --- /dev/null +++ b/test/files/run/patmat-seqs.scala @@ -0,0 +1,42 @@ +object Test { + def f1(x: Any) = x match { + case Seq(1, 2, 3) => "s3" + case Seq(4, 5) => "s2" + case Seq(7) => "s1" + case Nil => "s0" + case Seq(_, _, _, _, _, x: String) => "ss6" + case _ => "d" + } + + def f2(x: Any) = x match { + case Seq("a", "b", _*) => "s2" + case Seq(1, _*) => "s1" + case Seq(5, 6, 7, _*) => "s3" + case _ => "d" + } + + def main(args: Array[String]): Unit = { + val xs1 = List( + List(1,2,3), + List(4,5), + Vector(7), + Seq(), + Seq(1, 2, 3, 4, 5, "abcd"), + "abc" + ) map f1 + + xs1 foreach println + + val xs2 = List( + Seq(5, 6, 7), + Seq(5, 6, 7, 8, 9), + Seq("a"), + Seq(1, 6, 7), + List(5, 6, 7), + Nil, + 5 + ) map f2 + + xs2 foreach println + } +} -- cgit v1.2.3