Skip to content

Commit 4b27dc1

Browse files
authored
Merge pull request #8672 from dotty-staging/fix-error-if
Better error message for ifs that miss an else branch
2 parents 24672e6 + a6d4a36 commit 4b27dc1

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package typer
55
import ast._
66
import core._
77
import Types._, ProtoTypes._, Contexts._, Decorators._, Denotations._, Symbols._
8-
import Implicits._, Flags._
8+
import Implicits._, Flags._, Constants.Constant
99
import util.Spans._
1010
import util.SourcePosition
1111
import java.util.regex.Matcher.quoteReplacement
@@ -98,7 +98,11 @@ object ErrorReporting {
9898
val normTp = normalize(tree.tpe, pt)
9999
val treeTp = if (normTp <:< pt) tree.tpe else normTp
100100
// use normalized type if that also shows an error, original type otherwise
101-
errorTree(tree, TypeMismatch(treeTp, pt, implicitFailure.whyNoConversion))
101+
def missingElse = tree match
102+
case If(_, _, elsep @ Literal(Constant(()))) if elsep.span.isSynthetic =>
103+
"\nMaybe you are missing an else part for the conditional?"
104+
case _ => ""
105+
errorTree(tree, TypeMismatch(treeTp, pt, implicitFailure.whyNoConversion, missingElse))
102106
}
103107

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

tests/neg/if-error.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E007] Type Mismatch Error: tests/neg/if-error.scala:2:2 ------------------------------------------------------------
2+
2 | if ??? then System.in.read() // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| Found: Unit
5+
| Required: Int
6+
| Maybe you are missing an else part for the conditional?

tests/neg/if-error.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val x: Int =
2+
if ??? then System.in.read() // error

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)