Skip to content

Commit 3a06a2f

Browse files
Backport "Fix unreachable warning in deeply nested sealed hierarchy" to LTS (#20727)
Backports #18706 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 5258c3c + 9cd5fc2 commit 3a06a2f

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ object TypeOps:
883883
else if symbol.is(Module) then
884884
TermRef(this(tref.prefix), symbol.sourceModule)
885885
else if (prefixTVar != null)
886-
this(tref)
886+
this(tref.applyIfParameterized(tref.typeParams.map(_ => WildcardType)))
887887
else {
888888
prefixTVar = WildcardType // prevent recursive call from assigning it
889889
// e.g. tests/pos/i15029.more.scala, create a TypeVar for `Instances`' B, so we can disregard `Ints`

compiler/test/dotty/tools/dotc/reporting/TestReporter.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ extends Reporter with UniqueMessagePositions with HideNonSensicalMessages with M
3232
protected final val _consoleReporter = new ConsoleReporter(null, new PrintWriter(_consoleBuf))
3333
final def consoleOutput: String = _consoleBuf.toString
3434

35-
private var _didCrash = false
36-
final def compilerCrashed: Boolean = _didCrash
37-
3835
private var _skip: Boolean = false
3936
final def setSkip(): Unit = _skip = true
4037
final def skipped: Boolean = _skip
@@ -50,14 +47,6 @@ extends Reporter with UniqueMessagePositions with HideNonSensicalMessages with M
5047
def log(msg: String) =
5148
_messageBuf.append(msg)
5249

53-
def logStackTrace(thrown: Throwable): Unit = {
54-
_didCrash = true
55-
val sw = new java.io.StringWriter
56-
val pw = new java.io.PrintWriter(sw)
57-
thrown.printStackTrace(pw)
58-
log(sw.toString)
59-
}
60-
6150
/** Prints the message with the given position indication. */
6251
def printMessageAndPos(dia: Diagnostic, extra: String)(using Context): Unit = {
6352
val msg = messageAndPos(dia)

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
244244

245245
final def countErrors (reporters: Seq[TestReporter]) = countErrorsAndWarnings(reporters)._1
246246
final def countWarnings(reporters: Seq[TestReporter]) = countErrorsAndWarnings(reporters)._2
247-
final def reporterFailed(r: TestReporter) = r.compilerCrashed || r.errorCount > 0
247+
final def reporterFailed(r: TestReporter) = r.errorCount > 0
248248

249249
/**
250250
* For a given test source, returns a check file against which the result of the test run
@@ -733,22 +733,20 @@ trait ParallelTesting extends RunnerOrchestration { self =>
733733
lazy val (map, expCount) = getWarnMapAndExpectedCount(testSource.sourceFiles.toIndexedSeq)
734734
lazy val obtCount = reporters.foldLeft(0)(_ + _.warningCount)
735735
lazy val (expected, unexpected) = getMissingExpectedWarnings(map, reporters.iterator.flatMap(_.diagnostics))
736+
lazy val diagnostics = reporters.flatMap(_.diagnostics.toSeq.sortBy(_.pos.line).map(e => s" at ${e.pos.line + 1}: ${e.message}"))
737+
def showLines(title: String, lines: Seq[String]) = if lines.isEmpty then "" else title + lines.mkString("\n", "\n", "")
736738
def hasMissingAnnotations = expected.nonEmpty || unexpected.nonEmpty
737-
def showDiagnostics = "-> following the diagnostics:\n" +
738-
reporters.flatMap(_.diagnostics.toSeq.sortBy(_.pos.line).map(e => s"${e.pos.line + 1}: ${e.message}")).mkString(" at ", "\n at ", "")
739+
def showDiagnostics = showLines("-> following the diagnostics:", diagnostics)
739740
Option:
740-
if reporters.exists(_.compilerCrashed) then s"Compiler crashed when compiling: ${testSource.title}"
741-
else if reporters.exists(_.errorCount > 0) then
741+
if reporters.exists(_.errorCount > 0) then
742742
s"""Compilation failed for: ${testSource.title}
743743
|$showDiagnostics
744744
|""".stripMargin.trim.linesIterator.mkString("\n", "\n", "")
745-
else if obtCount == 0 then s"\nNo warnings found when compiling warn test $testSource"
746-
else if expCount == 0 then s"\nNo warning expected/defined in $testSource -- use // warn"
747745
else if expCount != obtCount then
748746
s"""|Wrong number of warnings encountered when compiling $testSource
749747
|expected: $expCount, actual: $obtCount
750-
|${expected.mkString("Unfulfilled expectations:\n", "\n", "")}
751-
|${unexpected.mkString("Unexpected warnings:\n", "\n", "")}
748+
|${showLines("Unfulfilled expectations:", expected)}
749+
|${showLines("Unexpected warnings:", unexpected)}
752750
|$showDiagnostics
753751
|""".stripMargin.trim.linesIterator.mkString("\n", "\n", "")
754752
else if hasMissingAnnotations then s"\nWarnings found on incorrect row numbers when compiling $testSource\n$showDiagnostics"
@@ -862,7 +860,6 @@ trait ParallelTesting extends RunnerOrchestration { self =>
862860
override def suppressErrors = true
863861

864862
override def maybeFailureMessage(testSource: TestSource, reporters: Seq[TestReporter]): Option[String] =
865-
def compilerCrashed = reporters.exists(_.compilerCrashed)
866863
lazy val (errorMap, expectedErrors) = getErrorMapAndExpectedCount(testSource.sourceFiles.toIndexedSeq)
867864
lazy val actualErrors = reporters.foldLeft(0)(_ + _.errorCount)
868865
lazy val (expected, unexpected) = getMissingExpectedErrors(errorMap, reporters.iterator.flatMap(_.errors))
@@ -871,8 +868,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
871868
reporters.flatMap(_.allErrors.sortBy(_.pos.line).map(e => s"${e.pos.line + 1}: ${e.message}")).mkString(" at ", "\n at ", "")
872869

873870
Option {
874-
if compilerCrashed then s"Compiler crashed when compiling: ${testSource.title}"
875-
else if actualErrors == 0 then s"\nNo errors found when compiling neg test $testSource"
871+
if actualErrors == 0 then s"\nNo errors found when compiling neg test $testSource"
876872
else if expectedErrors == 0 then s"\nNo errors expected/defined in $testSource -- use // error or // nopos-error"
877873
else if expectedErrors != actualErrors then
878874
s"""|Wrong number of errors encountered when compiling $testSource

tests/warn/i18661.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Jacket[T]:
2+
sealed trait BodyType:
3+
sealed trait OrganType:
4+
case class Heart() extends Body.Organ
5+
case class Brain() extends Body.Organ
6+
object Organ extends OrganType
7+
sealed trait Organ
8+
object Body extends BodyType
9+
sealed trait Body
10+
11+
type AnyJacket = Jacket[?]
12+
type AnyBodyOrgan = AnyJacket#BodyType#Organ
13+
type AnyBodyOrganHeart = AnyJacket#BodyType#OrganType#Heart
14+
type AnyBodyOrganBrain = AnyJacket#BodyType#OrganType#Brain
15+
16+
def check( asr : AnyBodyOrgan ) : String =
17+
asr match
18+
case c : AnyBodyOrganHeart => "Heart"
19+
case s : AnyBodyOrganBrain => "Brain" // was: unreachable
20+
21+
val jacket = new Jacket[Unit]
22+
val heart = new jacket.Body.Organ.Heart()
23+
val brain = new jacket.Body.Organ.Brain()
24+
25+
@main
26+
def go =
27+
println( check( heart ) )
28+
println( check( brain ) )

0 commit comments

Comments
 (0)