Skip to content
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

Add better explanation to error message #18665

Merged
merged 1 commit into from
Oct 24, 2023
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
18 changes: 14 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ extends Message(PatternMatchExhaustivityID) {

val pathes = List(
ActionPatch(
srcPos = endPos,
srcPos = endPos,
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
.mkString("\n", "\n", "")
),
Expand Down Expand Up @@ -1806,10 +1806,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 @@ -1574,7 +1574,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the reader can infer from looking at the printed tree, but perhaps also explain that placeholder parameters always infer parameters 1 level of nesting up from their position (however best to explain that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, not sure how to explain that, in a situation like this, where context is missing.

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