Skip to content

Backport "Add better explanation to error message" to LTS #20735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1795,10 +1795,20 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
| - a reference to `this`, or
| - a selection of an immutable path with an immutable value."""

class WrongNumberOfParameters(expected: Int)(using Context)
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
extends SyntaxMsg(WrongNumberOfParametersID) {
def msg(using Context) = s"Wrong number of parameters, expected: $expected"
def explain(using Context) = ""
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"
def explain(using Context) =
val ending = if foundCount == 1 then "" else "s"
i"""The function literal
|
| $tree
|
|has $foundCount parameter$ending. But the expected type
|
| $pt
|
|requires a function with $expectedCount parameters."""
}

class DuplicatePrivateProtectedQualifier()(using Context)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
/** Returns the type and whether the parameter is erased */
def protoFormal(i: Int): (Type, Boolean) =
if (protoFormals.length == params.length) (protoFormals(i), isDefinedErased(i))
else (errorType(WrongNumberOfParameters(protoFormals.length), tree.srcPos), false)
else (errorType(WrongNumberOfParameters(tree, params.length, pt, protoFormals.length), tree.srcPos), false)

/** Is `formal` a product type which is elementwise compatible with `params`? */
def ptIsCorrectProduct(formal: Type) =
Expand Down
17 changes: 17 additions & 0 deletions tests/neg/i18657.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- [E086] Syntax Error: tests/neg/i18657.scala:2:27 --------------------------------------------------------------------
2 |val f: (Int, Int) => Int = Integer.compare(_ + 1, _) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| Wrong number of parameters, expected: 2
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| The function literal
|
| _$2 => Integer.compare(_$1 => _$1 + 1, _$2)
|
| has 1 parameter. But the expected type
|
| (Int, Int) => Int
|
| requires a function with 2 parameters.
---------------------------------------------------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions tests/neg/i18657.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//> using options -explain
val f: (Int, Int) => Int = Integer.compare(_ + 1, _) // error
Loading