Skip to content

Commit

Permalink
Merge pull request #13069 from dwijnand/patmat-no-check-all
Browse files Browse the repository at this point in the history
Remove excessive -Ycheck-all-patmat use in tests
  • Loading branch information
anatoliykmetyuk authored Jul 27, 2021
2 parents 9997e9c + e27c7d2 commit 2260aa2
Show file tree
Hide file tree
Showing 24 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@ import reporting.TestReporter
import dotty.tools.io.Directory

import java.io._
import java.nio.file.{Path => JPath}
import java.nio.file.{Files, Path => JPath}

import scala.io.Source._
import org.junit.Test

class PatmatDefaultExhaustivityTest extends PatmatExhaustivityTest {
override val testsDir = "tests/patmat-default"
override val options = super.options.filter(_ != "-Ycheck-all-patmat")
}

class PatmatExhaustivityTest {
val testsDir = "tests/patmat"
// stop-after: patmatexhaust-huge.scala crash compiler
def options = List("-pagewidth", "80", "-color:never", "-Ystop-after:explicitSelf", "-Ycheck-all-patmat", "-classpath", TestConfiguration.basicClasspath)
// pagewidth/color: for a stable diff as the defaults are based on the terminal (e.g size)
// stop-after: patmatexhaust-huge.scala crash compiler (but also hides other warnings..)
val options = List("-pagewidth", "80", "-color:never", "-Ystop-after:explicitSelf", "-classpath", TestConfiguration.basicClasspath)

private def compile(files: Seq[String]): Seq[String] = {
private def compile(files: List[JPath]): Seq[String] = {
val opts = toolArgsFor(files)
val stringBuffer = new StringWriter()
val printWriter = new PrintWriter(stringBuffer)
val reporter = TestReporter.simplifiedReporter(printWriter)

try {
Main.process((options ++ files).toArray, reporter, null)
Main.process((options ::: opts ::: files.map(_.toString)).toArray, reporter, null)
} catch {
case e: Throwable =>
e.printStackTrace(printWriter)
Expand All @@ -44,7 +41,7 @@ class PatmatExhaustivityTest {
}

private def compileFile(path: JPath): Boolean = {
val actualLines = compile(path.toString :: Nil)
val actualLines = compile(List(path))
val baseFilePath = path.toString.stripSuffix(".scala")
val checkFilePath = baseFilePath + ".check"

Expand All @@ -55,7 +52,7 @@ class PatmatExhaustivityTest {
private def compileDir(path: JPath): Boolean = {
val files = Directory(path).list.toList
.filter(f => f.extension == "scala" || f.extension == "java" )
.map(_.jpath.toString)
.map(_.jpath)

val actualLines = compile(files)
val checkFilePath = s"${path}${File.separator}expected.check"
Expand All @@ -67,12 +64,7 @@ class PatmatExhaustivityTest {
def patmatExhaustivity: Unit = {
val res = Directory(testsDir).list.toList
.filter(f => f.extension == "scala" || f.isDirectory)
.map { f =>
if (f.isDirectory)
compileDir(f.jpath)
else
compileFile(f.jpath)
}
.map(f => if f.isDirectory then compileDir(f.jpath) else compileFile(f.jpath))

val failed = res.filter(!_)
val ignored = Directory(testsDir).list.toList.filter(_.extension == "ignore")
Expand All @@ -83,4 +75,22 @@ class PatmatExhaustivityTest {

println(msg)
}

// inspect given files for tool args of the form `tool: args`
// if args string ends in close comment, drop the `*` `/`
// if split, parse the args string as command line.
// (from scala.tools.partest.nest.Runner#toolArgsFor)
private def toolArgsFor(files: List[JPath]): List[String] = {
import scala.jdk.OptionConverters._
import config.CommandLineParser.tokenize
files.flatMap { path =>
val tag = "scalac:"
val endc = "*" + "/" // be forgiving of /* scalac: ... */
def stripped(s: String) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
val args = scala.util.Using.resource(Files.lines(path, scala.io.Codec.UTF8.charSet))(
_.limit(10).filter(_.contains(tag)).map(stripped).findAny.toScala
)
args.map(tokenize).getOrElse(Nil)
}
}
}
4 changes: 2 additions & 2 deletions tests/patmat/i10174b.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
2: Pattern Match Exhaustivity: _: Int
58: Match case Unreachable
3: Pattern Match Exhaustivity: _: Int
59: Match case Unreachable
3 changes: 2 additions & 1 deletion tests/patmat/i10174b.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
def foo(x: Int): Unit =
x match {
case 1 =>
Expand Down Expand Up @@ -1001,4 +1002,4 @@ def foo(x: Int): Unit =
case 998 =>
case 999 =>
case 1000 =>
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/patmat/i6197d.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5: Pattern Match Exhaustivity: _: Array[String]
6: Pattern Match Exhaustivity: _: Array[String]
3 changes: 2 additions & 1 deletion tests/patmat/i6197d.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// scalac: -Ycheck-all-patmat
def foo(x: Array[String]) = x match {
case _: Array[_] =>
}

def bar(x: Array[String]) = x match {
case _: Array[_ <: Int] =>
}
}
2 changes: 1 addition & 1 deletion tests/patmat/i6255b.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2: Pattern Match Exhaustivity: _: Expr[Int]
3: Pattern Match Exhaustivity: _: Expr[Int]
1 change: 1 addition & 0 deletions tests/patmat/i6255b.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
class Foo {
def foo(x: quoted.Expr[Int])(using scala.quoted.Quotes): Unit = x match {
case '{ 1 } =>
Expand Down
6 changes: 3 additions & 3 deletions tests/patmat/patmat-indent.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
9: Pattern Match Exhaustivity: Nil
23: Pattern Match Exhaustivity: true, false
27: Pattern Match Exhaustivity: _: Int
10: Pattern Match Exhaustivity: Nil
24: Pattern Match Exhaustivity: true, false
28: Pattern Match Exhaustivity: _: Int
3 changes: 2 additions & 1 deletion tests/patmat/patmat-indent.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
object Test {
val Nil: scala.collection.immutable.Nil.type = scala.collection.immutable.Nil
val X = 5
Expand Down Expand Up @@ -27,4 +28,4 @@ object Test {
def foo3(x: Int) = x match {
case X => 0
}
}
}
8 changes: 4 additions & 4 deletions tests/patmat/t10502.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
5: Pattern Match Exhaustivity: Perhaps(None)
15: Pattern Match Exhaustivity: Nil
31: Pattern Match Exhaustivity: Multi(None, _)
44: Pattern Match Exhaustivity: Prod(None, _)
6: Pattern Match Exhaustivity: Perhaps(None)
16: Pattern Match Exhaustivity: Nil
32: Pattern Match Exhaustivity: Multi(None, _)
45: Pattern Match Exhaustivity: Prod(None, _)
1 change: 1 addition & 0 deletions tests/patmat/t10502.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
object Perhaps {

def unapply[A](oa: Option[A]): Some[Option[A]] = Some(oa)
Expand Down
2 changes: 1 addition & 1 deletion tests/patmat/t3163.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2: Pattern Match Exhaustivity: _: AnyVal
3: Pattern Match Exhaustivity: _: AnyVal
3 changes: 2 additions & 1 deletion tests/patmat/t3163.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
object Test {
def foo(x : AnyVal) = x match {case b : Boolean => "It's a bool"}
}
}
6 changes: 3 additions & 3 deletions tests/patmat/t4526.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
2: Pattern Match Exhaustivity: _: Int
7: Pattern Match Exhaustivity: (_, _)
12: Pattern Match Exhaustivity: (true, true), (false, false)
3: Pattern Match Exhaustivity: _: Int
8: Pattern Match Exhaustivity: (_, _)
13: Pattern Match Exhaustivity: (true, true), (false, false)
3 changes: 2 additions & 1 deletion tests/patmat/t4526.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
object Test{
def foo(a: Int) = a match {
case 5 => "Five!"
Expand All @@ -13,4 +14,4 @@ object Test{
case (true, false) => "tf"
case (false, true) => "ft"
}
}
}
4 changes: 2 additions & 2 deletions tests/patmat/t4661b.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
10: Pattern Match Exhaustivity: _: c.Foo
13: Match case Unreachable
11: Pattern Match Exhaustivity: _: c.Foo
14: Match case Unreachable
1 change: 1 addition & 0 deletions tests/patmat/t4661b.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
class C {
trait Foo
class One extends Foo
Expand Down
6 changes: 3 additions & 3 deletions tests/patmat/t9351.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
8: Pattern Match Exhaustivity: _: A
17: Pattern Match Exhaustivity: (_, _)
28: Pattern Match Exhaustivity: (_, _)
9: Pattern Match Exhaustivity: _: A
18: Pattern Match Exhaustivity: (_, _)
29: Pattern Match Exhaustivity: (_, _)
1 change: 1 addition & 0 deletions tests/patmat/t9351.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
trait A {}
case object B extends A {}
case object C extends A {}
Expand Down
4 changes: 2 additions & 2 deletions tests/patmat/t9809.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
3: Pattern Match Exhaustivity: (_, _)
7: Pattern Match Exhaustivity: (_, _)
4: Pattern Match Exhaustivity: (_, _)
8: Pattern Match Exhaustivity: (_, _)
1 change: 1 addition & 0 deletions tests/patmat/t9809.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// scalac: -Ycheck-all-patmat
object Example {
val op1: (Any, Any) => Unit = {
case (_, b: Int) =>
Expand Down

0 comments on commit 2260aa2

Please sign in to comment.