Skip to content

Commit 9fccb7e

Browse files
authored
Add better explanation to error message (#18665)
Fixes #18657
2 parents 6b8b02d + 6defc38 commit 9fccb7e

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,10 +1811,20 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
18111811
| - a reference to `this`, or
18121812
| - a selection of an immutable path with an immutable value."""
18131813

1814-
class WrongNumberOfParameters(expected: Int)(using Context)
1814+
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
18151815
extends SyntaxMsg(WrongNumberOfParametersID) {
1816-
def msg(using Context) = s"Wrong number of parameters, expected: $expected"
1817-
def explain(using Context) = ""
1816+
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"
1817+
def explain(using Context) =
1818+
val ending = if foundCount == 1 then "" else "s"
1819+
i"""The function literal
1820+
|
1821+
| $tree
1822+
|
1823+
|has $foundCount parameter$ending. But the expected type
1824+
|
1825+
| $pt
1826+
|
1827+
|requires a function with $expectedCount parameters."""
18181828
}
18191829

18201830
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
@@ -1586,7 +1586,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15861586
/** Returns the type and whether the parameter is erased */
15871587
def protoFormal(i: Int): (Type, Boolean) =
15881588
if (protoFormals.length == params.length) (protoFormals(i), isDefinedErased(i))
1589-
else (errorType(WrongNumberOfParameters(protoFormals.length), tree.srcPos), false)
1589+
else (errorType(WrongNumberOfParameters(tree, params.length, pt, protoFormals.length), tree.srcPos), false)
15901590

15911591
/** Is `formal` a product type which is elementwise compatible with `params`? */
15921592
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)