From 6a440b960c00c01f3653385417a246e359d82e01 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Fri, 2 Feb 2007 10:29:43 +0000 Subject: matching: + exhaustivity check, warnings Iterator: gets mkString method Iterable: only whitespace Definitions: value classes no longer SEALED test cases for exhaustivity + unapply/array --- test/files/neg/patmatexhaust.check | 20 +++++++++++++ test/files/neg/patmatexhaust.scala | 59 ++++++++++++++++++++++++++++++++++++++ test/files/run/unapplyArray.scala | 31 ++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 test/files/neg/patmatexhaust.check create mode 100644 test/files/neg/patmatexhaust.scala create mode 100644 test/files/run/unapplyArray.scala (limited to 'test') diff --git a/test/files/neg/patmatexhaust.check b/test/files/neg/patmatexhaust.check new file mode 100644 index 0000000000..40054bad9f --- /dev/null +++ b/test/files/neg/patmatexhaust.check @@ -0,0 +1,20 @@ +patmatexhaust.scala:8: warning: does not cover case {object Baz} + def ma1(x:Foo) = x match { + ^ +patmatexhaust.scala:12: warning: does not cover case {class Bar} + def ma2(x:Foo) = x match { + ^ +patmatexhaust.scala:24: warning: does not cover case {class Kult} + case {Kult(_), Qult()} => // Kult missing + ^ +patmatexhaust.scala:26: warning: does not cover case {class Qult} + case {Qult(), Kult(_)} => // Qult missing + ^ +patmatexhaust.scala:45: warning: does not cover case {object Gu} + def ma4(x:Deep) = x match { // missing cases: Gu + ^ +patmatexhaust.scala:57: error: unreachable code + case 1 => + ^ +5 warnings found +one error found diff --git a/test/files/neg/patmatexhaust.scala b/test/files/neg/patmatexhaust.scala new file mode 100644 index 0000000000..0416749b88 --- /dev/null +++ b/test/files/neg/patmatexhaust.scala @@ -0,0 +1,59 @@ +class TestSealedExhaustive { // compile only + + sealed class Foo + + case class Bar(x:Int) extends Foo + case object Baz extends Foo + + def ma1(x:Foo) = x match { + case Bar(_) => // not exhaustive + } + + def ma2(x:Foo) = x match { + case Baz => // not exhaustive + } + + sealed class Mult + case class Kult(s:Mult) extends Mult + case class Qult() extends Mult + + def ma33(x:Kult) = x match { // exhaustive + case Kult(_) => // exhaustive + } + def ma3(x:Mult) = {x,x} match { // not exhaustive + case {Kult(_), Qult()} => // Kult missing + //case {Kult(_), Kult(_)} => + case {Qult(), Kult(_)} => // Qult missing + //case {Qult(), Qult()} => + } + + + sealed class Deep + + case object Ga extends Deep + sealed class Gp extends Deep + case object Gu extends Gp + + def zma3(x:Deep) = x match { // exhaustive! + case _ => + } + def zma4(x:Deep) = x match { // exhaustive! + case Ga => + case _ => + } + + def ma4(x:Deep) = x match { // missing cases: Gu + case Ga => + } + + def zma5(x:Deep) = x match { // exhaustive + case Gu => + case _ if 1 == 0 => + case Ga => + } + + def redundant = 1 match { // include this otherwise script won't test this in files/neg + case 1 => + case 1 => + } +} diff --git a/test/files/run/unapplyArray.scala b/test/files/run/unapplyArray.scala new file mode 100644 index 0000000000..bf6582dadf --- /dev/null +++ b/test/files/run/unapplyArray.scala @@ -0,0 +1,31 @@ +object Test { + def main(args:Array[String]): Unit = { + val z = Array(1,2,3,4) + val zs: Seq[int] = z + val za: Any = z + +/* + Console.println("z is arr[int]"+z.isInstanceOf[Array[int]]) + Console.println("zs is arr[int]"+zs.isInstanceOf[Array[int]]) + Console.println("za is arr[int]"+ za.isInstanceOf[Array[int]]) + + Console.println("z is seq[int]"+z.isInstanceOf[Seq[int]]) + Console.println("zs is seq[int]"+zs.isInstanceOf[Seq[int]]) + Console.println("za is seq[int]"+ za.isInstanceOf[Seq[int]]) + + Console.println("z is anyref"+z.isInstanceOf[AnyRef]) + + Console.println("z useq "+ Seq.unapplySeq(z)) + Console.println("zs useq "+ Seq.unapplySeq(zs)) + Console.println("za useq "+ Seq.unapplySeq(za)) + + Console.println("z aseq "+ Seq.unapplySeq(z)) + Console.println("zs aseq "+ Seq.unapplySeq(zs)) + Console.println("za aseq "+ Seq.unapplySeq(za)) +*/ + val zl = zs match { + case Seq(xs@_*) => xs.length + } + assert(zl == 4) + } +} -- cgit v1.2.3