Skip to content

Moved error message to case class scheme #2731

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public enum ErrorMessageID {
ValueClassNeedsExactlyOneValParamID,
OnlyCaseClassOrCaseObjectAllowedID,
ExpectedClassOrObjectDefID,
AnonymousFunctionMissingParamTypeID
;

public int errorNumber() {
Expand Down
28 changes: 28 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ object messages {
|Or, add an explicit `()' as a parameter list to ${cdef.name}."""
}

case class AnonymousFunctionMissingParamType(param: untpd.ValDef,
args: List[untpd.Tree],
tree: untpd.Function,
pt: Type)
(implicit ctx: Context)
extends Message(AnonymousFunctionMissingParamTypeID) {
val kind = "Syntax"

val msg = {
val ofFun =
if (MethodType.syntheticParamNames(args.length + 1) contains param.name)
i" of expanded function $tree"
else
""

i"missing parameter type for parameter ${param.name}$ofFun, expected = $pt"
}

val explanation =
hl"""|Anonymous functions must define a type. For example, if you define a function like this one:
|
|${"val f = { case xs @ List(1, 2, 3) => Some(xs) }"}
|
|Make sure you give it a type of what you expect to match and help the type inference system:
|
|${"val f: Seq[Int] => Option[List[Int]] = { case xs @ List(1, 2, 3) => Some(xs) }"} """
}


// Type Errors ------------------------------------------------------------ //
case class DuplicateBind(bind: untpd.Bind, tree: untpd.CaseDef)(implicit ctx: Context)
Expand Down
7 changes: 1 addition & 6 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}
case _ =>
}
val ofFun =
if (MethodType.syntheticParamNames(args.length + 1) contains param.name)
i" of expanded function $tree"
else
""
errorType(i"missing parameter type for parameter ${param.name}$ofFun, expected = $pt", param.pos)
errorType(AnonymousFunctionMissingParamType(param, args, tree, pt), param.pos)
}

def protoFormal(i: Int): Type =
Expand Down
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -802,5 +802,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val err :: Nil = messages
assertEquals(err, ExpectedClassOrObjectDef())
}
@Test def anonymousFunctionMissingParamType =
checkMessagesAfter("refchecks") {
"""
|object AnonymousF {
| val f = { case l@List(1,2,3) => Some(l) }
|}""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
val defn = ictx.definitions

assertMessageCount(1, messages)
val AnonymousFunctionMissingParamType(param, args, _, pt) :: Nil = messages
assertEquals("x$1", param.show)
assertEquals(s"List(ValDef(${param.show},TypeTree,EmptyTree))", args.toString)
assertEquals("?", pt.show)
}

}