Skip to content

Commit 90df196

Browse files
committed
Allow several addenda for TypeMismatch
It's cleaner to handle the choice what addendum to print in the message itself.
1 parent 2328fdf commit 90df196

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ object messages {
241241
}
242242
}
243243

244-
class TypeMismatch(found: Type, expected: Type, addendum: => String = "")(implicit ctx: Context)
244+
class TypeMismatch(found: Type, expected: Type, addenda: => String* = Nil)(implicit ctx: Context)
245245
extends TypeMismatchMsg(TypeMismatchID):
246246

247247
// replace constrained TypeParamRefs and their typevars by their bounds where possible
@@ -269,14 +269,15 @@ object messages {
269269
val expected1 = reported(expected)
270270
val (found2, expected2) =
271271
if (found1 frozen_<:< expected1) (found, expected) else (found1, expected1)
272-
val postScript =
273-
if !addendum.isEmpty
274-
|| expected.isAny
275-
|| expected.isAnyRef
276-
|| expected.isRef(defn.AnyValClass)
277-
|| defn.isBottomType(found)
278-
then addendum
279-
else ctx.typer.importSuggestionAddendum(ViewProto(found.widen, expected))
272+
val postscript = addenda.find(!_.isEmpty) match
273+
case Some(p) => p
274+
case None =>
275+
if expected.isAny
276+
|| expected.isAnyRef
277+
|| expected.isRef(defn.AnyValClass)
278+
|| defn.isBottomType(found)
279+
then ""
280+
else ctx.typer.importSuggestionAddendum(ViewProto(found.widen, expected))
280281
val (where, printCtx) = Formatting.disambiguateTypes(found2, expected2)
281282
val whereSuffix = if (where.isEmpty) where else s"\n\n$where"
282283
val (foundStr, expectedStr) = Formatting.typeDiff(found2, expected2)(printCtx)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ object ErrorReporting {
102102
case If(_, _, elsep @ Literal(Constant(()))) if elsep.span.isSynthetic =>
103103
"\nMaybe you are missing an else part for the conditional?"
104104
case _ => ""
105-
val addendum = List(implicitFailure.whyNoConversion, missingElse)
106-
.find(!_.isEmpty).getOrElse("")
107-
errorTree(tree, TypeMismatch(treeTp, pt, addendum))
105+
errorTree(tree, TypeMismatch(treeTp, pt, implicitFailure.whyNoConversion, missingElse))
108106
}
109107

110108
/** A subtype log explaining why `found` does not conform to `expected` */

0 commit comments

Comments
 (0)