Skip to content

Commit

Permalink
Unsuppress unchecked warnings
Browse files Browse the repository at this point in the history
And check it with a pos checkfile.
  • Loading branch information
dwijnand committed Aug 9, 2023
1 parent c5adafc commit 6a62525
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 56 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ abstract class Message(val errorId: ErrorMessageID)(using Context) { self =>
override def canExplain = true

/** Override with `true` for messages that should always be shown even if their
* position overlaps another messsage of a different class. On the other hand
* position overlaps another message of a different class. On the other hand
* multiple messages of the same class with overlapping positions will lead
* to only a single message of that class to be issued.
*/
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,10 @@ extends Message(PatternMatchExhaustivityID) {
}
}

class UncheckedTypePattern(msgFn: => String)(using Context)
class UncheckedTypePattern(argType: Type, whyNot: String)(using Context)
extends PatternMatchMsg(UncheckedTypePatternID) {
def msg(using Context) = msgFn
override def showAlways = true
def msg(using Context) = i"the type test for $argType cannot be checked at runtime because $whyNot"
def explain(using Context) =
i"""|Type arguments and type refinements are erased during compile time, thus it's
|impossible to check them at run-time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ object TypeTestsCasts {
if !isTrusted && !isUnchecked then
val whyNot = whyUncheckable(expr.tpe, argType, tree.span)
if whyNot.nonEmpty then
report.uncheckedWarning(em"the type test for $argType cannot be checked at runtime because $whyNot", expr.srcPos)
report.uncheckedWarning(UncheckedTypePattern(argType, whyNot), expr.srcPos)
transformTypeTest(expr, argType,
flagUnrelated = enclosingInlineds.isEmpty) // if test comes from inlined code, dont't flag it even if it always false
}
Expand Down
25 changes: 15 additions & 10 deletions compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ trait ParallelTesting extends RunnerOrchestration { self =>
Try(testSource match {
case testSource @ JointCompilationSource(name, files, flags, outDir, fromTasty, decompilation) =>
val reporter =
if (fromTasty) compileFromTasty(flags, suppressErrors, outDir)
else compile(testSource.sourceFiles, flags, suppressErrors, outDir)
if (fromTasty) compileFromTasty(flags, outDir)
else compile(testSource.sourceFiles, flags, outDir)
List(reporter)

case testSource @ SeparateCompilationSource(_, dir, flags, outDir) =>
testSource.compilationGroups.map { (group, files) =>
if group.compiler.isEmpty then
compile(files, flags, suppressErrors, outDir)
compile(files, flags, outDir)
else
compileWithOtherCompiler(group.compiler, files, flags, outDir)
}
Expand Down Expand Up @@ -469,7 +469,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
registerCompletion()
throw e

protected def compile(files0: Array[JFile], flags0: TestFlags, suppressErrors: Boolean, targetDir: JFile): TestReporter = {
protected def compile(files0: Array[JFile], flags0: TestFlags, targetDir: JFile): TestReporter = {
import scala.util.Properties.*

def flattenFiles(f: JFile): Array[JFile] =
Expand Down Expand Up @@ -634,7 +634,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>

reporter

protected def compileFromTasty(flags0: TestFlags, suppressErrors: Boolean, targetDir: JFile): TestReporter = {
protected def compileFromTasty(flags0: TestFlags, targetDir: JFile): TestReporter = {
val tastyOutput = new JFile(targetDir.getPath + "_from-tasty")
tastyOutput.mkdir()
val flags = flags0 and ("-d", tastyOutput.getPath) and "-from-tasty"
Expand Down Expand Up @@ -715,7 +715,14 @@ trait ParallelTesting extends RunnerOrchestration { self =>
}

private final class PosTest(testSources: List[TestSource], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting)
extends Test(testSources, times, threadLimit, suppressAllOutput)
extends Test(testSources, times, threadLimit, suppressAllOutput):
override def suppressErrors = true

override def onSuccess(testSource: TestSource, reporters: Seq[TestReporter], logger: LoggedRunnable): Unit =
checkFile(testSource).foreach(diffTest(testSource, _, reporterOutputLines(reporters), reporters, logger))

def reporterOutputLines(reporters: Seq[TestReporter]): List[String] =
reporters.flatMap(_.consoleOutput.split("\n")).toList

private final class RewriteTest(testSources: List[TestSource], checkFiles: Map[JFile, JFile], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting)
extends Test(testSources, times, threadLimit, suppressAllOutput) {
Expand Down Expand Up @@ -1019,12 +1026,10 @@ trait ParallelTesting extends RunnerOrchestration { self =>

cleanup()

if (!shouldFail && test.didFail) {
if !shouldFail && test.didFail then
fail(s"Expected no errors when compiling, failed for the following reason(s):\n${reasonsForFailure(test)}\n")
}
else if (shouldFail && !test.didFail && test.skipCount == 0) {
else if shouldFail && !test.didFail && test.skipCount == 0 then
fail("Pos test should have failed, but didn't")
}

this
}
Expand Down
24 changes: 0 additions & 24 deletions tests/pending/neg/i16451.check

This file was deleted.

2 changes: 0 additions & 2 deletions tests/pos-deep-subtype/tuples23.check

This file was deleted.

72 changes: 72 additions & 0 deletions tests/pos/i16451.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/pos/i16451.scala:12:73 ---------------------------------------------
12 | def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match // error: inexhaustive
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: Wrapper(_)
|
| longer explanation available when compiling with `-explain`
-- [E030] Match case Unreachable Warning: tests/pos/i16451.scala:25:9 --------------------------------------------------
25 | case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| Unreachable case
-- [E030] Match case Unreachable Warning: tests/pos/i16451.scala:29:9 --------------------------------------------------
29 | case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| Unreachable case
-- [E030] Match case Unreachable Warning: tests/pos/i16451.scala:34:11 -------------------------------------------------
34 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| Unreachable case
-- [E030] Match case Unreachable Warning: tests/pos/i16451.scala:39:11 -------------------------------------------------
39 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| Unreachable case
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:13:9 -------------------------------------------------
13 | case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:14:9 -------------------------------------------------
14 | case x: Wrapper[Color.Green.type] => None // error: unchecked
| ^
|the type test for Wrapper[(Color.Green : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:21:9 -------------------------------------------------
21 | case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Any
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:22:9 -------------------------------------------------
22 | case x: Wrapper[Color.Green.type] => None // error: unchecked
| ^
|the type test for Wrapper[(Color.Green : Color)] cannot be checked at runtime because its type arguments can't be determined from Any
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:25:9 -------------------------------------------------
25 | case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:29:9 -------------------------------------------------
29 | case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from A1
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:34:11 ------------------------------------------------
34 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
|
| longer explanation available when compiling with `-explain`
-- [E092] Pattern Match Unchecked Warning: tests/pos/i16451.scala:39:11 ------------------------------------------------
39 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
| ^
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
|
| longer explanation available when compiling with `-explain`
28 changes: 16 additions & 12 deletions tests/pending/neg/i16451.scala → tests/pos/i16451.scala
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
// scalac: -Werror
//
enum Color:
case Red, Green
//sealed trait Color
//object Color:
// case object Red extends Color
// case object Green extends Color

case class Wrapper[A](value: A)

object Test:
def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match
case x: Wrapper[Color.Red.type] => Some(x) // error
case null => None
def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match // error: inexhaustive
case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
case x: Wrapper[Color.Green.type] => None // error: unchecked

def test_different(x: Wrapper[Color]): Option[Wrapper[Color]] = x match
case x @ Wrapper(_: Color.Red.type) => Some(x)
case x @ Wrapper(_: Color.Green.type) => None

def test_any(x: Any): Option[Wrapper[Color.Red.type]] = x match
case x: Wrapper[Color.Red.type] => Some(x) // error
case _ => None
case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
case x: Wrapper[Color.Green.type] => None // error: unchecked

def test_wrong(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match
case x: Wrapper[Color.Red.type] => Some(x) // error
case null => None
case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
case _ => None

def t2[A1 <: Wrapper[Color]](x: A1): Option[Wrapper[Color.Red.type]] = x match
case x: Wrapper[Color.Red.type] => Some(x) // error
case null => None
case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
case _ => None

def test_wrong_seq(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] =
xs.collect {
case x: Wrapper[Color.Red.type] => x // error
case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
}

def test_wrong_seq2(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] =
xs.collect { x => x match
case x: Wrapper[Color.Red.type] => x // error
case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
}

def main(args: Array[String]): Unit =
Expand Down
3 changes: 0 additions & 3 deletions tests/pos/opaque-inline2.check

This file was deleted.

1 change: 0 additions & 1 deletion tests/pos/t11593.check

This file was deleted.

0 comments on commit 6a62525

Please sign in to comment.