Skip to content

Commit 6899cf7

Browse files
Backport "Add better explanation to error message" to LTS (#20735)
Backports #18665 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 8aafec4 + 90cfbb4 commit 6899cf7

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,20 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
17951795
| - a reference to `this`, or
17961796
| - a selection of an immutable path with an immutable value."""
17971797

1798-
class WrongNumberOfParameters(expected: Int)(using Context)
1798+
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
17991799
extends SyntaxMsg(WrongNumberOfParametersID) {
1800-
def msg(using Context) = s"Wrong number of parameters, expected: $expected"
1801-
def explain(using Context) = ""
1800+
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"
1801+
def explain(using Context) =
1802+
val ending = if foundCount == 1 then "" else "s"
1803+
i"""The function literal
1804+
|
1805+
| $tree
1806+
|
1807+
|has $foundCount parameter$ending. But the expected type
1808+
|
1809+
| $pt
1810+
|
1811+
|requires a function with $expectedCount parameters."""
18021812
}
18031813

18041814
class DuplicatePrivateProtectedQualifier()(using Context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15621562
/** Returns the type and whether the parameter is erased */
15631563
def protoFormal(i: Int): (Type, Boolean) =
15641564
if (protoFormals.length == params.length) (protoFormals(i), isDefinedErased(i))
1565-
else (errorType(WrongNumberOfParameters(protoFormals.length), tree.srcPos), false)
1565+
else (errorType(WrongNumberOfParameters(tree, params.length, pt, protoFormals.length), tree.srcPos), false)
15661566

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

tests/neg/i18657.check

+17
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

+2
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)