Skip to content

Commit a6d4a36

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 7c23027 commit a6d4a36

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-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*)(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-
def 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` */

tests/run/byname-varargs.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a
2+
b
3+
a
4+
b
5+
result = (ArraySeq(1, 2),ArraySeq(1, 2))

tests/run/byname-varargs.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def f[T](xs: => T*): (Seq[T], Seq[T]) = (xs, xs)
2+
3+
def a =
4+
println("a")
5+
1
6+
7+
def b =
8+
println("b")
9+
2
10+
11+
@main def Test =
12+
println(s"result = ${f(a, b)}")

0 commit comments

Comments
 (0)