Skip to content

Commit bd43e99

Browse files
committed
Show match type reduction failures for other errors
Also show match type reduction failures for implicit search errors and member not found errors.
1 parent 1857dc9 commit bd43e99

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import printing.Formatting
1616
import ErrorMessageID._
1717
import ast.Trees
1818
import config.{Feature, ScalaVersion}
19-
import typer.ErrorReporting.err
19+
import typer.ErrorReporting.{err, matchReductionAddendum}
2020
import typer.ProtoTypes.ViewProto
2121
import scala.util.control.NonFatal
2222
import StdNames.nme
@@ -45,22 +45,15 @@ import transform.SymUtils._
4545
abstract class TypeMsg(errorId: ErrorMessageID) extends Message(errorId):
4646
def kind = "Type"
4747

48-
abstract class TypeMismatchMsg(found: Type, expected: Type)(errorId: ErrorMessageID)(using Context) extends Message(errorId):
48+
trait ShowMatchTrace(tps: Type*)(using Context) extends Message:
49+
override def msgSuffix: String = matchReductionAddendum(tps*)
50+
51+
abstract class TypeMismatchMsg(found: Type, expected: Type)(errorId: ErrorMessageID)(using Context)
52+
extends Message(errorId), ShowMatchTrace(found, expected):
4953
def kind = "Type Mismatch"
5054
def explain = err.whyNoMatchStr(found, expected)
5155
override def canExplain = true
5256

53-
override def msgSuffix: String =
54-
val collectMatchTrace = new TypeAccumulator[String]:
55-
def apply(s: String, tp: Type): String =
56-
if s.nonEmpty then s
57-
else tp match
58-
case tp: AppliedType if tp.isMatchAlias => MatchTypeTrace.record(tp.tryNormalize)
59-
case tp: MatchType => MatchTypeTrace.record(tp.tryNormalize)
60-
case _ => foldOver(s, tp)
61-
collectMatchTrace(collectMatchTrace("", found), expected)
62-
end TypeMismatchMsg
63-
6457
abstract class NamingMsg(errorId: ErrorMessageID) extends Message(errorId):
6558
def kind = "Naming"
6659

@@ -292,7 +285,7 @@ import transform.SymUtils._
292285
end TypeMismatch
293286

294287
class NotAMember(site: Type, val name: Name, selected: String, addendum: => String = "")(using Context)
295-
extends NotFoundMsg(NotAMemberID) {
288+
extends NotFoundMsg(NotAMemberID), ShowMatchTrace(site) {
296289
//println(i"site = $site, decls = ${site.decls}, source = ${site.typeSymbol.sourceFile}") //DEBUG
297290

298291
def msg = {

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ object ErrorReporting {
5151
case _ =>
5252
report.error(em"missing arguments for $meth", tree.srcPos)
5353

54+
def matchReductionAddendum(tps: Type*)(using Context): String =
55+
val collectMatchTrace = new TypeAccumulator[String]:
56+
def apply(s: String, tp: Type): String =
57+
if s.nonEmpty then s
58+
else tp match
59+
case tp: AppliedType if tp.isMatchAlias => MatchTypeTrace.record(tp.tryNormalize)
60+
case tp: MatchType => MatchTypeTrace.record(tp.tryNormalize)
61+
case _ => foldOver(s, tp)
62+
tps.foldLeft("")(collectMatchTrace)
63+
5464
class Errors(using Context) {
5565

5666
/** An explanatory note to be added to error messages
@@ -253,7 +263,9 @@ class ImplicitSearchError(
253263
val shortMessage = userDefinedImplicitNotFoundParamMessage
254264
.orElse(userDefinedImplicitNotFoundTypeMessage)
255265
.getOrElse(defaultImplicitNotFoundMessage)
256-
formatMsg(shortMessage)() ++ hiddenImplicitsAddendum
266+
formatMsg(shortMessage)()
267+
++ hiddenImplicitsAddendum
268+
++ ErrorReporting.matchReductionAddendum(pt)
257269
}
258270

259271
private def formatMsg(shortForm: String)(headline: String = shortForm) = arg match {

tests/neg/i12049.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ type Reverse[X <: Tuple] = X match
2020
val z1: (B, A) = ??? : Reverse[(A, B)]
2121
val z2: (B, A, B, A) = ??? : Reverse[(A, B, A, B)]
2222
val z3: (A, B, A) = ??? : Reverse[(A, B, A)] // error
23+
24+
val _ = summon[M[B]]
25+
val _ = summon[String =:= Last[Int *: Int *: Boolean *: String *: EmptyTuple]]
26+
val _ = summon[(A, B, A) =:= Reverse[(A, B, A)]]
27+
28+
val _ = (??? : M[B]).length
29+

0 commit comments

Comments
 (0)