Skip to content

Commit 6defc38

Browse files
committed
Add better explanation to error message
Fixes #18657
1 parent 48bb59c commit 6defc38

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ extends Message(PatternMatchExhaustivityID) {
876876

877877
val pathes = List(
878878
ActionPatch(
879-
srcPos = endPos,
879+
srcPos = endPos,
880880
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
881881
.mkString("\n", "\n", "")
882882
),
@@ -1806,10 +1806,20 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
18061806
| - a reference to `this`, or
18071807
| - a selection of an immutable path with an immutable value."""
18081808

1809-
class WrongNumberOfParameters(expected: Int)(using Context)
1809+
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
18101810
extends SyntaxMsg(WrongNumberOfParametersID) {
1811-
def msg(using Context) = s"Wrong number of parameters, expected: $expected"
1812-
def explain(using Context) = ""
1811+
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"
1812+
def explain(using Context) =
1813+
val ending = if foundCount == 1 then "" else "s"
1814+
i"""The function literal
1815+
|
1816+
| $tree
1817+
|
1818+
|has $foundCount parameter$ending. But the expected type
1819+
|
1820+
| $pt
1821+
|
1822+
|requires a function with $expectedCount parameters."""
18131823
}
18141824

18151825
class DuplicatePrivateProtectedQualifier()(using Context)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15741574
/** Returns the type and whether the parameter is erased */
15751575
def protoFormal(i: Int): (Type, Boolean) =
15761576
if (protoFormals.length == params.length) (protoFormals(i), isDefinedErased(i))
1577-
else (errorType(WrongNumberOfParameters(protoFormals.length), tree.srcPos), false)
1577+
else (errorType(WrongNumberOfParameters(tree, params.length, pt, protoFormals.length), tree.srcPos), false)
15781578

15791579
/** Is `formal` a product type which is elementwise compatible with `params`? */
15801580
def ptIsCorrectProduct(formal: Type) =

tests/neg/i18657.check

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- [E086] Syntax Error: tests/neg/i18657.scala:2:27 --------------------------------------------------------------------
2+
2 |val f: (Int, Int) => Int = Integer.compare(_ + 1, _) // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| Wrong number of parameters, expected: 2
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| The function literal
9+
|
10+
| _$2 => Integer.compare(_$1 => _$1 + 1, _$2)
11+
|
12+
| has 1 parameter. But the expected type
13+
|
14+
| (Int, Int) => Int
15+
|
16+
| requires a function with 2 parameters.
17+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i18657.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//> using options -explain
2+
val f: (Int, Int) => Int = Integer.compare(_ + 1, _) // error

0 commit comments

Comments
 (0)