Skip to content

Commit

Permalink
Add better explanation to error message (#18665)
Browse files Browse the repository at this point in the history
Fixes #18657
  • Loading branch information
bishabosha authored Oct 24, 2023
2 parents 6b8b02d + 6defc38 commit 9fccb7e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
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 @@ -1811,10 +1811,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 @@ -1586,7 +1586,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

0 comments on commit 9fccb7e

Please sign in to comment.